waveofmymind
기록하는 습관
waveofmymind
전체 방문자
오늘
어제
  • 분류 전체보기 (124)
    • 📝 정리 (5)
    • 🌊TIL (9)
    • 💻CS (1)
      • 자료구조 (1)
    • 📙Language (9)
      • ☕Java (6)
      • 🤖Kotlin (3)
    • 🍃Spring (28)
    • 👨🏻‍💻알고리즘 (67)
      • 프로그래머스 (59)
      • 백준 (3)
    • 👷DevOps (4)
      • 🐳Docker (2)
      • 🤵Jenkins (1)

블로그 메뉴

  • 홈
  • Spring
  • Java
  • 알고리즘

공지사항

인기 글

태그

  • JDBC
  • Open AI
  • CORS
  • chat GPT
  • 챗GPT
  • spring boot
  • kotest
  • 완전탐색
  • spring
  • Spring Security
  • Connection
  • 통합테스트
  • SpringAOP
  • 스프링
  • 트랜잭션 전파
  • 코틀린
  • 다이나믹 프로그래밍
  • sql
  • 스프링 시큐리티
  • 트랜잭션
  • BFS
  • kotlin
  • LeetCode
  • 스택
  • resultset
  • AOP
  • til
  • mybatis
  • 힙
  • 스프링 부트

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
waveofmymind

기록하는 습관

🍃Spring

[Spring Boot] @ResponseStatus와 ResponseEntity

2023. 2. 18. 22:10

@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
    '🍃Spring' 카테고리의 다른 글
    • [Spring] Business Exception 처리하기
    • [Spring Boot] 스프링 부트 3 이상에서 Springdocs swagger 적용하기
    • [Spring Data JPA] 페이징
    • [Spring] MapStruct 사용시 매핑이 제대로 안될 경우
    waveofmymind
    waveofmymind

    티스토리툴바