Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/395.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 如何在Spring Boot中对POST、PUT和DELETE执行不同的验证 我正在尝试验证员工请求,对post方法、put方法和delete方法的验证应该不同 公营雇员{ @NotNull(message=“员工Id不能为空”) 私有整数id; @最小值(值=2000,消息=“薪资不能低于2000”) @最大值(值=50000,消息=“薪资不能大于50000”) 私人整数工资; @NotNull(message=“名称不能为空”) 专用字符串指定; } 对于post方法,您希望验证请求中存在的所有字段 @后映射(“/员工”) 公共响应添加员工(@Valid@RequestBody-Employee-newEmployee){ 员工emp=服务。新增员工(新员工); 如果(emp==null){ 返回ResponseEntity.noContent().build(); } 返回新的ResponseEntity(HttpStatus.CREATED); }_Java_Spring Boot_Validation - Fatal编程技术网

Java 如何在Spring Boot中对POST、PUT和DELETE执行不同的验证 我正在尝试验证员工请求,对post方法、put方法和delete方法的验证应该不同 公营雇员{ @NotNull(message=“员工Id不能为空”) 私有整数id; @最小值(值=2000,消息=“薪资不能低于2000”) @最大值(值=50000,消息=“薪资不能大于50000”) 私人整数工资; @NotNull(message=“名称不能为空”) 专用字符串指定; } 对于post方法,您希望验证请求中存在的所有字段 @后映射(“/员工”) 公共响应添加员工(@Valid@RequestBody-Employee-newEmployee){ 员工emp=服务。新增员工(新员工); 如果(emp==null){ 返回ResponseEntity.noContent().build(); } 返回新的ResponseEntity(HttpStatus.CREATED); }

Java 如何在Spring Boot中对POST、PUT和DELETE执行不同的验证 我正在尝试验证员工请求,对post方法、put方法和delete方法的验证应该不同 公营雇员{ @NotNull(message=“员工Id不能为空”) 私有整数id; @最小值(值=2000,消息=“薪资不能低于2000”) @最大值(值=50000,消息=“薪资不能大于50000”) 私人整数工资; @NotNull(message=“名称不能为空”) 专用字符串指定; } 对于post方法,您希望验证请求中存在的所有字段 @后映射(“/员工”) 公共响应添加员工(@Valid@RequestBody-Employee-newEmployee){ 员工emp=服务。新增员工(新员工); 如果(emp==null){ 返回ResponseEntity.noContent().build(); } 返回新的ResponseEntity(HttpStatus.CREATED); },java,spring-boot,validation,Java,Spring Boot,Validation,对于我的put方法,我只想验证Salary字段,其余字段不会被验证 I am trying to validate Employee Request and the validations should be different for post method,put method and delete method public class Employee { @NotNull(message = "Employee Id can not be null") private

对于我的put方法,我只想验证Salary字段,其余字段不会被验证

I am trying to validate Employee Request and the validations should be different for post method,put method and delete method

public class Employee {
    @NotNull(message = "Employee Id can not be null")
    private Integer id;

    @Min(value = 2000, message = "Salary can not be less than 2000")
    @Max(value = 50000, message = "Salary can not be greater than 50000")
    private Integer salary;

    @NotNull(message = "designation can not be null")
    private String designation;
}

For post method want to validate all the fields present in the request


  @PostMapping("/employees")
        public ResponseEntity<Void> addEmployee(@Valid @RequestBody Employee newEmployee) {
            Employee emp= service.addEmployee(newEmployee);
            if (emp== null) {
                return ResponseEntity.noContent().build();
            }
            return new ResponseEntity<Void>(HttpStatus.CREATED);
        }
@PutMapping(“/employees/{id}”)
公共响应性更新员工(@Valid@RequestBody-Employee-updateEmployee){
emp=service.EmployeeById(updateEmployee.getId());
如果(null==emp){
返回新的ResponseEntity(未找到HttpStatus.NOT_);
}
emp.setSalary(updateEmployee.getSalary());
emp.setDesignation(updateEmployee.getDesignation());
服务更新员工(emp);
返回新的响应状态(emp、HttpStatus.OK);
}
对于删除,我不想执行任何验证

 @PutMapping("/employees/{id}")
        public ResponseEntity<Vehicle> updateEmployee(@Valid @RequestBody Employee updateEmployee) {
            Employee emp= service.EmployeeById(updateEmployee.getId());
            if (null == emp) {
                return new ResponseEntity<Employee>(HttpStatus.NOT_FOUND);
            }
            emp.setSalary(updateEmployee.getSalary());
            emp.setDesignation(updateEmployee.getDesignation());
            service.updateEmployee(emp);
            return new ResponseEntity<Employee>(emp, HttpStatus.OK);
        }
@DeleteMapping(“/employees/{id}”)
公共响应属性deleteEmployee(@Valid@PathVariable int-id){
emp=service.getEmployeeById(id);
if(null==员工){
返回新的响应状态(HttpStatus.FOUND);
}
服务。删除员工(id);
返回新的响应属性(HttpStatus.NO_内容);
}

但是如果我使用@Valid,所有的方法都会得到所有字段的验证。

实现这一点的一种方法是使用
org.springframework.validation
库中的
@Valid
注释,而不是在方法参数中使用
@Valid
注释

这样,您可以根据模型中的需求对约束进行分组(第一组用于POST方法,第二组用于PUT方法等)。在模型中,您需要使用
groups
属性并指定要绑定的组的名称

这里有一个详细的解释,并给出了使用它的示例代码:

@DeleteMapping("/employees/{id}")
public ResponseEntity<Employee> deleteEmployee(@Valid @PathVariable int id) {
    Employee emp = service.getEmployeeById(id);
    if (null == employee) {
        return new ResponseEntity<Employee>(HttpStatus.FOUND);
    }
    service.deleteEmployee(id);
    return new ResponseEntity<Employee>(HttpStatus.NO_CONTENT);
}