Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java SpringBoot DTO验证_Java_Spring_Hibernate_Spring Boot - Fatal编程技术网

Java SpringBoot DTO验证

Java SpringBoot DTO验证,java,spring,hibernate,spring-boot,Java,Spring,Hibernate,Spring Boot,我是SpringBoot新手,我正在尝试向我的DTO类添加验证,如下所示 import javax.validation.constraints.NotBlank; @Getter @Setter public class EmployeeDto { private Long id; @NotBlank(message = "Employee first name is required") private String firstName; private

我是SpringBoot新手,我正在尝试向我的DTO类添加验证,如下所示

import javax.validation.constraints.NotBlank;

@Getter
@Setter
public class EmployeeDto {
    private Long id;

    @NotBlank(message = "Employee first name is required")
    private String firstName;

    private String lastName;

    @NotBlank(message = "EmployeeNUM  is required")
    private String employeeNum;

}
下面是我用来保存员工的REST端点

import javax.validation.Valid;
 @PostMapping("/employee")
    public ResponseEntity<?> addEmployee(@Valid @RequestBody EmployeeDto employeeDto) throws ClassNotFoundException {
        return   ResponseEntity.ok(employeeService.saveEmployee(deptId,employeeDto));

    }
import javax.validation.Valid;
@后映射(“/员工”)
public ResponseEntity addEmployee(@Valid@RequestBody EmployeeDto EmployeeDto)引发ClassNotFoundException{
返回ResponseEntity.ok(employeeService.saveEmployee(deptId,employeeDto));
}
我创建了一个如下所示的验证类来验证DTO字段

@ControllerAdvice
@RestController
public class Validation {

    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public Map<String, String> handleValidationExceptions(
            MethodArgumentNotValidException ex) {
        Map<String, String> errors = new HashMap<>();
        ex.getBindingResult().getAllErrors().forEach((error) -> {
            String fieldName = ((FieldError) error).getField();
            String errorMessage = error.getDefaultMessage();
            errors.put(fieldName, errorMessage);
        });
        return errors;

    }
}
@ControllerAdvice
@RestController
公共类验证{
@ResponseStatus(HttpStatus.BAD_请求)
@ExceptionHandler(MethodArgumentNotValidException.class)
公共地图手册例外(
MethodArgumentNotValidEx){
映射错误=新建HashMap();
例如:getBindingResult().getAllErrors().forEach((错误)->{
字符串fieldName=((FieldError)error).getField();
字符串errorMessage=error.getDefaultMessage();
errors.put(字段名、errorMessage);
});
返回错误;
}
}
预期产量为

{ “名字”:“需要员工的名字”, “employeeNum”:“employeeNum是必需的” }

但当通过邮递员到达终点时,我只收到了400个错误请求。
我的代码有什么问题?如何修复并获得上面提到的预期输出?

我使用了下面的类,现在它工作正常

@Service
public class MapValidationErrorService {
    public ResponseEntity<?> MapValidationService(BindingResult result){

        if(result.hasErrors()){
            Map<String, String> errorMap = new HashMap<>();

            for(FieldError error: result.getFieldErrors()){
                errorMap.put(error.getField(), error.getDefaultMessage());
            }
            return new ResponseEntity<Map<String, String>>(errorMap, HttpStatus.BAD_REQUEST);
        }

        return null;

    }

}
@服务
公共类MapValidationErrorService{
公共响应属性映射验证服务(BindingResult){
if(result.hasErrors()){
Map errorMap=newhashmap();
for(FieldError错误:result.getFieldErrors()){
errorMap.put(error.getField(),error.getDefaultMessage());
}
返回新的响应属性(errorMap、HttpStatus.BAD_请求);
}
返回null;
}
}
内部控制器

 @Autowired
    private MapValidationErrorService mapValidationErrorService;

    @PostMapping("/employee/{deptId}")
    public ResponseEntity<?> addEmployee(@PathVariable(name = "deptId") String deptId,@Valid @RequestBody EmployeeDto employeeDto, BindingResult result) throws ClassNotFoundException {
        ResponseEntity<?> errorMap = mapValidationErrorService.MapValidationService(result);
        if(errorMap != null)return errorMap;
        return   ResponseEntity.ok(employeeService.saveEmployee(deptId,employeeDto));

    }
@Autowired
私有MapValidationErrorService MapValidationErrorService;
@后映射(“/employee/{deptId}”)
public ResponseEntity addEmployee(@PathVariable(name=“deptId”)字符串deptId,@Valid@RequestBody EmployeeDto EmployeeDto,BindingResult)引发ClassNotFoundException{
ResponseEntity errorMap=mapValidationErrorService.MapValidationService(结果);
如果(errorMap!=null)返回errorMap;
返回ResponseEntity.ok(employeeService.saveEmployee(deptId,employeeDto));
}

尝试扩展
ResponseEntityExceptionHandler
类,如下所示:


import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;

import javax.validation.ConstraintViolationException;
import java.time.Instant;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * * Handle all exceptions and java bean validation errors for all endpoints income data that use the @Valid annotation
 *
 * @author Ehab Qadah
 */
@ControllerAdvice
public class GeneralExceptionHandler extends ResponseEntityExceptionHandler {


    @Override
    protected ResponseEntity<Object> handleMethodArgumentNotValid(MethodArgumentNotValidException exception, HttpHeaders headers,
                                                                  HttpStatus status, WebRequest request) {
        List<String> validationErrors = exception.getBindingResult()
                .getFieldErrors()
                .stream()
                .map(error -> error.getField() + ": " + error.getDefaultMessage())
                .collect(Collectors.toList());
        return getExceptionResponseEntity(HttpStatus.BAD_REQUEST, request, validationErrors);
    }


    @ExceptionHandler({ConstraintViolationException.class})
    public ResponseEntity<Object> handleConstraintViolation(
            ConstraintViolationException exception, WebRequest request) {
        List<String> validationErrors = exception.getConstraintViolations().stream().
                map(violation -> violation.getPropertyPath() + ": " + violation.getMessage())
                .collect(Collectors.toList());
        return getExceptionResponseEntity(HttpStatus.BAD_REQUEST, request, validationErrors);
    }

    private ResponseEntity<Object> getExceptionResponseEntity(final HttpStatus status, WebRequest request, List<String> errors) {
        final Map<String, Object> body = new LinkedHashMap<>();
        final String errorsMessage = CollectionUtils.isNotEmpty(errors) ? errors.stream().filter(StringUtils::isNotEmpty).collect(Collectors.joining(",")):status.getReasonPhrase();
        final String path = request.getDescription(false);
        body.put("TIMESTAMP", Instant.now());
        body.put("STATUS", status.value());
        body.put("ERRORS", errorsMessage);
        body.put("PATH", path);
        body.put("MESSAGE", status.getReasonPhrase());
        return new ResponseEntity<>(body, status);
    }
}


导入org.apache.commons.collections4.CollectionUtils;
导入org.apache.commons.lang3.StringUtils;
导入org.springframework.http.HttpHeaders;
导入org.springframework.http.HttpStatus;
导入org.springframework.http.ResponseEntity;
导入org.springframework.web.bind.MethodArgumentNotValidException;
导入org.springframework.web.bind.annotation.ControllerAdvice;
导入org.springframework.web.bind.annotation.ExceptionHandler;
导入org.springframework.web.context.request.WebRequest;
导入org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
导入javax.validation.ConstraintViolationException;
导入java.time.Instant;
导入java.util.LinkedHashMap;
导入java.util.List;
导入java.util.Map;
导入java.util.stream.collector;
/**
**处理使用@Valid注释的所有端点收入数据的所有异常和java bean验证错误
*
*@作者Ehab Qadah
*/
@控制器建议
公共类GeneralExceptionHandler扩展了ResponseEntityExceptionHandler{
@凌驾
受保护的ResponseEntity handleMethodArgumentNotValid异常(MethodArgumentNotValidException异常,HttpHeaders标头,
HttpStatus状态,WebRequest请求){
List validationErrors=exception.getBindingResult()
.getFieldErrors()
.stream()
.map(错误->错误.getField()+”:“+错误.getDefaultMessage())
.collect(Collectors.toList());
返回getExceptionResponseEntity(HttpStatus.BAD_请求、请求、验证错误);
}
@ExceptionHandler({ConstraintViolationException.class})
公共责任处理培训暴力(
ConstraintViolationException异常,WebRequest请求){
List validationErrors=exception.getConstraintViolations().stream()。
映射(违例->违例.getPropertyPath()+”:“+违例.getMessage())
.collect(Collectors.toList());
返回getExceptionResponseEntity(HttpStatus.BAD_请求、请求、验证错误);
}
private ResponseEntity getExceptionResponseEntity(最终HttpStatus状态、WebRequest请求、列表错误){
最终映射体=新LinkedHashMap();
最终字符串errorsMessage=CollectionUtils.isNotEmpty(错误)?errors.stream().filter(StringUtils::isNotEmpty)。collect(Collectors.joining(“,”):status.GetReasonPhase();
最终字符串路径=request.getDescription(false);
body.put(“TIMESTAMP”,Instant.now());
body.put(“STATUS”,STATUS.value());
body.put(“错误”,errorsMessage);
主体。放置(“路径”,路径);
body.put(“MESSAGE”,status.getReasonPhrase());
返回新的响应(主体、状态);
}
}

复制了@Ehab Qadah的答案

唯一的修改是在字符串映射(而不是字符串)中提供带有原因的错误字段

    @ExceptionHandler({ConstraintViolationException.class})
public ResponseEntity<Object> handleConstraintViolation(
        ConstraintViolationException exception, WebRequest request) {
        
    Map<String, String> fieldErrors = ex.getConstraintViolations().stream()
            .collect(Collectors.toMap(cv -> cv.getPropertyPath().toString(), ConstraintViolation::getMessage));
        
    return getExceptionResponseEntity(HttpStatus.BAD_REQUEST, request, fieldErrors);
}

private ResponseEntity<Object> getExceptionResponseEntity(final HttpStatus status, WebRequest request, List<String> errors) {
    final Map<String, Object> body = new LinkedHashMap<>();
    final String errorsMessage = CollectionUtils.isNotEmpty(errors) ? errors.stream().filter(StringUtils::isNotEmpty).collect(Collectors.joining(",")):status.getReasonPhrase();
    final String path = request.getDescription(false);
    body.put("TIMESTAMP", Instant.now());
    body.put("STATUS", status.value());
    body.put("ERRORS", errorsMessage);
    body.put("PATH", path);
    body.put("MESSAGE", status.getReasonPhrase());
    return new ResponseEntity<>(body, status);
}   
@ExceptionHandler({ConstraintViolationException.class})
公共责任处理培训暴力(
ConstraintViolationException异常,WebRequest请求){
Map fieldErrors=ex.getConstraintViolations().stream()
.collect(Collectors.toMap(cv->cv.getPropertyPath().toString(),ConstraintViolation::getMessage));
返回getExceptionResponseEntity(HttpStatus.BAD_请求,请求,fi