Group Validation

This commit is contained in:
uPagge 2021-03-22 20:06:05 +03:00
parent 9799bd7e5c
commit 51f315189a
No known key found for this signature in database
GPG Key ID: 964B40928E4C9088
3 changed files with 47 additions and 1 deletions

View File

@ -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<String> create(@RequestBody @Valid PersonDto personDto) {
return ResponseEntity.ok("valid");
}
@PutMapping
@Validated(Marker.OnUpdate.class)
public ResponseEntity<String> update(@RequestBody @Valid PersonDto personDto) {
return ResponseEntity.ok("valid");
}
}

View File

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

View File

@ -0,0 +1,9 @@
package org.sadtech.example.springvalidation.valid;
public interface Marker {
interface OnCreate {}
interface OnUpdate {}
}