diff --git a/src/main/java/org/sadtech/example/springvalidation/controller/PersonControllerGroupValid.java b/src/main/java/org/sadtech/example/springvalidation/controller/PersonControllerGroupValid.java new file mode 100644 index 0000000..12b2256 --- /dev/null +++ b/src/main/java/org/sadtech/example/springvalidation/controller/PersonControllerGroupValid.java @@ -0,0 +1,33 @@ +package org.sadtech.example.springvalidation.controller; + +import org.sadtech.example.springvalidation.dto.PersonDto; +import org.sadtech.example.springvalidation.valid.Marker; +import org.springframework.http.ResponseEntity; +import org.springframework.validation.annotation.Validated; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.PutMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import javax.validation.Valid; +import javax.validation.groups.Default; + +@Validated +@RestController +@RequestMapping("/api/group-valid/person") +public class PersonControllerGroupValid { + + @PostMapping + @Validated({Marker.OnCreate.class}) + public ResponseEntity create(@RequestBody @Valid PersonDto personDto) { + return ResponseEntity.ok("valid"); + } + + @PutMapping + @Validated(Marker.OnUpdate.class) + public ResponseEntity update(@RequestBody @Valid PersonDto personDto) { + return ResponseEntity.ok("valid"); + } + +} diff --git a/src/main/java/org/sadtech/example/springvalidation/dto/PersonDto.java b/src/main/java/org/sadtech/example/springvalidation/dto/PersonDto.java index b3534e6..5a9d5cd 100644 --- a/src/main/java/org/sadtech/example/springvalidation/dto/PersonDto.java +++ b/src/main/java/org/sadtech/example/springvalidation/dto/PersonDto.java @@ -3,6 +3,7 @@ package org.sadtech.example.springvalidation.dto; import lombok.Getter; import lombok.Setter; import org.sadtech.example.springvalidation.valid.CapitalLetter; +import org.sadtech.example.springvalidation.valid.Marker; import javax.persistence.Column; import javax.persistence.Entity; @@ -10,10 +11,11 @@ import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Table; -import javax.persistence.Transient; import javax.validation.constraints.Max; import javax.validation.constraints.Min; import javax.validation.constraints.NotBlank; +import javax.validation.constraints.NotNull; +import javax.validation.constraints.Null; import javax.validation.constraints.Pattern; @Entity @@ -23,6 +25,8 @@ import javax.validation.constraints.Pattern; public class PersonDto { @Id + @Null(groups = Marker.OnCreate.class) + @NotNull(groups = Marker.OnUpdate.class) @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; diff --git a/src/main/java/org/sadtech/example/springvalidation/valid/Marker.java b/src/main/java/org/sadtech/example/springvalidation/valid/Marker.java new file mode 100644 index 0000000..2c5c2ac --- /dev/null +++ b/src/main/java/org/sadtech/example/springvalidation/valid/Marker.java @@ -0,0 +1,9 @@ +package org.sadtech.example.springvalidation.valid; + +public interface Marker { + + interface OnCreate {} + + interface OnUpdate {} + +}