java – HTTP状态为500而不是HTTP状态400的JAX-RS响应

有时JAR-RS客户端发送错误的语法请求体.服务器应该响应HTTP status 400 (Bad Request),但它响应HTTP status 500 (Internal Server Error).

码:

JAX-B模型类:

@XmlRootElement(namespace = "http://www.test.com/test")
@XmlAccessorType(value = XmlAccessType.FIELD)
public class TestModel {

    @XmlElement
    private String id;
}

JAX-RS资源类:

@Path("test")
public class TestResource {

    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    public void create(TestModel testModel) {
        // some code
    }
}

CXF配置:

<jaxrs:server address="/rest" id="test" staticSubresourceResolution="true">
    <jaxrs:serviceBeans>
        <ref bean="testResource" /> 
    </jaxrs:serviceBeans>
    <jaxrs:providers>
        <bean class="com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider" />
    </jaxrs:providers>
</jaxrs:server>

例:

请求机构:

{"id2": "test"}

id2是错误的,所以客户端应该得到HTTP status 400,但它得到HTTP status 500.

服务器日志:

Caused by: com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "id2" (class test.TestModel), not marked as ignorable (one known property: "id"])
    at [Source: org.apache.cxf.transport.http.AbstractHTTPDestination$1@6f30793d; line: 1, column: 10] (through reference chain: test.TestModel["id2"])
    at com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException.from(UnrecognizedPropertyException.java:51)
    at com.fasterxml.jackson.databind.DeserializationContext.reportUnknownProperty(DeserializationContext.java:839)
    at com.fasterxml.jackson.databind.deser.std.StdDeserializer.handleUnknownProperty(StdDeserializer.java:1045)
    at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownProperty(BeanDeserializerBase.java:1352)
    at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownVanilla(BeanDeserializerBase.java:1330)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:264)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:125)
    at com.fasterxml.jackson.databind.ObjectReader._bind(ObjectReader.java:1470)
    at com.fasterxml.jackson.databind.ObjectReader.readValue(ObjectReader.java:912)
    at com.fasterxml.jackson.jaxrs.base.ProviderBase.readFrom(ProviderBase.java:811)
    at org.apache.cxf.jaxrs.utils.JAXRSUtils.readFromMessageBodyReader(JAXRSUtils.java:1343)
    at org.apache.cxf.jaxrs.utils.JAXRSUtils.readFromMessageBody(JAXRSUtils.java:1294)
    at org.apache.cxf.jaxrs.utils.JAXRSUtils.processParameter(JAXRSUtils.java:826)
    at org.apache.cxf.jaxrs.utils.JAXRSUtils.processParameters(JAXRSUtils.java:789)
    at org.apache.cxf.jaxrs.interceptor.JAXRSInInterceptor.processRequest(JAXRSInInterceptor.java:212)
    ... 68 more

有没有办法配置Jackson和/或CXF在没有schema validationbean validation的情况下为错误的语法请求主体返回HTTP状态400?

解决方法:

问题是未映射到响应的异常(通过ExceptionMappers)转换为一般服务器错误响应,因为运行时不知道如何处理异常.

jackson-jaxrs-provider模块具有ExceptionMappers来处理Jackson基类异常类JsonMappingException和JsonParseException.映射器分别为JsonMappingExceptionMapperJsonParseExceptionMapper.这些映射器将异常映射到400响应以及异常消息作为响应主体.如果你不喜欢这个响应体,你可以编写自己的映射器.

上一篇:java – 对CXF使用的类中的公共方法的@Transactional问题


下一篇:java – 在CXF中使用rpc / encoded样式的方法