목록Spring (36)
Rootable의 개발일기
📌 @RequestBody HTTP 요청 데이터 body를 조회할 때 사용하는 애노테이션 스프링MVC 내부에서 HTTP 메시지 바디를 읽어서 문자나 객체로 변환해서 전달해주는데, 이때 HttpMessageConverter라는 기능을 사용한다. (@ResponseBody도 마찬가지) 🔎 단순 텍스트 @ResponseBody @PostMapping("/request-body-string-v4") public String requestBodyStringV4(@RequestBody String messageBody) { log.info("messageBody={}", messageBody); return "ok"; } 🔎 JSON @ResponseBody @PostMapping("/request-body-jso..
📌 Spring MVC 패턴 Model, View, Controller 의 약자로, 요청에 대한 로직 수행과 뷰 렌더링 작업을 분리하기 위해 도입되었다. 동작 방식은 요청이 들어오면 Controller에서 인식하여 렌더링할 뷰를 선택한다. 여기서 View에 전달할 데이터가 있다면 Model이라는 객채에 담아 전달한다. 그리고 Controller가 가리키는 View는 Model의 값과 함께 화면을 렌더링하는 식으로 동작한다. Model 데이터를 가진 객체를 지칭한다. 데이터는 내부의 상태에 대한 정보를 가질 수도 있고, 모델을 표현하는 이름 속성으로 가질 수 있다. 모델의 상태에 변화가 있을 때 컨트롤러와 뷰에 이를 통보한다. 이와 같은 통보를 통해 뷰는 최신의 결과를 보여줄 수 있고, 컨트롤러는 모델의 ..
1. 웹 브라우저는 사용자가 입력한 URL 을 내장 톰캣 서버에 요청 2. 내장 톰캣 서버는 해당 URL과 매칭되는 Controller 를 찾는다. - 만약 적절한 Controller 를 못 찾는다면 resources 폴더에서 해당 html 파일을 찾아 그대로 내려준다. 3. 찾았다면 해당 Controller 가 반환하는 값 즉, html 파일명을 templates 디렉토리 이하에서 찾는다. - 이렇게 리턴한 값에 맞는 화면을 찾는 일을 뷰 리졸버(viewResolver)가 수행한다. - 기본 매핑 : resources/templates/ + {viewName} + .html 4. html 파일을 렌더링한다. - 여기서 Model 객체를 통해 전달한 값이 있다면 Thymeleaf 를..
📌 @Controller Spring 이 처음 로딩될 때, Spring Container 라는 공간이 생긴다. 그리고 이 공간에 @Controller 가 선언된 객체들을 넣어두고 관리한다. 이것을 스프링 컨테이너에서 스프링 빈이 관리된다라고 한다. Web Application에서 첫 번째 진입점사용자의 요청이 진입하는 지점사용자 요청을 받아서 파라미터를 검증(요청 스펙을 검증)요청에 따라 어떤 처리를 할지 결정을 서비스에 넘김 (실제 처리는 서비스에서 수행)Model 객체를 만들어 데이터를 담고 사용자에게 지정된 View 를 반환하여 응답 결과를 보여준다. ❓ Controller 를 왜 사용할까 특정 비즈니스 목적을 가진 프로젝트에서 수행하는 기능은 다양하다. 이러한 기능을 수행하기 위해서는 기본적으로..