处理没有身体的POST

时间:2013-12-06 12:23:54

标签: java jax-rs resteasy

我正在使用JAX-RS 2.0编写一个REST服务来模仿现有的HTTP接口。

这包括针对路径发布空请求,然后将内容POST到同一路径。

现在我的资源看起来像这样:

@Path("data")
public class MyResource {
  @Inject
  MyService service;

  @POST
  @Produces(MediaType.APPLICATION_OCTET_STREAM)
  public byte[] init() {
    return service.init();
  }

  @POST
  @Produces(MediaType.APPLICATION_OCTET_STREAM)
  @Consumes(MediaType.APPLICATION_OCTET_STREAM)
  public byte[] work(byte[] data) {
    return service.work(data);
  }
}

不幸的是,当客户端尝试POST其init时,会发生以下错误:

org.jboss.resteasy.spi.BadRequestException: content-type was null and expecting to extract a body into public javax.ws.rs.core.Response rest.MyResource.work(byte[])
        at org.jboss.resteasy.core.MessageBodyParameterInjector.inject(MessageBodyParameterInjector.java:131)
        at org.jboss.resteasy.core.MethodInjectorImpl.injectArguments(MethodInjectorImpl.java:114)
        at org.jboss.resteasy.core.MethodInjectorImpl.invoke(MethodInjectorImpl.java:137)
        at org.jboss.resteasy.core.ResourceMethod.invokeOnTarget(ResourceMethod.java:252)
        at org.jboss.resteasy.core.ResourceMethod.invoke(ResourceMethod.java:217)
        at org.jboss.resteasy.core.ResourceMethod.invoke(ResourceMethod.java:206)
        at org.jboss.resteasy.core.SynchronousDispatcher.getResponse(SynchronousDispatcher.java:514)
        at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:491)
        at org.jboss.resteasy.core.SynchronousDispatcher.invokePropagateNotFound(SynchronousDispatcher.java:140)
        at org.jboss.resteasy.plugins.server.servlet.ServletContainerDispatcher.service(ServletContainerDispatcher.java:204)
        at org.jboss.resteasy.plugins.server.servlet.FilterDispatcher.doFilter(FilterDispatcher.java:59)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
        at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:235)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
        at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:190)
        at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:92)
        at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.process(SecurityContextEstablishmentValve.java:126)
        at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.invoke(SecurityContextEstablishmentValve.java:70)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
        at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:158)
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:330)
        at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:829)
        at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:598)
        at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
        at java.lang.Thread.run(Thread.java:662)

有没有办法通过JAX-RS解决这个问题,还是我必须回到Servlet来处理这种特殊情况?

1 个答案:

答案 0 :(得分:0)

我无法重现这个问题。考虑这个JAX-RS方法:

@POST
@Path("/bytes")
@Consumes(MediaType.APPLICATION_OCTET_STREAM)
public Response postOctetStream(byte[] bytes) {
    return Response.ok(bytes.length).build();
}

考虑这个curl电话:

$ curl -v -X POST -H "Content-Type: application/octet-stream" -d "" http://localhost:8080/WebApplication4/webresources/bytes
* About to connect() to localhost port 8080 (#0)
*   Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 8080 (#0)
> POST /WebApplication4/webresources/foo/bytes HTTP/1.1
> User-Agent: curl/7.29.0
> Host: localhost:8080
> Accept: */*
> Content-Type: application/octet-stream
> Content-Length: 0
> 
< HTTP/1.1 200 OK
< X-Powered-By: Servlet/3.1 JSP/2.3 (GlassFish Server Open Source Edition  4.0  Java/Oracle Corporation/1.7)
< Server: GlassFish Server Open Source Edition  4.0 
< Content-Type: text/plain
< Date: Mon, 09 Dec 2013 10:56:55 GMT
< Content-Length: 1
< 

如您所见,客户端设置Content-Length: 0。服务器可以将此空体映射到零长度byte[]