Spring @Controller @RestController 차이를 알아보자

Hayoung Kim·2022년 3월 7일
0

SpringFramework

목록 보기
8/9

이 둘의 가장 큰 차이점은 HTTP Response Body가 생성되는 방식이다.

  1. Controller : Spring MVC 의 컨트롤러로 주로 View를 반환하기 위해 사용한다.
@Controller
public class TestController{

	@PostMapping("sample/test/save")
    public String save(){
    	return "save";
    }
}
  1. RestController : Restful Web Service에서 사용되는 컨트롤러
    @Controller + @ResponseBody 가 합쳐진 형태로 Json 형태로 객체 데이터를 반환한다.
@Controller
public class TestController{

	private final TestService testService;
	
	@PostMapping("sample/test/save")
    public String save(@RequestBody Sample sample){
    	String id = Sample.getId();
    	return testService.findById(id);
    }
}

0개의 댓글