ExceptionMapper方法toResponse没有调用

时间:2018-04-03 12:48:58

标签: java jax-rs jersey-2.0

不调用ExceptionMapper类的toResponse方法。

代码

public class PortalExceptionMapper implements  ExceptionMapper<ConstraintViolationException> {

@Override
public Response toResponse(ConstraintViolationException exception) {
System.out.println("%%%%%%%%%%%%%%%%%%%%%%%% testing product%%%%%%%%%%%%%%%%%%%%%%%%%%%%");
    return Response.status(Response.Status.BAD_REQUEST)
            .entity(prepareMessage(exception))
            .type("application/json")
            .build();


}

private String prepareMessage(ConstraintViolationException exception) {
    String msg = "";
    for (ConstraintViolation<?> cv : exception.getConstraintViolations()) {
        msg+=cv.getPropertyPath()+" "+cv.getMessage()+"\n";
    }
    return msg;
}

}

=============================================== ======

这个exceptionMapper类在ResourcesConfig上注册

public class PortalApp extends ResourceConfig 
{
public PortalApp()
{
    register(AccessRequestFilter.class);
    packages("org.learn");      
    register(JacksonFeature.class);
    register(PortalExceptionMapper.class);
    register(PortalJackson.class);
    register(PortalFilter.class);


}

请帮忙。感谢

1 个答案:

答案 0 :(得分:0)

尝试使用@Provider标记您的课程:

@Provider
public class PortalExceptionMapper implements  ExceptionMapper<ConstraintViolationException> {...}