Bean Validation(使用Hibernate-Validator校验基本数据类型)

1,在controller上加Validated

@Validated
public class UserBillController

2,在方法参数上加注解

public Object getUnSettled(@NotNull(message = "用户ID不能为空") Long userId) 

3,处理异常

if (e instanceof ConstraintViolationException) {
            ConstraintViolationException eNew = (ConstraintViolationException) e;
            if (!eNew.getConstraintViolations().isEmpty()) {
                return ResponseUtil.getNotNormalMap(
                        eNew.getConstraintViolations()
                                .toArray(new ConstraintViolation[0])[0]
                                .getMessage()
                );
            }
        }

参考:https://www.ibm.com/developerworks/cn/java/j-lo-jsr303/index.html

猜你喜欢

转载自blog.csdn.net/u012661496/article/details/82588277
今日推荐