不调用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);
}
请帮忙。感谢
答案 0 :(得分:0)
尝试使用@Provider标记您的课程:
@Provider
public class PortalExceptionMapper implements ExceptionMapper<ConstraintViolationException> {...}