异常mapper方法toresponse未调用



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;
}

}

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

resources Config上的此exceptionMapper类登记

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);

}

请帮忙。谢谢

尝试用@provider标记您的类:

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

最新更新