I have JAX-RS API endpoints defined in a class. I have methods in the class such as
@GET
@Path("{param1}/{param2}/mypath")
@Consumes({ MediaType.APPLICATION_XML, MediaType.TEXT_XML,
MediaType.APPLICATION_JSON })
@Produces({ MediaType.APPLICATION_XML, MediaType.TEXT_XML,
MediaType.APPLICATION_JSON })
public Response getSomeResource(@PathParam("param1") MyClass1 myclass1,
@PathParam("param2") MyClass2 myclass2 )
{
//business logic goes here
}
Now as per the documentation at -- https://docs.oracle.com/cd/E19776-01/820-4867/6nga7f5n6/index.html , @QueryParam and @PathParam can only be used on the following Java types:
All primitive types except char
All wrapper classes of primitive types except Character
Have a constructor that accepts a single String argument
Any class with the static method named valueOf(String) that accepts
a single String argument
Any class with a constructor that takes a single String as a
parameter
List<T>, Set<T>, or SortedSet<T>, where T matches the already listed
criteria. Sometimes parameters may contain more than one value for
the same name. If this is the case, these types may be used to
obtain all values.
In the codebase, MyClass1
and MyClass2
don't follow any of the above yet everything works fine on PROD. But when I tried to test the same method with Jersey Test framework, jersey gives me the exception on startup :
org.glassfish.jersey.server.model.ModelValidationException: Validation of
the application resource model has failed during application initialization.
[[FATAL] No injection source found for a parameter of type public
javax.ws.rs.core.Response
On further digging I found that above conditions were required for my @PathParams/@QueryParams
bindings for MyClass1
and MyClass2
. I supplied a public constructor with a single String argument to MyClass1
and the problem disappeared partly. I tried the same with MyClass2
but the problem persisted. MyClass2
is a static inner class which has a parent class too. So here are my questions :
- How is it working fine on PROD but complaining on JUnit test?
- Are there any special considerations to be made for static inner classes to be used for binding
@PathParams/@QueryParams
? I gave a public constructor with single String arg to the static inner class but the problem still persisted while for the other class (MyClass1
) it worked. Is there something which I am missing?
Thanks!