@ResponseStatus
- Controller 클래스의 메서드에 선언하여 사용하는 어노테이션으로, HttpStatus를 표현하는 방식 중 하나
@GetMapping("/api/problems")
@ResponseStatus(HttpStatus.OK)
public Long addProblem(@RequestParam @Valid ProblemRequestDto requestDto) {
return problemService.addProblem(requestDto);
}
ResponseEntity
- HTTP Request, Response를 나타내기 위해 사용하는 HttpEntity를 상속받은 클래스.
- HttpHeader, HttpBody를 포함한다.
@GetMapping("/api/problems")
public ResponseEntity<?> addProblem(@RequestParam @Valid ProblemRequestDto requestDto) {
Long problemId = problemService.addProblem(requestDto);
return new ResponseEntity<>(problemId,HttpStatus.OK);
}
차이점
- @ResponseStatus의 경우 어노테이션 하나만으로 HttpStatus를 적용할 수 있어 코드가 짧고, 심플하게 작동할 수 있다.
- 단점으로는 디테일하게 커스터마이징 하는 것이 어렵다.
- ResponseEntity는 그에 반해 헤더를 커스터마이징을 할 수 있으며, 유연하게 동작할 수 있다.
- 코드의 복잡성이 증가한다.
'🍃Spring' 카테고리의 다른 글
| [Spring] Business Exception 처리하기 (0) | 2023.02.21 |
|---|---|
| [Spring Boot] 스프링 부트 3 이상에서 Springdocs swagger 적용하기 (0) | 2023.02.19 |
| [Spring Data JPA] 페이징 (0) | 2023.02.16 |
| [Spring] MapStruct 사용시 매핑이 제대로 안될 경우 (0) | 2023.02.14 |
| [Spring] 트랜잭션 전파 - REQUIRES_NEW 활용하기 (0) | 2023.02.05 |