我有一个使用 Jersey 编写的 RESTful Web 服务,如下所示:
@POST
@Consumes(MediaType.APPLICATION_JSON)
public Response myAction(RequestObject requestObject) {
// do some action
}
请求对象只是一个简单的 DTO:
public class RequestObject implements Serializable {
private String theString;
// Getter for member
}
当我尝试使用空请求有效负载进行 POST 时,我希望 RequestObject 要么为空,要么 theString 为空。相反,我得到了这个:
根本原因
java.io.EOFException:由于输入结束,没有要映射到 Object 的内容 org.codehaus.jackson.map.ObjectMapper._initForReading(ObjectMapper.java:2766) org.codehaus.jackson.map.ObjectMapper._readValue(ObjectMapper.java:2682) org.codehaus.jackson.map.ObjectMapper.readValue(ObjectMapper.java:1308) org.codehaus.jackson.jaxrs.JacksonJsonProvider.readFrom(JacksonJsonProvider.java:419) com.sun.jersey.json.impl.provider.entity.JacksonProviderProxy.readFrom(JacksonProviderProxy.java:139) com.sun.jersey.spi.container.ContainerRequest.getEntity(ContainerRequest.java:483) com.sun.jersey.server.impl.model.method.dispatch.EntityParamDispatchProvider$EntityInjectable.getValue(EntityParamDispatchProvider.java:123) com.sun.jersey.server.impl.inject.InjectableValuesProvider.getInjectableValues(InjectableValuesProvider.java:46) com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$EntityParamInInvoker.getParams(AbstractResourceMethodDispatchProvider.java:153) com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$ResponseOutInvoker._dispatch(AbstractResourceMethodDispatchProvider.java:203) com.sun.jersey.server.impl.model.method.dispatch.ResourceJavaMethodDispatcher.dispatch(ResourceJavaMethodDispatcher.java:75) com.sun.jersey.server.impl.uri.rules.HttpMethodRule.accept(HttpMethodRule.java:302) com.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:108) com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147) com.sun.jersey.server.impl.uri.rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:84) com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1480) com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1411) com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1360) com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1350) com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:416) com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:538) com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:716) javax.servlet.http.HttpServlet.service(HttpServlet.java:803) org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
当我使用看起来像这样的请求有效负载发布时,我得到了所需的结果(theString 设置为 null)。:
{}
问题:有没有办法接受一个空的请求负载,并在服务器端处理它?
注意:我正在使用 Jersey 提供的 POJO 支持。
<servlet>
<servlet-name>jersey-serlvet</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
<param-value>true</param-value>
</init-param>
</servlet>