我在我的REST API中使用Jersey 2.10的Jackson序列化/反序列化功能。澤西島反序列化失敗時異常映射器不工作
我的想法是讓我的REST API總是返回一個標準的JSON錯誤響應。爲此,我有了ExceptionMapper類,它可以爲Jersey應用程序中拋出的任何異常構建適當的json錯誤響應。我也有一個jsp,它生成相同類型的JSON響應,我在web.xml中將其註冊爲錯誤頁面,其中包含可能在Jersey加載之前發生的所有錯誤。
但在這既不是我的異常映射器也不是我的JSON生產的jsp正在一個情況,那就是發送一個壞建制JSON的POST REST端點剛剛返回以下消息時:
HTTP/1.1 400 Bad Request
Server: Apache-Coyote/1.1
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: GET, POST, DELETE, PUT
Content-Type: text/plain
Content-Length: 210
Date: Tue, 24 Jun 2014 22:14:11 GMT
Connection: close
Can not deserialize instance of com.example.rest.User[] out of START_OBJECT token
at [Source: [email protected]; line: 1, column: 1]
我如何讓澤西返回我的自定義錯誤響應,而不是這個?
UPDATE:
基於由@Lucasz答案,我做更多的研究,發現有包com.fasterxml.jackson.jaxrs.base(https://github.com/FasterXML/jackson-jaxrs-providers/tree/master/base/src/main/java/com/fasterxml/jackson/jaxrs/base)JsonMappingExceptionMapper裏面定義了兩個異常映射器和JsonParseExceptionMapper似乎隱藏我的自定義映射器。
我該如何註銷這些映射器?
這是我當前如何註冊映射器:
@ApplicationPath("/")
public class MyApp extends ResourceConfig{
public SyntheticAPIApp() {
packages("com.example.resource", "com.example.mapper");
register(org.glassfish.jersey.jackson.JacksonFeature.class);
}
}
通常,無效的JSON會觸發500內部服務器錯誤響應。它看起來像一些異常映射器工作並將JsonParseExcpetion轉換爲400響應,並從異常消息中獲取正文。 –
但我沒有任何生成文本/純文本響應的異常映射器,似乎有人攔截了該異常,因爲我甚至沒有在日誌中看到它 – raspacorp