엔티티 유효성 검사(null이나 사용자가 데이터를 JSON에 포함하지 않고 보낼 때)
- 도메인 지식
- 1번.생성자를 통해 유효성 검사하는 방법과 2번.Bean Validation을 통한 유효성 검사방법이 있다.
1번. 생성자를 통해 유효성 검사하는 방법
package kr.co.hanbit.product.management.domain;
import jakarta.validation.constraints.Max;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.Size;
import java.util.Objects;
public class Product {
private Long id;
private String name;
private Integer price;
private Integer amount;
public Product(Long id, String name, Integer price, Integer amount){
this.id=id;
if(name.lenght() >100 || name.lenght()<1){
throw new IllegalArgumentException("Name must be between 1 and 100 characters long");
}else {this.name=name;}
if(price>1000000 || price<0){
throw new IllegalArgumentException("Price must be between 0 and 1,000,000");
} else {this.price=price;}
if(amount>9999 || amount<0){
throw new IllegalArgumentException("Amount must be between 0 and 9999");
} else {this.amount=amount;}
}
public void setId(Long id) {
this.id = id;
}
public Boolean sameId(Long id) {
return this.id.equals(id);
}
public Boolean containsName(String name) {
return this.name.contains(name);
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Product product = (Product) o;
return Objects.equals(id, product.id);
}
}
2번. .Bean Validation 을 통한 유효성 검사방법
pom.xml에 dependency 추가
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
product.java에 제약사항 적기
package kr.co.hanbit.product.management.domain;
import jakarta.validation.constraints.Max;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.Size;
import java.util.Objects;
public class Product {
private Long id;
@Size(min = 1, max = 100)
private String name;
@Max(1_000_000)
@Min(0)
private Integer price;
@Max(9_999)
@Min(0)
private Integer amount;
public void setId(Long id) {
this.id = id;
}
public Boolean sameId(Long id) {
return this.id.equals(id);
}
public Boolean containsName(String name) {
return this.name.contains(name);
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Product product = (Product) o;
return Objects.equals(id, product.id);
}
}
ValidationService 생성
package kr.co.hanbit.product.management.application;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
import jakarta.validation.Valid;
@Service
@Validated
public class ValidationService {
public <T> void checkValid(@Valid T validationTarget) {
// do nothing
}
}
서비스에 ValidationService DI 추가후 add메서드안에서 엔티티 추가 전 유효성 검사 실시
package kr.co.hanbit.product.management.application;
import kr.co.hanbit.product.management.domain.Product;
import kr.co.hanbit.product.management.infrastructure.ListProductRepository;
import kr.co.hanbit.product.management.presentation.ProductDto;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class SimpleProductService {
private ListProductRepository listProductRepository;
private ModelMapper modelMapper;
private ValidationService validationService;
@Autowired
SimpleProductService(ListProductRepository listProductRepository, ModelMapper modelMapper, ValidationService validationService
) {
this.listProductRepository = listProductRepository;
this.modelMapper = modelMapper;
this.validationService = validationService; //DI
}
public ProductDto add(ProductDto productDto) {
Product product = modelMapper.map(productDto, Product.class);
validationService.checkValid(product); //엔티티 유효성 검사 실시
Product savedProduct = listProductRepository.add(product);
ProductDto savedProductDto = modelMapper.map(savedProduct, ProductDto.class);
return savedProductDto;
}
public ProductDto findById(Long id) {
Product product = listProductRepository.findById(id);
ProductDto productDto = modelMapper.map(product, ProductDto.class);
return productDto;
}
public List<ProductDto> findAll() {
List<Product> products = listProductRepository.findAll();
List<ProductDto> productDtos = products.stream()
.map(product -> modelMapper.map(product, ProductDto.class))
.toList();
return productDtos;
}
public List<ProductDto> findByNameContaining(String name) {
List<Product> products = listProductRepository.findByNameContaining(name);
List<ProductDto> productDtos = products.stream()
.map(product -> modelMapper.map(product, ProductDto.class))
.toList();
return productDtos;
}
public ProductDto update(ProductDto productDto) {
Product product = modelMapper.map(productDto, Product.class);
Product updatedProduct = listProductRepository.update(product);
ProductDto updatedProductDto = modelMapper.map(updatedProduct, ProductDto.class);
return updatedProductDto;
}
public void delete(Long id) {
listProductRepository.delete(id);
}
}
컨트롤러 유효성 검사(DTO데이터 존재하는지랑 null체크)
ProductDto.java
package kr.co.hanbit.product.management.presentation;
import jakarta.validation.constraints.NotNull;
public class ProductDto {
private Long id;
@NotNull
private String name;
@NotNull
private Integer price;
@NotNull
private Integer amount;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public Integer getPrice() {
return price;
}
public Integer getAmount() {
return amount;
}
}
@NotNull: 오직 null만 허용하지 않는다. ""처럼 빈 문자열이나 " "처럼 띄어쓰기만 있는 문자열은 허용된다.
@NotEmpty: null과 ""처럼 빈 문자열이 허용되지 않는다. " " 이것만 허용
@NotBlank: null, "", " " 전부 허용하지 않는다.
ProductController.java의 추가하는 API에 @Valid 붙이기
package kr.co.hanbit.product.management.presentation;
import kr.co.hanbit.product.management.application.SimpleProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import jakarta.validation.Valid;
import java.util.List;
@RestController
public class ProductController {
private SimpleProductService simpleProductService;
@Autowired
ProductController(SimpleProductService simpleProductService) {
this.simpleProductService = simpleProductService;
}
@RequestMapping(value = "/products", method = RequestMethod.POST)
public ProductDto createProduct(@Valid @RequestBody ProductDto productDto) {
return simpleProductService.add(productDto);
}
@RequestMapping(value = "/products/{id}", method = RequestMethod.GET)
public ProductDto findProductById(@PathVariable Long id) {
return simpleProductService.findById(id);
}
@RequestMapping(value = "/products", method = RequestMethod.GET)
public List<ProductDto> findProducts(
@RequestParam(required = false) String name
) {
if (null == name)
return simpleProductService.findAll();
return simpleProductService.findByNameContaining(name);
}
@RequestMapping(value = "/products/{id}", method = RequestMethod.PUT)
public ProductDto updateProduct(
@PathVariable Long id,
@RequestBody ProductDto productDto
) {
productDto.setId(id);
return simpleProductService.update(productDto);
}
@RequestMapping(value = "/products/{id}", method = RequestMethod.DELETE)
public void deleteProduct(@PathVariable Long id) {
simpleProductService.delete(id);
}
}
엔티티 도메인 객체 유효성 검사 실패시 -> ConstrainViolationException 예외 발생
컨트롤러 유효성 검사 실패시 -> MethodArgumentNotValidException 예외 발생
ConstrainViolationException (도메인) 예외 처리
MethodArgumentNotValidException (DTO) 예외 처리
GlobalExceptionHandler.java 에 추가 (500에러를 400에러로 변경후 에러 메세지 담기)
package kr.co.hanbit.product.management.presentation;
import kr.co.hanbit.product.management.domain.EntityNotFoundException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import jakarta.validation.ConstraintViolation;
import jakarta.validation.ConstraintViolationException;
import jakarta.validation.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
@RestControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(ConstraintViolationException.class)
public ResponseEntity<ErrorMessage> handleConstraintViolatedException(
ConstraintViolationException ex
) {
Set<ConstraintViolation<?>> constraintViolations = ex.getConstraintViolations();
List<String> errors = constraintViolations.stream()
.map(
constraintViolation ->
extractField(constraintViolation.getPropertyPath()) + ", " + constraintViolation.getMessage()
)
.toList();
ErrorMessage errorMessage = new ErrorMessage(errors);
return new ResponseEntity(errorMessage, HttpStatus.BAD_REQUEST);
}
@ExceptionHandler(MethodArgumentNotValidException.class)
public ResponseEntity<ErrorMessage> handleMethodArgumentNotValidException(
MethodArgumentNotValidException ex
) {
List<FieldError> fieldErrors = ex.getBindingResult().getFieldErrors();
List<String> errors = fieldErrors.stream()
.map(
fieldError ->
fieldError.getField() + ", " + fieldError.getDefaultMessage()
)
.toList();
ErrorMessage errorMessage = new ErrorMessage(errors);
return new ResponseEntity(errorMessage, HttpStatus.BAD_REQUEST);
}
@ExceptionHandler(EntityNotFoundException.class)
public ResponseEntity<ErrorMessage> handleEntityNotFoundExceptionException(
EntityNotFoundException ex
) {
List<String> errors = new ArrayList<>();
errors.add(ex.getMessage());
ErrorMessage errorMessage = new ErrorMessage(errors);
return new ResponseEntity<>(errorMessage, HttpStatus.NOT_FOUND);
}
@ExceptionHandler(IllegalArgumentException.class)
public ResponseEntity<ErrorMessage> handleIllegalArgumentException(
IllegalArgumentException ex
) {
List<String> errors = new ArrayList<>();
errors.add(ex.getMessage());
ErrorMessage errorMessage = new ErrorMessage(errors);
return new ResponseEntity<>(errorMessage, HttpStatus.BAD_REQUEST);
}
private String extractField(Path path) {
String[] splittedArray = path.toString().split("[.]");
int lastIndex = splittedArray.length - 1;
return splittedArray[lastIndex];
}
}
좀더 정확한 에러로 표시하기 위해 ErrorMessage.java 추가
package kr.co.hanbit.product.management.presentation;
import java.util.List;
public class ErrorMessage {
private List<String> errors;
public ErrorMessage(List<String> errors) {
this.errors = errors;
}
public List<String> getErrors() {
return errors;
}
}
ErrorMessage.java 사용하지 않고 List<String> 형태로 반환해도 무관하지만, List<String> 형태로 반환하면 해당 JSON에 필드 이름 없이 다음과 같이 문자열 배열 형태로 전달되므로 어떤 정보인지 명확하게 파악되지 않는다. 어떤 필드가 유효성검사에 실패했는지 외에도 여러 가지 에러 메시지를 일관된 형태로 제공하고 싶다면 커스텀한 에러 응답 클래스를 만드는 것이 좋다.
List 형태로 반환:
{
"checkValidationTarget.amount, 9999 이하여야 합니다",
"checkValidationTarget.price, 1000000 이하여야 합니다"
}
ErrorMessage 로 반환:
{
"errors": [
"checkValidationTarget.amount, 9999 이하여야 합니다",
"checkValidationTarget.price, 1000000 이하여야 합니다"
]
}
repository에서 id로 엔티티 못찾으면 EntityNotFoundException 예외 발생함 이것도 처리
package kr.co.hanbit.product.management.domain;
public class EntityNotFoundException extends RuntimeException {
public EntityNotFoundException(String message) {
super(message);
}
}
'springboot' 카테고리의 다른 글
[springboot] Spring Security의 /login 요청 시 처리 과정(세션 저장) (5) | 2024.10.09 |
---|---|
[springboot] 스프링부트 2점대에 swagger 3점대 연결 (0) | 2024.04.14 |
[springboot] 엔티티 int VS Integer 타입 차이 (0) | 2024.04.06 |
[springboot] HTTP 상태 코드 및 리다이렉트 (0) | 2024.04.06 |
[springboot] HTTP 전송 데이터의 형식 JSON, 직렬화 역직렬화 (1) | 2024.04.06 |