Hi.
I have the following JAX-RS resource class:

@Path("user")
public class UserResource {
  private final UserHandler m_userHandler = 
Program.Injector.getInstance(UserHandler.class);

  @GET
  @Path("{id}")
  public User getUser(@PathParam("id") int id) {
    User user = m_userHandler.get(id);
    if (user == null) {
      throw new WebApplicationException(Response.Status.NOT_FOUND);
    }
    return user;
  }
}

I am using neither the @Consumes nor the @Produces annotation, because I do not 
want to limit the User resource to a predefined set of representations. On the 
other hand, I have no problem to declare supported media type writers somewhere 
in a resource agnostic place, although I have no idea where.

Anyway, when I am trying to GET the resource at http://localhost/user/1, I get 
back this:

30 nov. 2011 00:55:00 org.restlet.ext.jaxrs.internal.util.ExceptionHandler 
noMessageBodyWriter
ATTENTION: No message body writer found for class class com.shunra.poc.User, 
genericType class com.shunra.poc.User; response media type should be: 
text/html; accepted media types are: [[text/html, application/xhtml+xml, 
application/xml, */*]]

The flow goes like this:
1. Firefox sends Accept: 
text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
2. JaxRsProviders.writerSubSet discovers the following suitable media writers - 
[org.restlet.ext.jaxrs.internal.provider.JaxbProvider, 
org.restlet.ext.jaxrs.internal.provider.ConverterProvider, 
org.restlet.ext.jaxrs.internal.provider.JsonProvider]
3. JaxRsRestlet.determineMediaType translates the result of 
JaxRsProviders.writerSubSet into [application/xml, text/xml, application/*+xml, 
application/json, */*]
4. Next JaxRsRestlet.determineMediaType merges the list of acceptable media 
types [[text/html, application/xhtml+xml, application/xml, */*]] with 
[application/xml, text/xml, application/*+xml, application/json, */*] to yield 
[text/html, application/xhtml+xml, application/xhtml+xml, application/xml, 
application/xml, application/xml, application/xml, text/xml, application/*+xml, 
application/json, */*]
5. Then JaxRsRestlet.determineMediaType returns the first concrete media type 
from the merged list, which is text/html
6. MessageBodyWriterSubSet.getBestWriter tries to cross reference the list of 
suitable providers [org.restlet.ext.jaxrs.internal.provider.JaxbProvider, 
org.restlet.ext.jaxrs.internal.provider.ConverterProvider, 
org.restlet.ext.jaxrs.internal.provider.JsonProvider] with text/html, but none 
of them supports it and so it returns null.
7. The aforementioned exception is thrown.

I think this flow is wrong. Choosing text/html ultimately fails the request, 
but if the code had tried picking the next after it - application/xhtml+xml, 
then the request would have succeeded.

Please advice on how to deal with this issue without annotating the resource 
handler method with @Consumes or @Provides. 
Thanks.

------------------------------------------------------
http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447&dsMessageId=2889157

Reply via email to