Spring Boot | Spring MVC의 간단한 사용법 | 응답 상태 코드(@ResponseStatus)값 지정하기
코드 작성
src/main/java/sample/springboot/web/HelloController.java
package sample.springboot.web;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/hello")
public class HelloController {
@RequestMapping(method=RequestMethod.GET)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public void getMethod() {
}
}
실행 결과
curl으로 테스트하기
$ curl http://localhost:8080/hello -v
( 생략 )
< HTTP/1.1 400 Bad Request
* Server Apache-Coyote/1.1 is not blacklisted
< Server: Apache-Coyote/1.1
< Content-Length: 0
< Date: Wed, 29 Apr 2015 11:50:08 GMT
< Connection: close
( 생략 )
설명
- 메소드를 @ResponseStatus에 어노테이션을 부여하고 value에 상태 코드를 지정하면, 그 응답의 상태 코드를 지정할 수 있다.
- 아무것도 지정하지 않으면 200 OK가 반환된다.