简体   繁体   English

从Jersey迁移到RESTEasy时,内容类型为空。

[英]Null content-type when migrating from Jersey to RESTEasy.

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. 所以我编写了一个示例REST资源,其工作方式类似于Jersey / Tomcat中的一个魅力,但是当我把它带到RestEASY / Tomcat时,它就会受到打击。 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 ) 尝试访问资源时出现此错误( http:// localhost:7070 / mg / mytest

"content-type was null and expecting to extract a body" “content-type为null并期望提取一个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) 7842 [http-7070-2]错误com.loyalty.mg.rest.exception.MGExceptionMapper - 在异常映射器中捕获到错误 - org.jboss.resteasy.spi.BadRequestException:content-type为null并且期望在以下处提取正文org.jboss.resteasy.core.MessageBodyParameterInjector.inject(MessageBodyParameterInjector.java:131)org.jboss.resteasy.core.MethodInjectorImpl.injectArguments(MethodInjectorImpl.java:98)org.jboss.resteasy.core.MethodInjectorImpl.invoke(在org.jboss.resteasy上的org.jboss.resteasy.core.ResourceMethod.invokeOnTarget(ResourceMethod.java:247)org.jboss.reste上的org.jboss.resteasy.core.ResourceMethod.invoke(ResourceMethod.java:212)中的MethodInjectorImpl.java:121) .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. 我想这个问题也是 - RestEASY比泽西更好,这只是一个开始而且我得到了错误。 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>

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. 问题似乎是RestEASY无法从收到的请求的标头中找出内容类型。 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. 这表明请求中的内容类型是伪造的,或者说您配置RestEASY的方式存在问题。

I guess the question also is - is RestEASY any better than Jersey, this is just the start and I am getting errors. 我想这个问题也是 - RestEASY比泽西更好,这只是一个开始而且我得到了错误。 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. 但是,我认为你太快速责怪RestEASY 可能是你代码的错。

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. ...而且你忘记用@PathParam注释bar参数。 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. 然后,RestEasy认为它应该是从请求正文中读取条形图,而不是从URL路径中读取条形图,并将查看此异常。

That doesn't seem to be what's happening in your case, but I got the same exception, and this was the cause. 这似乎不是你的情况下发生的事情,但我得到了同样的例外,这就是原因。

RestEASY vs Jersey is hard to say: http://www.infoq.com/news/2008/10/jaxrs-comparison RestEASY vs Jersey很难说: 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注释会发生什么,例如:

@Produces("application/json")
@GET
public Response getData() {
  ...
}

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. 因此,与其他非属性RESTLET框架相比,RestEasy不应该得到糟糕的说唱。

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. 实际上我认为JBoss RestEasy具有最轻的占地面积,它不会因不必要的* .jars,灵活,完全认证的JAX-RS实现而变得臃肿,完整且易于使用,这是无法比拟的。

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? 有人说,GET请求不应该在请求中期望Content_Type,(并且我同意),但是对于每个GET请求,必须指出您打算将哪些内容发送回请求者? Right! 对! (will it be JSON, XML, plain text, XML and a sheetsheet, multi-part, etc). (它将是JSON,XML,纯文本,XML和工作表,多部分等)。 Well RestEasy, JBoss's framework addresses this with annotation as shown below, and configurable per URL REST request. Well RestEasy,JBoss的框架通过如下所示的注释解决了这个问题,并且可以根据URL REST请求进行配置。 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());  
}   

a GET request must not have a body , and an application must not expet a Content-Type header. GET请求必须没有正文 ,并且应用程序不得使用Content-Type标头。

If this is a bug of RestEASY, it makes one wonder how many people really are using the software. 如果这是RestEASY的错误,那就让人怀疑有多少人真的在使用该软件。

EDIT 编辑

RFC2616 $4.3 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. 如果请求方法的规范(第5.1.1节)不允许在请求中发送实体主体,则消息主体不得包含在请求中。

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. GET方法“不允许在请求中发送实体主体”,因此GET请求可能有一个主体。 But GET "does not include defined semantics for an entity-body" therefore the body should be ignored anyway. 但是GET “不包括实体主体的定义语义”,因此无论如何都应该忽略主体。

In any case, RestEASY should not have required the presence of Content-Type in a GET request. 在任何情况下,RestEASY都不应要求在GET请求中存在Content-Type。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM