I saw a lot of people had struggled with "MessageBodyWriter not found" using Jersey (2.11 for me) for a REST server, but none of their problems seem to be related to my case.
I had a simple REST method, everything was working fine. Then I moved to a custom ResourceMethodInvocationHandlerProvider.
I implement ResourceMethodInvocationHandlerProvider:
public class MyResourceHandlerProvider implements ResourceMethodInvocationHandlerProvider{
public void someMethodInvokedEarly() {
Set<Class<?>> res = new HashSet<Class<?>>();
res.add(some.package.MyInteface.class);
res.add(some.package.MyException.class);
ResourceConfig packagesResourceConfig = new ResourceConfig(res);
packagesResourceConfig.register(JacksonFeature.class);
packagesResourceConfig.registerInstances(new org.glassfish.hk2.utilities.binding.AbstractBinder() {
@Override
protected void configure() {
bind(MyResourceHandlerProvider.class).to(ResourceMethodInvocationHandlerProvider.class);
}
});
}
@Override
public InvocationHandler create(Invocable arg0) {
return new MyInvocationHandler();
}
}
As you see I give him MyInvocationHandler that is like this:
public class MyInvocationHandler implements InvocationHandler {
final MyIntefaceImplemented target = GlobalItems.myIntefaceImplementedInstance;
public MyInvocationHandler() {
Class<?>[] clazz = new Class<?>[1];
clazz[0] = some.package.MyInterface.class;
}
@Override
public Object invoke(Object obj, Method method, Object[] args)
throws Throwable {
GenericEntity entity = null;
Object ret = method.invoke(target , args);
if(ret instanceof List<?>) {
entity = new GenericEntity<List>((List)p) {};
}
return Response.status(Status.OK).entity(entity).type(MediaType.APPLICATION_JSON).build();
}
}
If I don't go through this, I get a response 200, with a well formated body.
But when I go through this, I get a
GRAVE: MessageBodyWriter not found for media type=text/xml, type=class org.glassfish.jersey.message.internal.OutboundJaxrsResponse, genericType=java.util.List<java.lang.String>.
I believe dependencies / registration is not the problem because I got a 200 response before.
Do you have any idea of what could be the cause? Thank you.
Regards, Yutanpo/湯たんぽ
Edit :
More precision. The invocated method
Object ret = method.invoke(target , args);
Does not return a Response. It seems that this is where the problem lies.
The thing is that I want to make "MyInterfaceImplemented" fully available through web services without having to modify its methods return types. This is why I had the idea of implementing the InvocationHandler, to act as a Java Proxy.
Solved.
I thought that
packagesResourceConfig.register(JacksonFeature.class);
and
res.add(JacksonJaxbJsonProvider.class);
ResourceConfig packagesResourceConfig = new ResourceConfig(res);
would be somehow equivalent, but they are not. The second fixed the problem (also in the invocation handler I have to return the entity, not the response)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.