我在我的REST API中使用Jersey 2.10和Jackson序列化/反序列化功能.
我的想法是让我的REST API始终返回标准的JSON错误响应.为此,我有ExceptionMapper类,为Jersey应用程序中抛出的任何异常构建正确的json错误响应.我还有一个生成相同类型的JSON响应的jsp,我在web.xml中注册为error-page,它涵盖了在加载Jersey之前可能出现的所有错误.
但是有一种情况,我的Exception mappers和我的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: org.glassfish.jersey.message.internal.EntityInputStream@1dcccac; line: 1, column: 1]
Run Code Online (Sandbox Code Playgroud)
如何让泽西岛返回我的自定义错误响应而不是这个?
更新:
基于@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);
}
}
Run Code Online (Sandbox Code Playgroud)