[英]how to map JSON parsing related errors to json response in jersey
我定義了一個數據類型及其 API:
public class DataType {
@Column
private String name;
}
// API is:
public class DataTypeAPI {
@POST
@Path("/")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@ManagedAsync
public void createDataType(
final DataType dataType,
) {
...
asyncResponse.resume(Response.status(Response.Status.CREATED)
.entity(dataType)
.build());
}
}
如果我擺姿勢{ "name": "xxx" }
,一切都很好,
但是當我發布{ "name1": "xxx" }
,我得到了以下text/plain
回復:
Unrecognized field "name1" (class com.xxx.datatypes.DataType), not marked as ignorable (1 known properties: "name"])
at [Source: org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$UnCloseableInputStream@526e34b1; line: 2, column: 15] (through reference chain: com.xxx.datatypes.DataType["name1"])
我更喜歡將上述錯誤轉換為 JSON 響應。 但是事件我添加了以下異常映射器,它沒有返回 JSON 響應。
@Provider
public class GenericExceptionMapper implements ExceptionMapper<Throwable> {
public Response toResponse(Throwable ex) {
System.out.println("GenericExceptionMapper");
ex.printStackTrace();
return Response.status(Response.Status.INTERNAL_SERVER_ERROR)
.entity(new ErrorBody(500, ex.getMessage()))
.type(MediaType.APPLICATION_JSON)
.build();
}
}
為什么我上面的異常映射器無法捕獲球衣解析錯誤。 有人可以向我解釋一下嗎? 謝謝
更新
我有兩個問題:
1、如何讓響應為application/json
而不是text/plain
?
2、為什么我的異常映射器沒有捕獲引發的異常並將其映射到json響應?
更新
我添加了以下映射器:
public class JsonMappingExceptionMapper implements ExceptionMapper<JsonMappingException> {
public Response toResponse(JsonMappingException ex) {
return Response.status(Response.Status.INTERNAL_SERVER_ERROR)
.entity(new ErrorBody(500, ex.getMessage()))
.type(MediaType.APPLICATION_JSON)
.build();
}
}
現在,我可以得到 json 響應: { "code": 500, "message": "Unrecognized field \\"name1\\" (class com.xxx.datatypes.DataType), not marked as ignorable (1 known properties: \\"name\\"])\\n at [Source: org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$UnCloseableInputStream@1d8cd12a; line: 2, column: 15] (through reference chain: class com.xxx.datatypes.DataType[\\"name1\\"])" }
我還有兩個問題:
1、為什么我的通用異常映射器無法捕獲它。
2、 json映射異常映射器無法映射以下異常Unexpected character ('}' (code 125)): was expecting double-quote to start field name at [Source: org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$UnCloseableInputStream@7615743c; line: 3, column: 2]
Unexpected character ('}' (code 125)): was expecting double-quote to start field name at [Source: org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$UnCloseableInputStream@7615743c; line: 3, column: 2]
when {"name": "xxxx",}
發布(逗號,添加用於測試)
為什么我的通用異常映射器無法捕獲它。
因為 Jackson-JAXRS 模塊已經為 Jackson 異常提供了ExceptionMappers 。
json映射異常映射器無法映射以下異常
我沒有測試這個,但很可能是因為它不是映射異常,而是解析異常。 Jackson 有JsonParseException
和JsonMappingException
。 您可以在上一個鏈接中看到 Jackson 為JsonMappingException
和JsonParseException
提供了一個 ExceptionMapper 。 您只是覆蓋了JsonMappingException
的映射器。
我遇到了同樣的問題,我已經解決了。 我試着解釋你的問題:
JsonMappingException
和JsonParseException
擴展了JsonProcessingException
。 澤西定義JsonMappingExceptionMapper
為JsonMappingException
和JsonParseExceptionMapper
為JsonParseException
。
JsonProcessingException
(或更廣泛的異常映射器)定義自己的映射器,則它無法捕獲JsonMappingException
和JsonParseException
,因為這兩個異常有自己的映射器。 如果他們沒有,您的映射器可以捕獲。XxxJsonMappingExceptionMapper
的JsonMappingException
和YyyJsonParseExceptionMapper
的JsonParseException
),您可能會發現有時您可以捕獲這些異常,有時不能,這取決於您自己的映射器正在工作還是球衣的映射器正在工作。 現在,您可以將@Priority(1)
(javax.annotation.Priority) 添加到您的映射器以確保您的映射器正常工作。 請參閱Jackson JsonParseExceptionMapper 和 JsonMappingExceptionMapper 陰影自定義映射器。回到你的問題:
JsonMappingExceptionMapper
和JsonParseExceptionMapper
,它們具有更高的優先級)。JsonParseExceptionMapper
遮蔽了。 嘗試通過將@Priority(1)
添加到您自己的映射器來弄清楚無法識別的字段“name1”(類 com.xxx.datatypes.DataType),未標記為可忽略(1 個已知屬性:“name”])
如果您查看引發的異常,很明顯,您的 POJO 類具有名稱name
而非name1
屬性。
而且,您正在嘗試使用屬性名稱name1
發布您的 JSON。
{ "name1": "xxx" }
並且,它沒有被識別為name1
沒有定義而不是name
。
public class DataType {
@Column
private String name;
}
這就是原因,在其他情況下,當您將 JSON 發布為
{ "name": "xxx" }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.