Spring

@Controller와 @RestController

yong_ღ'ᴗ'ღ 2022. 11. 9. 01:01

- Presentation Layer: 클라이언트의 요청을 받아서 Application Layer에게 처리 넘겨줌

- Application Layer: 비즈니스 로직 처리

- Data Access Layer: DB에 접근하여 데이터를 저장하거나 조회

 

@Controller와 @RestController의 차이

     - 클라이언트의 요청을 받아서 처리하고 응답하는 동작은 유사하다.

     - 응답하는 형태가 다르다.

     - @Controller : "View"를 반환 (html 파일 등) → view의 이름을 반환

                                      ex) return "hello"; 

                                      → hello.html 문서를 반환한 것이다. 즉, view의 이름을 의미한다.

     - @RestController: "Data"를 반환 (문자열, Json, xml 등) → 주로 Json

                                             메서드가 반환하는 데이터 자체를 응답으로 받게된다.

                                             ex) return "hello"; 

                                             → "hello"라는 문자열 자체를 반환



연습할 때 파일 위치

▶ controller.java 클래스파일

    src > main > java > com.~~~ > controller 패키지 안에 만들었음

▶ HTML 파일

    src > main > resources > templates 안에 만들었음