0

我创建了一个类来覆盖 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;
}
4

0 回答 0