3

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>
1
  • I think both Jersey and RestEASY can work pretty well... so I guess one could also ask "why switch in the first place".
    – StaxMan
    Jan 13, 2011 at 23:35

5 Answers 5

4

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.

4
  • 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.
    – kapso
    Jun 22, 2010 at 3:22
  • @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
    Jun 22, 2010 at 4:06
  • @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
    Jun 22, 2010 at 5:24
  • if there is no method annotated as @POST, then a POST would return status 405, Method Not Allowed. (I've tried this in RESTEasy.) Jul 14, 2010 at 19:09
4

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.

1
  • +1 for the @PathParam hint - just caught me out, and this helped fix the problem for me.
    – matt
    Sep 20, 2011 at 10:49
1

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() {
  ...
}
1

Well I know this requested is dated, and so much on the internet old..in a year of two everything usually changes and works better. So RestEasy should not get a bad rap in comparison to other non-propertary RESTLET frameworks.

Actually I think JBoss RestEasy has the lightest footprint, it's not bloated with unnecessary *.jars, flexible, fully certified JAX-RS implementation, complete and its ease of use is beyond comparison.

Some eluded, that a GET request should not expect a Content_Type on the request, (And I agree), but with a every GET request one must indicate what you intend on sending back to the requestor? Right! (will it be JSON, XML, plain text, XML and a sheetsheet, multi-part, etc). Well RestEasy, JBoss's framework addresses this with annotation as shown below, and configurable per URL REST request. Therefore, therein is your answer

 @GET 
 @Path("/echo/{message}")  
 @Produces("text/plain")  
 public String echo(@PathParam("message")String message){  
     return message;      
 }  

 @GET 
 @Path("/employees")  
 @Produces("application/xml")  
 @Stylesheet(type="text/css", href="${basepath}foo.xsl")
 public List<Employee> listEmployees(){  
    return new ArrayList<Employee>(employees.values());  
 }  

 @GET 
 @Path("/employee/{employeeid}")  
 @Produces("application/xml")  
 public Employee getEmployee(@PathParam("employeeid")String employeeId){  
     return employees.get(employeeId);          
 }  

 @GET 
 @Path("/json/employees/")  
 **@Produces("application/json")**  
 public List<Employee> listEmployeesJSON(){  
     return new ArrayList<Employee>(employees.values());  
}   
-1

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.

2
  • In fact it's not prohibited, just unusual. Oct 19, 2010 at 14:52
  • @binary_runner you are right. the HTTP RFC never ceases to surprise me Oct 19, 2010 at 16:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.