728x90
스프링 부트에서 유효성 검사를 해보려고 합니다.
Create, Update Post REST API 요청에 대해 유효성 검사
dependency 추가
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-validation -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
<version>2.7.0</version>
</dependency>
PostDto 에 애노테이션 추가
package com.springboot.blog.payload;
@Data
public class PostDto
{
private long id;
@NotEmpty
@Size(min =2, message="Post title should have at least 2 characters")
private String title;
@NotEmpty
@Size(min= 10, message="Post description should have at 10 characters")
private String description;
@NotEmpty
private String content;
private Set<CommentDto> comments;
}
@NotEmpty
The annotated element must not be null nor empty
String , Collection, Map, Array 에 적용
Controller에 적용
PostController
@PostMapping
public ResponseEntity<PostDto> createPost(@Valid @RequestBody PostDto postDto){
return new ResponseEntity<>(postService.createPost(postDto),HttpStatus.CREATED);
}
.
// update post by id rest api
@PutMapping("/{id}")
public ResponseEntity<PostDto> updatePost(@Valid @RequestBody PostDto postDto, @PathVariable(name="id") long id){
PostDto postResponse = postService.updatePost(postDto, id);
return new ResponseEntity<>(postResponse,HttpStatus.OK);
}
TEST
Customizing Validation Response
2022-07-03 08:14:25.582 WARN 8956 --- [nio-8080-exec-1] .m.m.a.ExceptionHandlerExceptionResolver : Resolved [org.springframework.web.bind.MethodArgumentNotValidException: Validation failed for argument [0] in public org.springframework.http.ResponseEntity<com.springboot.blog.payload.PostDto> com.springboot.blog.controller.PostController.createPost(com.springboot.blog.payload.PostDto): [Field error in object 'postDto' on field 'title': rejected value [g]; codes [Size.postDto.title,Size.title,Size.java.lang.String,Size]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [postDto.title,title]; arguments []; default message [title],2147483647,2]; default message [Post title should have at least 2 characters]] ]
MethodArgumentNotValidException 가 발생하는 것을 볼 수 있다.
GlobalExceptionHandler 에서 @ExceptionHandler 추가
GlobalExceptionHandler
//MethodArgumentNotValidException
@ExceptionHandler(MethodArgumentNotValidException .class)
public ResponseEntity<Object> handleMethodArgumentNotValidException(MethodArgumentNotValidException exception, WebRequest webRequest)
{
Map<String , String >errors= new HashMap<>();
exception.getBindingResult().getAllErrors().forEach((error)->{
String fieldName = ((FieldError) error).getField();
String message = error.getDefaultMessage();
errors.put(fieldName, message);
});
return new ResponseEntity<>(errors,HttpStatus.BAD_REQUEST);
}
TEST
MethodArgumentNotValidException에 걸린 부분만 설정한 에러 메시지가 담겨오는 걸 볼 수 있다.
마찬가지로 Comment 에도 추가
CommentDto
package com.springboot.blog.payload;
@Data
public class CommentDto
{
private long id;
@NotEmpty(message="Name should not be null or empty")
private String name;
@NotEmpty(message="Email should not be null or empty")
@Email
private String email;
@NotEmpty
@Size(min=10, message="Comment body must be mininum 10 characters")
private String body;
}
CommentController
@PostMapping("/posts/{postId}/comments")
public ResponseEntity<CommentDto> createComment(
@PathVariable long postId,
@Valid @RequestBody CommentDto commentDto){
return new ResponseEntity<>(commentService.createComment(postId, commentDto), HttpStatus.CREATED);
}
.
@PutMapping("/posts/{postId}/comments/{id}")
public ResponseEntity<CommentDto> updateComment(@PathVariable Long postId, @PathVariable(value="id") Long commentId,
@Valid @RequestBody CommentDto commentDto)
{
return new ResponseEntity<>(commentService.updateComment(postId, commentId, commentDto), HttpStatus.OK);
}
'Back-end > Spring Boot + REST API' 카테고리의 다른 글
Spring boot - blog application (REST API) : Spring Security + JWT (0) | 2022.08.02 |
---|---|
Spring boot - blog application (REST API) : Securing REST APIs (0) | 2022.07.20 |
Spring boot - blog application (REST API) : Global Exception Handling (0) | 2022.07.05 |
Spring boot - blog application (REST API) : ModelMapper (0) | 2022.07.04 |
Spring boot - blog application (REST API) : Comment (0) | 2022.07.03 |
댓글