상세 컨텐츠

본문 제목

Spring MVC @ResponseEntity

프레임워크/Spring Boot

by 최승호 2022. 5. 31. 12:00

본문

https://docs.spring.io/spring-framework/docs/current/reference/html/web.html#mvc-ann-responsebody

 

Web on Servlet Stack

Spring Web MVC is the original web framework built on the Servlet API and has been included in the Spring Framework from the very beginning. The formal name, “Spring Web MVC,” comes from the name of its source module (spring-webmvc), but it is more com

docs.spring.io

@ResponseEntity 어노테이션은

@ResponseBody와 비슷한데 HttpStatus와 헤더 정보를 같이 보낼 수 있다.

 

@ResponseEntity 예시

@RequestMapping("/resEntity")
@Controller
public class ResponseBodyController {

	@ResponseBody
	@GetMapping("/void")
	public ResponseEntity<Void> responseVoid() {
		return new ResponseEntity<Void>(HttpStatus.OK);
	}
	
	@ResponseBody
	@GetMapping("/string")
	public ResponseEntity<String> responseString() {
		return new ResponseEntity<String>("success", HttpStatus.OK);
	}
	
	@ResponseBody
	@GetMapping("/beans")
	public ResponseEntity<Member> responseBeans() {
		Member member = new Member();
		member.setId("hong");
		member.setName("홍길동");
		return new ResponseEntity<Member>(member, HttpStatus.OK);
	}
}

실행결과

요청 url : localhost:8080/resEntity/void

요청 url : localhost:8080/resEntity/string

요청 url : localhost:8080/resEntity/beans

'프레임워크 > Spring Boot' 카테고리의 다른 글

SpringMVC - @ModelAttribute (메소드 레벨)  (0) 2022.06.01
Spring MVC @ResponseBody  (0) 2022.05.30
Spring MVC @RequestBody  (0) 2022.05.26

관련글 더보기

댓글 영역