Commit 0b91886f authored by Syed Javed Ali's avatar Syed Javed Ali

Added javax validation to end points

parent 879181ba
...@@ -29,6 +29,9 @@ dependencies { ...@@ -29,6 +29,9 @@ dependencies {
implementation 'org.mapstruct:mapstruct:1.4.2.Final' implementation 'org.mapstruct:mapstruct:1.4.2.Final'
annotationProcessor 'org.mapstruct:mapstruct-processor:1.4.2.Final' annotationProcessor 'org.mapstruct:mapstruct-processor:1.4.2.Final'
implementation group: 'javax.validation', name: 'validation-api', version: '2.0.1.Final'
} }
test { test {
......
...@@ -7,15 +7,19 @@ import lombok.extern.slf4j.Slf4j; ...@@ -7,15 +7,19 @@ import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux; import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono; import reactor.core.publisher.Mono;
import javax.validation.Valid;
/** /**
* Controller class having http POST, GET, UPDATE and DELETE methods * Controller class having http POST, GET, UPDATE and DELETE methods
*/ */
@Slf4j @Slf4j
@RestController @RestController
@Validated
public class EmployeeController { public class EmployeeController {
@Autowired @Autowired
...@@ -31,7 +35,7 @@ public class EmployeeController { ...@@ -31,7 +35,7 @@ public class EmployeeController {
*/ */
@PostMapping(path = "/create") @PostMapping(path = "/create")
@ResponseStatus(HttpStatus.CREATED) @ResponseStatus(HttpStatus.CREATED)
public Mono<Employee> create(@RequestBody Employee employee) { public Mono<Employee> create(@Valid @RequestBody Employee employee) {
log.info("Calling service insert method..."); log.info("Calling service insert method...");
return employeeService.insert(employeeMapper.toDto(employee)) return employeeService.insert(employeeMapper.toDto(employee))
......
...@@ -4,6 +4,9 @@ import lombok.AllArgsConstructor; ...@@ -4,6 +4,9 @@ import lombok.AllArgsConstructor;
import lombok.Data; import lombok.Data;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
import javax.validation.constraints.DecimalMax;
import javax.validation.constraints.NotNull;
/** /**
* Model class for Employee * Model class for Employee
...@@ -13,9 +16,12 @@ import lombok.NoArgsConstructor; ...@@ -13,9 +16,12 @@ import lombok.NoArgsConstructor;
@NoArgsConstructor @NoArgsConstructor
@AllArgsConstructor @AllArgsConstructor
public class Employee { public class Employee {
private Integer id; private Integer id;
@NotNull(message = "Name cannot be null")
private String name; private String name;
@DecimalMax(value = "1000",message = "Value cannot be more than 1000")
private Long salary; private Long salary;
......
server.port=8081
spring.data.mongodb.database=testdb spring.data.mongodb.database=testdb
spring.data.mongodb.port=27017 spring.data.mongodb.port=27017
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment