eclipse-ee4j / eclipse-ee4j/jersey

jersey endpoint with method params created programmatically not working

Open
#3,388 4 comments 0 reactions 0 assignees View on GitHub
Component: core Priority: Major Type: Bug
Dominant language
Java
Stars
730
Forks
382
PR merge metrics
No merged PRs in 30d

Description

I am trying to create jersey end-point in runtime, all looks fine except for the method parameters. I use the code below:

public void add(Object object) {
Class clazz = object.getClass();
final Resource.Builder resourceBuilder = Resource.builder().path(clazz.getSimpleName());

for (Method method: clazz.getDeclaredMethods()) {
Builder methodBuilder = resourceBuilder
.addChildResource(method.getName() + "/

{string}

")
.addMethod("GET")
.produces(MediaType.TEXT_PLAIN_TYPE)
.handledBy(object, method);

List parameters = new LinkedList<>();
int index = 1;
for(Class type : method.getParameterTypes()) {
parameters.add(Parameter.create(clazz, clazz, true, type, type, new Annotation[]

{new MyPathParam(index++)}

));
}

methodBuilder
.handlerParameters(parameters);
}
final Resource resource = resourceBuilder.build();
registerResources(resource);
}

I expect that the first argument of the method will bear the @PathParam annotation, however getting the below warning in logs:

May 31, 2016 2:51:50 PM org.glassfish.jersey.internal.Errors logErrors
WARNING: The following warnings have been detected: WARNING: A HTTP GET method, public java.lang.String com.example.service.MyService.cool(java.lang.String), should not consume any entity.

I have debugged the code and found that even though the handler passed to the below method has all parameters with right annotations, the invocable instance is missing annotations because the parameters attribute is initialized based on handlerClass and not on the handler instance, resulting in parameters getting lost during the build stage.

private Invocable(MethodHandler handler, Method definitionMethod, Method handlingMethod, boolean encodedParameters,
Type routingResponseType)

{ … this.parameters = Collections.unmodifiableList(Parameter.create( handlerClass, definitionMethod.getDeclaringClass(), definitionMethod, encodedParameters)); }
#### Affected Versions
[2.22.2]

Contributor guide

Open the contributing guide

Research direction

Start at the Invocable constructor and its Parameter.create call, then reproduce the issue with the programmatic Resource.Builder example and a method using MyPathParam. Verify whether handler parameters survive resource construction; done means the generated endpoint binds its method argument as a path parameter without the entity-consumption warning.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
api, backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.