简体   繁体   中英

Spring Boot before Put, Post and Delete Validation

I have created Post, Put and Delete Request in my controller in spring boot. I have added validations in my model and also added @Valid parameter in method on controller. I want to what else I am supposed to add for validation for Post, Put and Delete operation?

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

My Post Method is :

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

My Put Method is :

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

Delete Method

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

What is your specific problem?

Please refer to the following source for further reading.

Validation in Spring Boot

To your question about PUT - update does not work properly ? Although, code looks ok. But if you are using JPA, then please remember JPA has delay data writing to database mechanism meaning it does not write data to database right away. And if you want JPA to write/save your data right away then you will have to call respository.saveAndFlush() - to force the JPA to write all data in session.

So, instead of calling the repository.saveAndFlush() every time you save data, you can simply return the same request object in this case "updateEmployee" instead of "emp" object for updating record eg :

return new ResponseEntity(updateEmployee, HttpStatus.OK);

POST : You should not use "@NotNull(message = "Employee Id can not be null")" on private Integer id since you are using same object for both POST and PUT method because @Valid will validate all fields in class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM