我正在使用Jersey为外界提供java REST服务.我提供了一些采用JSON的功能,我将Jackson框架与jersey结合使用,将它们转换为POJO.
我有一个问题,如果错误的杰克逊格式被发送到服务器,答案(http响应的内容)是杰克逊特定的异常描述.如果我有一个带有属性"surname"的POJO并将json字符串中的"sursname"发送到服务器,我得到:
Unrecognized field "sursname" (Class XY), not marked as ignorable at [Source: sun.net.httpserver.FixedLengthInputStream@903025; line: 1, column: 49] (through reference chain: XY["sursname"])
Run Code Online (Sandbox Code Playgroud)
这很好,但我想有自己的响应内容,例如我自己的错误代码.我已经编写了一个自定义的ExceptionMapper,它应该映射所有Throwables.
@Provider
public class WebserviceExceptionMapper implements ExceptionMapper<Throwable> {
@Override
public Response toResponse(Exception e) {
e.printStackTrace();
return Response.status(400).entity("{\"errorcode\":\"CRITICAL_ERROR\"}").type(MediaType.APPLICATION_JSON).build();
}
}
Run Code Online (Sandbox Code Playgroud)
似乎在调用我的webservice方法之前抛出了jackson异常,所以我没有机会映射它?
有没有人有想法?非常感谢,对不起我的英语;)