gpt4 book ai didi

java - MyCustomExceptionMapper 未调用 - 自定义验证错误消息 - Jersey 验证

转载 作者:行者123 更新时间:2023-11-30 07:08:30 25 4
gpt4 key购买 nike

我想创建自己的自定义验证错误消息。我搜索了很多论坛,但我不确定我哪里出错了。

@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 注册为

environment.jersey().register(new ConstraintViolationExceptionMapper());

我不知道我哪里出了问题。

最佳答案

在注册我自己的映射器之前,我必须禁用 Dropwizard 的默认映射器。

((DefaultServerFactory)rateLimitConfiguration.getServerFactory()).setRegisterDefaultExceptionMappers(false);

引用链接here

关于java - MyCustomExceptionMapper 未调用 - 自定义验证错误消息 - Jersey 验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39619002/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com