How to correctly configure resources with jersey programmatically
Solution 1:
The problem is with your lambdas. Basically, you have
Inflector<ContainerRequestContext, ?> lambda = (ContainerRequestContext ctx) -> {
return Response.ok("Posted Profile {" + (ctx == null) + "}").build();
};
If you check the method argument type:
for (Method m : lambda.getClass().getMethods()) {
if ("apply".equals(m.getName())) {
for (Type t : m.getGenericParameterTypes()) {
System.out.println(t); //java.lang.Object
}
}
}
you get java.lang.Object
. So Jersey tries to get Object
type from the entity stream and gets null
.
So you need to tell java to use ContainerRequestContext
argument type by a real anonymous class:
Inflector<ContainerRequestContext, Response> anonymous = new Inflector<>() {
@Override
public Response apply(ContainerRequestContext ctx) {
return Response.ok("Posted Profile {" + (ctx == null) + "}").build();
}
};
for (Method m : anonymous.getClass().getMethods()) {
if ("apply".equals(m.getName())) {
for (Type t : m.getGenericParameterTypes()) {
System.out.println(t); // ContainerRequestContext
}
}
}