- HTTP 요청과 응답헤더
- 바디
- 요청 - 메서드, path, http version
- 응답헤더
- 바디
- http version , 상태코드, 상태 text
***독립실행이 가능한 서블릿컨테이너를 만들어봄
우선 스프링부트에서 서블릿 컨테이너를 만드는 간단한 작업 부터 시작
package tobyspring.helloboot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class HellobootApplication {
public static void main(String[] args) {
SpringApplication.run(HellobootApplication.class, args);
}
}
ㅇ우선 스프링부트어플리케이션 어노테이션을 제거.
톰캣도 결국 자바로 짜여진 것이기 떄문에 임베디드된 톰캣을 불러서 사용
public class HellobootApplication {
public static void main(String[] args) {
TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory();
WebServer webServer = factory.getWebServer(servletContext -> {
servletContext.addServlet("hello", new HttpServlet(){
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setStatus(200);
resp.setHeader("Content-Type", "text/plain");
resp.getWriter().println("HELLO SERVLET");
}
}).addMapping("/hello");
});
webServer.start();
}
}
위 코드를 사용해서 톰캣처럼 동작하게 함.
* 프론트 컨트롤러
서블릿은 각 URL에 맡아서 각자 처리하는 방식으로 동작해야하지만
중앙제어시스템으로 이해하면 됨.
인증, 다국어 처리 등 프론트 컨트롤러가 있으면 유용함.
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 인증, 보안 , 다국어 처리 등 공통처리
if(req.getRequestURI().equals("/hello") && req.getMethod().equals(HttpMethod.GET.name())) {
resp.setStatus(200);
resp.setHeader("Content-Type", "text/plain");
resp.getWriter().println("HELLO SERVLET");
}
else if(req.getRequestURI().equals("/user")) {
//
}
else {
resp.setStatus(HttpStatus.NOT_FOUND.value());
}
키워드 : 매핑, 바인딩
'Spring Boot' 카테고리의 다른 글
20230604_스프링부트학습 (0) | 2023.06.04 |
---|---|
20230528_스프링부트학습 (0) | 2023.05.28 |
20230526_스프링부트 학습 (0) | 2023.05.26 |
20230521_스프링부트 학습 (0) | 2023.05.21 |
인프런_스프링부트학습_20230514 (0) | 2023.05.14 |