views:

503

answers:

4

So I wrote a sample REST resource that works like a charm in Jersey/Tomcat, but when I take it to RestEASY/Tomcat it blows. I mean really? what happened to working out of the box. Anyway a little frustrated. I get this error when trying to access the resource(http://localhost:7070/mg/mytest)

"content-type was null and expecting to extract a body"

7842 [http-7070-2] ERROR com.loyalty.mg.rest.exception.MGExceptionMapper - Error caught in the exception mapper - org.jboss.resteasy.spi.BadRequestException: content-type was null and expecting to extract a body at org.jboss.resteasy.core.MessageBodyParameterInjector.inject(MessageBodyParameterInjector.java:131) at org.jboss.resteasy.core.MethodInjectorImpl.injectArguments(MethodInjectorImpl.java:98) at org.jboss.resteasy.core.MethodInjectorImpl.invoke(MethodInjectorImpl.java:121) at org.jboss.resteasy.core.ResourceMethod.invokeOnTarget(ResourceMethod.java:247) at org.jboss.resteasy.core.ResourceMethod.invoke(ResourceMethod.java:212) at org.jboss.resteasy.core.ResourceMethod.invoke(ResourceMethod.java:202)

@Path("/mytest")
public class TestResource  {

    @GET
    public Response getData()

I guess the question also is - is RestEASY any better than Jersey, this is just the start and I am getting errors. Should I just stick to Jersey?

Also already tried this as well :)

<context-param>
  <param-name>resteasy.media.type.mappings</param-name>
  <param-value>json : application/json, xml : application/xml</param-value> 
</context-param>
+2  A: 

The code that throws that exception looks like this:

     final MediaType mediaType = request.getHttpHeaders().getMediaType();
     if (mediaType == null) {
        throw new BadRequestException(
             "content-type was null and expecting to extract a body");
     }

The problem seems to be that RestEASY cannot figure out a content type from the headers of the request that it received. This suggests that either that the content type in the request is bogus, or that there is a problem with the way that you have configured RestEASY.

I guess the question also is - is RestEASY any better than Jersey, this is just the start and I am getting errors. Should I just stick to Jersey?

I cannot answer that. However, I think you are being too quick to blame RestEASY for something that could be your code's fault.

Stephen C
i fixed the issue by explicitly passing "Content-Type" in the REST request header. So I guess this header is "mandatory" for RestEASY??And thats my point - this wasn't mandatory for Jersey/JAXRS and thats why I am frustrated with RestEASY.
@Kapil - I would have said that trying to use a RESTful API without setting an explicit content-type is pretty flakey. If anything, I'd have said that the fault was with Jersey/JAXRS for letting you get away with it.
Stephen C
@Kapil - actually, @irreputable has a good point. Could the problem be that your client is sending a "POST" request where it should be sending a "GET"?
Stephen C
@Stephen C, if there is no method annotated as @POST, then a POST would return status 405, Method Not Allowed. (I've tried this in RESTEasy.)
Mark Lutton
+1  A: 

RestEASY vs Jersey is hard to say: http://www.infoq.com/news/2008/10/jaxrs-comparison

Regarding your error, you can control the content type via annotations, what happens if you place @Produces annotation, for example:

@Produces("application/json")
@GET
public Response getData() {
  ...
}
Mohamed Mansour
A: 

a GET request must not have a body, and an application must not expet a Content-Type header.

If this is a bug of RestEASY, it makes one wonder how many people really are using the software.

EDIT

RFC2616 $4.3

A message-body MUST NOT be included in a request if the specification of the request method (section 5.1.1) does not allow sending an entity-body in requests.

A server SHOULD read and forward a message-body on any request; if the request method does not include defined semantics for an entity-body, then the message-body SHOULD be ignored when handling the request.

The GET method does not "does not allow sending an entity-body in request" therefore a GET request COULD have a body. But GET "does not include defined semantics for an entity-body" therefore the body should be ignored anyway.

In any case, RestEASY should not have required the presence of Content-Type in a GET request.

irreputable
In fact it's not prohibited, just unusual.
binary_runner
@binary_runner you are right. the HTTP RFC never ceases to surprise me
irreputable
A: 

A classic cause of this, is if you have code like this:

@GET
@Path("/foo/{bar}")
@Produces(MediaType.TEXT_HTML)
public Response foo(@PathParam("bar") String bar) {

...and you forget to annotate the bar argument with @PathParam. Then RestEasy thinks it should be reading bar from the body of the request, instead of from the URL path, and will chuck this exception.

That doesn't seem to be what's happening in your case, but I got the same exception, and this was the cause.

Eric Bowman - abstracto -