controller - res(ResponseEntity, HttpStatus, @ResponseStatus)

apayaya·2022년 5월 19일
0

Spring Boot - WebMvc

목록 보기
3/3

HttpStatus

  • enum 타입
  • HTTP 응답 상태를 정의
  • 1XX
    • CONTINUE(100)
    • SWITCHING_PROTOCOLS(101)
    • PROCESSING(102)
    • CHECKPOINT(103)
  • 2XX
    • OK(200)
    • CREATED(201)
    • ACCEPTED(202)
    • NON_AUTHORITATIVE_INFORMATION(203)
  • 4XX
    • BAD_REQUEST(400)
    • UNAUTHORIZED(401)
    • FORBIDDEN(403)
    • NOT_FOUND(404)
  • 5XX
    • INTERNAL_SERVER_ERROR(500)

@ResponseStatus

  • 응답상태 지정

Properties

  • value: HttpStatus
@ResponseStatus(value= HttpStatus.CREATED)
@PostMapping("/created")
public String good(Info info){
	...
	return "Created";
}

ResponseEntity

  • HTTP요청에 대한 응답 객체
  • 응답 상태(HttpStatus)와 응답 헤더 지정가능
  • ResponseEntity<T> : T는 body의 타입

응답의 Body가 존재하는 ResponseEntity 객체 생성

  • 생성자
    • new ResponseEntity<T>(T body, HttpStatus) -> ResponseEntity<T>
    • new ResponseEntity<T>(T body, HttpHeaders, HttpStatus) -> ResponseEntity<T>
  • Builder
    • ResponseEntity.status(HttpStatus).headers(HttpHeaders).body(T body) -> ResponseEntity<T>
  • status 지정
    • ResponseEntity.ok(T body) -> ResponseEntity<T>
    • ResponseEntity.ok().headers(headers).build() -> ResponseEntity<T>
    • ResponseEntity.badRequest().headers(headers).body(body) -> ResponseEntity<T>
    • ResponseEntity.notFound().build() -> ResponseEntity<T>

methods

  • .getStatusCode() -> HttpStatus
  • .getStatusCodeValue() -> int
  • .getHeaders() -> HttpHeaders
  • .getBody() -> T

profile
java 정리

0개의 댓글