有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何解析此消息!关于异常消息

message=“”

Validation failed for classes [com.PointsSystem.entity.Membership] during persist time for groups [javax.validation.groups.Default, ]\nList of constraint violations:[\n\tConstraintViolationImpl{interpolatedMessage='请输入正确的手机号(11位)', propertyPath=cellPhone, rootBeanClass=class com.PointsSystem.entity.Membership, messageTemplate='请输入正确的手机号(11位)'}\n\tConstraintViolationImpl{interpolatedMessage='lastName 不能为空', propertyPath=lastName, rootBeanClass=class com.PointsSystem.entity.Membership, messageTemplate='lastName 不能为空'}\n]

“”

我不想在我的web应用程序上打印此消息,我需要获取包含中文字符的关键字“interpolatedMessage”。我该怎么做?此消息是在我使用e.getMessage时获取的,e.getMessage是一个异常


共 (1) 个答案

  1. # 1 楼答案

    您必须捕获或编写ConstraintViolationException的处理程序

    @ExceptionHandler(ConstraintViolationException.class)
        public ResponseEntity<ApiResponse> handleConstraintViolation(final ConstraintViolationException exception,
                final WebRequest request) {
            final List<String> errors = exception.getConstraintViolations()
                    .parallelStream()
                    .map(violation -> violation
                          .getInvalidValue() + " : " + violation.getMessage())
                    .collect(Collectors.toList());
            return new ResponseEntity<>(errors, HttpStatus.BAD_REQUEST);
    
        }
    

    或者在抓块里

    try {
    } catch() {
       final List<String> errors = exception.getConstraintViolations()
                        .parallelStream()
                        .map(violation -> violation
                              .getInvalidValue() + " : " + violation.getMessage())
                        .collect(Collectors.toList());
    }