코드에 String을 직접 담는 것은 오타의 위험성이 존재하기 때문에 리팩토링을 진행 → enum을 이용 (HttpHeaders.CONTENT_TYPTE, MediaType.TEXT_PLAIN_VALUE)
resp.setStatus(HttpStatus.OK.value());
resp.setHeader(HttpHeaders.CONTENT_TYPE, MediaType.TEXT_PLAIN_VALUE);
public class HellobootApplication {
public static void main(String[] args) {
ServletWebServerFactory serverFactory = new TomcatServletWebServerFactory();
WebServer webServer = serverFactory.getWebServer(servletContext -> {
servletContext.addServlet("hello", new HttpServlet() {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String name = req.getParameter("name");
resp.setStatus(HttpStatus.OK.value());
resp.setHeader(HttpHeaders.CONTENT_TYPE, MediaType.TEXT_PLAIN_VALUE);
resp.getWriter().println("Hello " + name);
}
}).addMapping("/hello");
}); // 웹서버 생성
webServer.start(); // Tomcat Servelet Container 동작
}
}