放置,发布和删除验证之前的Spring Boot

问题描述 投票:0回答:1

我在春季启动时在控制器中创建了Post,Put和Delete Request。我在模型中添加了验证,还在控制器的方法中添加了@Valid参数。我想为验证发布,放置和删除操作添加什么其他内容?

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;
}

我的发帖方法是:

@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);
    }

我的Put方法是:

@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}")
    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);
    }
java spring-boot validation post put
1个回答
0
投票

您的具体问题是什么?

请参考以下资料以进一步阅读。

Validation in Spring Boot

© www.soinside.com 2019 - 2024. All rights reserved.