首页 > 解决方案 > 在 JAX-RS 1.1.0 中处理未知属性

问题描述

我坚持使用 JAX-RS 1.1.0 来使用 REST,而我正在使用 javax.validation 1.1.0.FINAL。

当消费者在 JSON 中发送未知属性时,他们会收到UnrecognizedPropertyException500 错误代码和大量 yadi yadi..

我还没有找到一种方法来捕获这个异常并发回我选择的东西(例如 400 错误)

签名如下所示:

@POST
@Consumes(MediaType.APPLICATION_JSON)
public Response storeDocument(DokumentData dokumentData)

在这个旧版本的 JAX-RS 中有没有办法很好地处理这个异常?在 2.0 版中有,@JsonIgnoreProperties(ignoreUnknown = true)但我不能使用它。

堆栈跟踪:

 [ERROR   ] An unhandled exception occurred which will be propagated to the 
    container.
    Unrecognized field "personnummer1" (Class 
    se.skandia.ad14.valueobjects.DokumentData), not marked as ignorable
    at [Source: com.ibm.ws.webcontainer.srt.SRTInputStream@46611da2; line: 6, 
    column: 23] (through reference chain: 
    se.skandia.ad14.valueobjects.DokumentData["personnummer1"])
    [ERROR   ] SRVE0777E: Exception thrown by application class 
    'org.apache.wink.server.internal.RequestProcessor.handleRequest:195'
    javax.servlet.ServletException: 
    org.codehaus.jackson.map.exc.UnrecognizedPropertyException: Unrecognized 
    field "personnummer1" (Class se.skandia.ad14.valueobjects.DokumentData), not 
    marked as ignorable
    at [Source: com.ibm.ws.webcontainer.srt.SRTInputStream@46611da2; line: 6, 
    column: 23] (through reference chain: 
    se.skandia.ad14.valueobjects.DokumentData["personnummer1"])
    at org.apache.wink.server.internal.RequestProcessor.handleRequest(RequestProcessor.java:195)
    at [internal classes]
Caused by: org.codehaus.jackson.map.exc.UnrecognizedPropertyException: Unrecognized field "personnummer1" (Class 
   se.skandia.ad14.valueobjects.DokumentData), not marked as ignorable
 at [Source: com.ibm.ws.webcontainer.srt.SRTInputStream@46611da2; line: 6, column: 23] (through reference chain: se.skandia.ad14.valueobjects.DokumentData["personnummer1"])
    at org.codehaus.jackson.map.exc.UnrecognizedPropertyException.from(UnrecognizedPropertyException.java:53)
    ... 1 more

标签: javajax-rswebsphere-8

解决方案


推荐阅读