gpt4 book ai didi

java - 处理无正文的 POST

转载 作者:行者123 更新时间:2023-12-02 03:06:24 25 4
gpt4 key购买 nike

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

这包括针对路径发布空请求以及稍后将内容发布到同一路径。

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

@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 来处理这种特殊情况?

最佳答案

我无法重现这个问题。考虑这个 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[]

关于java - 处理无正文的 POST,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20423859/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com