我创建了一个类来覆盖JsonMappingException。这是我的自定义异常处理程序。
@Provider
public class JsonMappingExceptionMapper implements ExceptionMapper<JsonMappingException> {
public JsonMappingExceptionMapper(){
System.out.println("I am called");
}
@Override
public Response toResponse( JsonMappingException e ) {
System.out.println("Should be called");
return Response.status(Status.BAD_REQUEST).type(MediaType.TEXT_PLAIN)
.entity("The supplied JSON contained an invalid property: " + "").build();
}
}
但是这没有被调用,而是我收到HTTP 500错误。
Caused by: org.codehaus.jackson.map.JsonMappingException: Can not construct instance of boolean from String value '': only "true" or "false" recognized
at [Source: org.apache.cxf.transport.http.AbstractHTTPDestination$1@463a4396; line: 5, column: 20]
at org.codehaus.jackson.map.JsonMappingException.from(JsonMappingException.java:160)
我还将已编译的自定义ExceptionMapper类打包到我的Web应用程序项目中。
public class MyApplication extends Application{
@Override
public Set<Object> getSingletons()
{
Set<Object> singleton = new HashSet<Object>();
singleton.add(new JsonMappingExceptionMapper());
return singleton;
}