gpt4 book ai didi

java - 具有 HTTP 状态 500 而不是 HTTP 状态 400 的 JAX-RS 响应

转载 作者:搜寻专家 更新时间:2023-10-31 20:17:56 27 4
gpt4 key购买 nike

有时 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 validation 的情况下为错误的语法请求主体返回 HTTP 状态 400?或 bean validation

最佳答案

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

jackson-jaxrs-provider 模块有 ExceptionMapper 来处理 Jackson 基异常类 JsonMappingExceptionJsonParseException。映射器是 JsonMappingExceptionMapperJsonParseExceptionMapper , 分别。这些映射器会将异常映射到 400 响应以及作为响应主体的异常消息。如果您不喜欢这个响应主体,您可以编写自己的映射器。

关于java - 具有 HTTP 状态 500 而不是 HTTP 状态 400 的 JAX-RS 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36865212/

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