1
我想創建自己的自定義驗證錯誤消息。我搜索了這麼多的論壇,但我不知道我哪裏錯了。MyCustomExceptionMapper未調用 - 自定義驗證錯誤消息 - 澤西島驗證
@Provider
public class ConstraintViolationExceptionMapper implements ExceptionMapper<ValidationException>{
private String field;
private String code;
private String message;
public ConstraintViolationExceptionMapper(){
}
@Override
public Response toResponse(ValidationException e) {
message = e.getMessage();
InvalidRespose invalidRespose = new InvalidRespose(code, message, field);
return Response.status(400).entity(invalidRespose).type(MediaType.APPLICATION_JSON).build();
}
}
資源類
@POST
@Path("/increment")
@Consumes(MediaType.APPLICATION_JSON)
@Timed
public Response greetings(@Valid @NotNull @Pattern(regexp = "^[1-9]\\d*$", message = "Should be a positive Number") @PathParam("id") String account_id){
...
...
}
我已經註冊了我的ExceptionMapper在我Applicationclass作爲
environment.jersey().register(new ConstraintViolationExceptionMapper());
我不知道我要去的地方錯了。