我有一个使用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为null,或者theString为null。相反,我得到了这个:
根本原因
java.io.EOFException: No content to map to Object due to end of input 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):
{}
问题:有没有办法接受空请求有效负载,并在服务器端处理它?</ b>
注意:我正在使用泽西岛提供的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>
答案 0 :(得分:1)
尝试实现ContainerRequestFilter并在有效负载为空时中止请求。
requestContext.abortWith(Response.status(Response.Status.BAD_REQUEST)
.entity("Payload is null")
.build());
可以找到更多详细信息here