@Valid + BindingResult 拦截接口错误信息

@Valid + BindingResult 拦截接口错误信息###测试发现: HttpServletRequest request, HttpServletResponse response,需要去掉,否则无法输出message错误

@Valid + BindingResult 拦截接口错误信息

@Valid + BindingResult 拦截接口错误信息 ###测试发现: HttpServletRequest request, HttpServletResponse response, 需要去掉,否则无法输出message错误消息 ###测试发现 @Validated @Valid 这两个注解方法都可以。

代码语言:javascript代码运行次数:0运行复制
public ResponseData testMvc(@Valid @RequestBody ReqVo reqVo,BindingResult bindingResult) {
logger.info("testMvc controller params={}",GsonUtils.toJson(reqVo));

if (bindingResult.hasErrors()) {
Optional<FieldError> error = bindingResult.getFieldErrors().stream().findFirst();
throw new InputErrorException(error.isPresent()?error.get().getDefaultMessage() : "参数输入错误");
}
}
代码语言:javascript代码运行次数:0运行复制
ReqVo.java

@NotBlank(message = "订单号不能为空") private String orderNo;

@Range(min = 1,max = 4,message = "取值范围,1-4") private int type;

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。 原始发表:2022-05-09,如有侵权请联系 cloudcommunity@tencent 删除errormessage测试接口注解

发布者:admin,转转请注明出处:http://www.yc00.com/web/1754987496a5224352.html

相关推荐

  • @Valid + BindingResult 拦截接口错误信息

    @Valid + BindingResult 拦截接口错误信息###测试发现: HttpServletRequest request, HttpServletResponse response,需要去掉,否则无法输出message错误

    1月前
    220

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信