From 896e811a1840da707d3c8bda56ae33f2fabd9f93 Mon Sep 17 00:00:00 2001 From: Geonhoe Lee <79038908+rawfishthelgh@users.noreply.github.com> Date: Tue, 26 Sep 2023 17:46:49 +0900 Subject: [PATCH] =?UTF-8?q?[MVC=20=EA=B5=AC=ED=98=84=ED=95=98=EA=B8=B0=20-?= =?UTF-8?q?=203=EB=8B=A8=EA=B3=84]=20=ED=95=98=EB=A7=88=EB=93=9C(=EC=9D=B4?= =?UTF-8?q?=EA=B1=B4=ED=9A=8C)=20=EB=AF=B8=EC=85=98=20=EC=A0=9C=EC=B6=9C?= =?UTF-8?q?=ED=95=A9=EB=8B=88=EB=8B=A4.=20=20(#548)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 패키지 위치 변경 및 코드 정리 * 서블릿 학습 테스트 코드 개선 * chore: 충돌 해결 * refactor: Mapper 관련 클래스 mvc 패키지로 이동 * refactor: 어노테이션 기반 컨트롤러로 대체 * feat: json타입 직렬화 * refactor: DispatcherServlet 위치 이동 * refactor: DispatcherServlet 위치 이동 * refactor: 인덱스 페이지 어노테이션 기반 출력 * refactor: 데이터 사이즈 1개일 때 값을 그대로 반환 --------- Co-authored-by: kang-hyungu --- .../DispatcherServletInitializer.java | 1 + .../controller/IndexController.java | 18 ++++++ .../controller/LoginController.java | 2 +- .../controller/LoginViewController.java | 22 ------- .../controller/LogoutController.java | 17 ++++-- .../controller/RegisterController.java | 2 +- .../controller/RegisterViewController.java | 13 ----- .../techcourse/controller/UserController.java | 32 ++++++++++ app/src/main/webapp/index.html | 2 +- app/src/main/webapp/index.jsp | 2 +- app/src/main/webapp/login.jsp | 2 +- .../AnnotationHandlerAdapterTest.java | 3 +- .../ManualHandlerAdapterTest.java | 2 + .../web/servlet/mvc}/DispatcherServlet.java | 14 ++--- .../mvc/tobe/AnnotationHandlerMapping.java | 58 ------------------- .../AnnotationHandlerAdapter.java | 2 +- .../AnnotationHandlerMapping.java | 2 +- .../tobe}/handlerMapper/HandlerAdapter.java | 2 +- .../tobe}/handlerMapper/HandlerMapping.java | 2 +- .../handlerMapper/ManualHandlerAdapter.java | 2 +- .../handlerMapper/ManualHandlerMapping.java | 6 +- .../web/servlet/view/JsonView.java | 17 ++++++ .../tobe/AnnotationHandlerMappingTest.java | 4 +- 23 files changed, 104 insertions(+), 123 deletions(-) create mode 100644 app/src/main/java/com/techcourse/controller/IndexController.java delete mode 100644 app/src/main/java/com/techcourse/controller/LoginViewController.java delete mode 100644 app/src/main/java/com/techcourse/controller/RegisterViewController.java create mode 100644 app/src/main/java/com/techcourse/controller/UserController.java rename {app/src/main/java/com/techcourse => mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc}/DispatcherServlet.java (81%) delete mode 100644 mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/AnnotationHandlerMapping.java rename {app/src/main/java/com/techcourse => mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe}/handlerMapper/AnnotationHandlerAdapter.java (89%) rename {app/src/main/java/com/techcourse => mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe}/handlerMapper/AnnotationHandlerMapping.java (97%) rename {app/src/main/java/com/techcourse => mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe}/handlerMapper/HandlerAdapter.java (83%) rename {app/src/main/java/com/techcourse => mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe}/handlerMapper/HandlerMapping.java (69%) rename {app/src/main/java/com/techcourse => mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe}/handlerMapper/ManualHandlerAdapter.java (91%) rename {app/src/main/java/com/techcourse => mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe}/handlerMapper/ManualHandlerMapping.java (82%) diff --git a/app/src/main/java/com/techcourse/DispatcherServletInitializer.java b/app/src/main/java/com/techcourse/DispatcherServletInitializer.java index 6e814cdd25..53fb9af5c8 100644 --- a/app/src/main/java/com/techcourse/DispatcherServletInitializer.java +++ b/app/src/main/java/com/techcourse/DispatcherServletInitializer.java @@ -4,6 +4,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import web.org.springframework.web.WebApplicationInitializer; +import webmvc.org.springframework.web.servlet.mvc.DispatcherServlet; /** * Base class for {@link WebApplicationInitializer} diff --git a/app/src/main/java/com/techcourse/controller/IndexController.java b/app/src/main/java/com/techcourse/controller/IndexController.java new file mode 100644 index 0000000000..21f78bd55f --- /dev/null +++ b/app/src/main/java/com/techcourse/controller/IndexController.java @@ -0,0 +1,18 @@ +package com.techcourse.controller; + +import context.org.springframework.stereotype.Controller; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import web.org.springframework.web.bind.annotation.RequestMapping; +import web.org.springframework.web.bind.annotation.RequestMethod; +import webmvc.org.springframework.web.servlet.ModelAndView; +import webmvc.org.springframework.web.servlet.view.JspView; + +@Controller +public class IndexController { + + @RequestMapping(value = "/", method = RequestMethod.GET) + public ModelAndView showPage(HttpServletRequest request, HttpServletResponse response) { + return new ModelAndView(new JspView("/index.jsp")); + } +} diff --git a/app/src/main/java/com/techcourse/controller/LoginController.java b/app/src/main/java/com/techcourse/controller/LoginController.java index 82bc4c2404..27be6f55d1 100644 --- a/app/src/main/java/com/techcourse/controller/LoginController.java +++ b/app/src/main/java/com/techcourse/controller/LoginController.java @@ -13,7 +13,7 @@ @Controller public class LoginController { - @RequestMapping(value = "/login/view", method = RequestMethod.GET) + @RequestMapping(value = "/login", method = RequestMethod.GET) public ModelAndView showPage(HttpServletRequest request, HttpServletResponse response) { if (UserSession.isLoggedIn(request.getSession())) { return new ModelAndView(new JspView("redirect:/index.jsp")); diff --git a/app/src/main/java/com/techcourse/controller/LoginViewController.java b/app/src/main/java/com/techcourse/controller/LoginViewController.java deleted file mode 100644 index 86ec26cdce..0000000000 --- a/app/src/main/java/com/techcourse/controller/LoginViewController.java +++ /dev/null @@ -1,22 +0,0 @@ -package com.techcourse.controller; - -import jakarta.servlet.http.HttpServletRequest; -import jakarta.servlet.http.HttpServletResponse; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import webmvc.org.springframework.web.servlet.mvc.asis.Controller; - -public class LoginViewController implements Controller { - - private static final Logger log = LoggerFactory.getLogger(LoginViewController.class); - - @Override - public String execute(final HttpServletRequest req, final HttpServletResponse res) throws Exception { - return UserSession.getUserFrom(req.getSession()) - .map(user -> { - log.info("logged in {}", user.getAccount()); - return "redirect:/index.jsp"; - }) - .orElse("/login.jsp"); - } -} diff --git a/app/src/main/java/com/techcourse/controller/LogoutController.java b/app/src/main/java/com/techcourse/controller/LogoutController.java index 4642fd9450..0d98a4144c 100644 --- a/app/src/main/java/com/techcourse/controller/LogoutController.java +++ b/app/src/main/java/com/techcourse/controller/LogoutController.java @@ -1,15 +1,20 @@ package com.techcourse.controller; +import context.org.springframework.stereotype.Controller; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; -import webmvc.org.springframework.web.servlet.mvc.asis.Controller; +import web.org.springframework.web.bind.annotation.RequestMapping; +import web.org.springframework.web.bind.annotation.RequestMethod; +import webmvc.org.springframework.web.servlet.ModelAndView; +import webmvc.org.springframework.web.servlet.view.JspView; -public class LogoutController implements Controller { +@Controller +public class LogoutController { - @Override - public String execute(final HttpServletRequest req, final HttpServletResponse res) throws Exception { - final var session = req.getSession(); + @RequestMapping(value = "/logout", method = RequestMethod.GET) + public ModelAndView showPage(HttpServletRequest request, HttpServletResponse response) { + final var session = request.getSession(); session.removeAttribute(UserSession.SESSION_KEY); - return "redirect:/"; + return new ModelAndView(new JspView("redirect:/")); } } diff --git a/app/src/main/java/com/techcourse/controller/RegisterController.java b/app/src/main/java/com/techcourse/controller/RegisterController.java index fa275b26e9..ef378d578f 100644 --- a/app/src/main/java/com/techcourse/controller/RegisterController.java +++ b/app/src/main/java/com/techcourse/controller/RegisterController.java @@ -23,7 +23,7 @@ public ModelAndView register(HttpServletRequest request, HttpServletResponse res return new ModelAndView(new JspView("redirect:/index.jsp")); } - @RequestMapping(value = "/register/view", method = RequestMethod.GET) + @RequestMapping(value = "/register", method = RequestMethod.GET) public ModelAndView showPage(HttpServletRequest request, HttpServletResponse response) { return new ModelAndView(new JspView("/register.jsp")); } diff --git a/app/src/main/java/com/techcourse/controller/RegisterViewController.java b/app/src/main/java/com/techcourse/controller/RegisterViewController.java deleted file mode 100644 index 136962136d..0000000000 --- a/app/src/main/java/com/techcourse/controller/RegisterViewController.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.techcourse.controller; - -import jakarta.servlet.http.HttpServletRequest; -import jakarta.servlet.http.HttpServletResponse; -import webmvc.org.springframework.web.servlet.mvc.asis.Controller; - -public class RegisterViewController implements Controller { - - @Override - public String execute(final HttpServletRequest req, final HttpServletResponse res) throws Exception { - return "/register.jsp"; - } -} diff --git a/app/src/main/java/com/techcourse/controller/UserController.java b/app/src/main/java/com/techcourse/controller/UserController.java new file mode 100644 index 0000000000..c1361f487b --- /dev/null +++ b/app/src/main/java/com/techcourse/controller/UserController.java @@ -0,0 +1,32 @@ +package com.techcourse.controller; + +import com.techcourse.domain.User; +import com.techcourse.repository.InMemoryUserRepository; +import context.org.springframework.stereotype.Controller; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import web.org.springframework.web.bind.annotation.RequestMapping; +import web.org.springframework.web.bind.annotation.RequestMethod; +import webmvc.org.springframework.web.servlet.ModelAndView; +import webmvc.org.springframework.web.servlet.view.JsonView; + +@Controller +public class UserController { + + private static final Logger log = LoggerFactory.getLogger(UserController.class); + + @RequestMapping(value = "/api/user", method = RequestMethod.GET) + public ModelAndView show(HttpServletRequest request, HttpServletResponse response) { + final String account = request.getParameter("account"); + log.debug("user id : {}", account); + + final ModelAndView modelAndView = new ModelAndView(new JsonView()); + final User user = InMemoryUserRepository.findByAccount(account) + .orElseThrow(); + + modelAndView.addObject("user", user); + return modelAndView; + } +} diff --git a/app/src/main/webapp/index.html b/app/src/main/webapp/index.html index 95df361069..1ca98f1be2 100644 --- a/app/src/main/webapp/index.html +++ b/app/src/main/webapp/index.html @@ -19,7 +19,7 @@ diff --git a/app/src/main/webapp/index.jsp b/app/src/main/webapp/index.jsp index 142ee3d104..4ee84d2926 100644 --- a/app/src/main/webapp/index.jsp +++ b/app/src/main/webapp/index.jsp @@ -26,7 +26,7 @@ <% } else { %> <% } %> diff --git a/app/src/main/webapp/login.jsp b/app/src/main/webapp/login.jsp index 486640b810..1d63fe4eb4 100644 --- a/app/src/main/webapp/login.jsp +++ b/app/src/main/webapp/login.jsp @@ -30,7 +30,7 @@ diff --git a/app/src/test/java/com/techcourse/handlerMapper/AnnotationHandlerAdapterTest.java b/app/src/test/java/com/techcourse/handlerMapper/AnnotationHandlerAdapterTest.java index 065224a2de..89043c66c3 100644 --- a/app/src/test/java/com/techcourse/handlerMapper/AnnotationHandlerAdapterTest.java +++ b/app/src/test/java/com/techcourse/handlerMapper/AnnotationHandlerAdapterTest.java @@ -5,7 +5,8 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import webmvc.org.springframework.web.servlet.ModelAndView; -import webmvc.org.springframework.web.servlet.mvc.tobe.AnnotationHandlerMapping; +import webmvc.org.springframework.web.servlet.mvc.tobe.handlerMapper.AnnotationHandlerAdapter; +import webmvc.org.springframework.web.servlet.mvc.tobe.handlerMapper.AnnotationHandlerMapping; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.mock; diff --git a/app/src/test/java/com/techcourse/handlerMapper/ManualHandlerAdapterTest.java b/app/src/test/java/com/techcourse/handlerMapper/ManualHandlerAdapterTest.java index 023222a099..e8e03bae74 100644 --- a/app/src/test/java/com/techcourse/handlerMapper/ManualHandlerAdapterTest.java +++ b/app/src/test/java/com/techcourse/handlerMapper/ManualHandlerAdapterTest.java @@ -5,6 +5,8 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import webmvc.org.springframework.web.servlet.ModelAndView; +import webmvc.org.springframework.web.servlet.mvc.tobe.handlerMapper.ManualHandlerAdapter; +import webmvc.org.springframework.web.servlet.mvc.tobe.handlerMapper.ManualHandlerMapping; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.mock; diff --git a/app/src/main/java/com/techcourse/DispatcherServlet.java b/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/DispatcherServlet.java similarity index 81% rename from app/src/main/java/com/techcourse/DispatcherServlet.java rename to mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/DispatcherServlet.java index ed05610439..0b1071241b 100644 --- a/app/src/main/java/com/techcourse/DispatcherServlet.java +++ b/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/DispatcherServlet.java @@ -1,11 +1,5 @@ -package com.techcourse; +package webmvc.org.springframework.web.servlet.mvc; -import com.techcourse.handlerMapper.AnnotationHandlerAdapter; -import com.techcourse.handlerMapper.AnnotationHandlerMapping; -import com.techcourse.handlerMapper.HandlerAdapter; -import com.techcourse.handlerMapper.HandlerMapping; -import com.techcourse.handlerMapper.ManualHandlerAdapter; -import com.techcourse.handlerMapper.ManualHandlerMapping; import jakarta.servlet.ServletException; import jakarta.servlet.http.HttpServlet; import jakarta.servlet.http.HttpServletRequest; @@ -14,6 +8,12 @@ import org.slf4j.LoggerFactory; import webmvc.org.springframework.web.servlet.ModelAndView; import webmvc.org.springframework.web.servlet.View; +import webmvc.org.springframework.web.servlet.mvc.tobe.handlerMapper.AnnotationHandlerAdapter; +import webmvc.org.springframework.web.servlet.mvc.tobe.handlerMapper.AnnotationHandlerMapping; +import webmvc.org.springframework.web.servlet.mvc.tobe.handlerMapper.HandlerAdapter; +import webmvc.org.springframework.web.servlet.mvc.tobe.handlerMapper.HandlerMapping; +import webmvc.org.springframework.web.servlet.mvc.tobe.handlerMapper.ManualHandlerAdapter; +import webmvc.org.springframework.web.servlet.mvc.tobe.handlerMapper.ManualHandlerMapping; import java.util.ArrayList; import java.util.List; diff --git a/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/AnnotationHandlerMapping.java b/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/AnnotationHandlerMapping.java deleted file mode 100644 index 5479d55c47..0000000000 --- a/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/AnnotationHandlerMapping.java +++ /dev/null @@ -1,58 +0,0 @@ -package webmvc.org.springframework.web.servlet.mvc.tobe; - -import context.org.springframework.stereotype.Controller; -import jakarta.servlet.http.HttpServletRequest; -import org.reflections.Reflections; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import web.org.springframework.web.bind.annotation.RequestMapping; -import web.org.springframework.web.bind.annotation.RequestMethod; - -import java.lang.reflect.Method; -import java.util.HashMap; -import java.util.Map; -import java.util.Set; - -public class AnnotationHandlerMapping { - - private static final Logger log = LoggerFactory.getLogger(AnnotationHandlerMapping.class); - - private final Object[] basePackage; - private final Map handlerExecutions; - - public AnnotationHandlerMapping(final Object... basePackage) { - this.basePackage = basePackage; - this.handlerExecutions = new HashMap<>(); - } - - public void initialize() { - Reflections reflections = new Reflections(basePackage); - Set> controllers = reflections.getTypesAnnotatedWith(Controller.class); - for (Class controller : controllers) { - Method[] methods = controller.getDeclaredMethods(); - for (Method method : methods) { - if (method.isAnnotationPresent(RequestMapping.class)) { - RequestMapping annotation = method.getAnnotation(RequestMapping.class); - String url = annotation.value(); - for (RequestMethod requestMethod : annotation.method()) { - Object instance = null; - try { - instance = controller.getDeclaredConstructor().newInstance(); - } catch (Exception e) { - throw new RuntimeException(e); - } - HandlerKey handlerKey = new HandlerKey(url, requestMethod); - HandlerExecution handlerExecution = new HandlerExecution(instance, method); - handlerExecutions.put(handlerKey, handlerExecution); - } - } - } - } - log.info("Initialized AnnotationHandlerMapping!"); - } - - public Object getHandler(final HttpServletRequest request) { - HandlerKey handlerKey = new HandlerKey(request.getRequestURI(), RequestMethod.valueOf(request.getMethod())); - return handlerExecutions.get(handlerKey); - } -} diff --git a/app/src/main/java/com/techcourse/handlerMapper/AnnotationHandlerAdapter.java b/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/handlerMapper/AnnotationHandlerAdapter.java similarity index 89% rename from app/src/main/java/com/techcourse/handlerMapper/AnnotationHandlerAdapter.java rename to mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/handlerMapper/AnnotationHandlerAdapter.java index 327e2ced22..0e2feae6f8 100644 --- a/app/src/main/java/com/techcourse/handlerMapper/AnnotationHandlerAdapter.java +++ b/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/handlerMapper/AnnotationHandlerAdapter.java @@ -1,4 +1,4 @@ -package com.techcourse.handlerMapper; +package webmvc.org.springframework.web.servlet.mvc.tobe.handlerMapper; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; diff --git a/app/src/main/java/com/techcourse/handlerMapper/AnnotationHandlerMapping.java b/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/handlerMapper/AnnotationHandlerMapping.java similarity index 97% rename from app/src/main/java/com/techcourse/handlerMapper/AnnotationHandlerMapping.java rename to mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/handlerMapper/AnnotationHandlerMapping.java index 968999d6eb..1e9a864bbf 100644 --- a/app/src/main/java/com/techcourse/handlerMapper/AnnotationHandlerMapping.java +++ b/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/handlerMapper/AnnotationHandlerMapping.java @@ -1,4 +1,4 @@ -package com.techcourse.handlerMapper; +package webmvc.org.springframework.web.servlet.mvc.tobe.handlerMapper; import context.org.springframework.stereotype.Controller; import jakarta.servlet.http.HttpServletRequest; diff --git a/app/src/main/java/com/techcourse/handlerMapper/HandlerAdapter.java b/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/handlerMapper/HandlerAdapter.java similarity index 83% rename from app/src/main/java/com/techcourse/handlerMapper/HandlerAdapter.java rename to mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/handlerMapper/HandlerAdapter.java index 6c56affbe3..d2830fd5ed 100644 --- a/app/src/main/java/com/techcourse/handlerMapper/HandlerAdapter.java +++ b/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/handlerMapper/HandlerAdapter.java @@ -1,4 +1,4 @@ -package com.techcourse.handlerMapper; +package webmvc.org.springframework.web.servlet.mvc.tobe.handlerMapper; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; diff --git a/app/src/main/java/com/techcourse/handlerMapper/HandlerMapping.java b/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/handlerMapper/HandlerMapping.java similarity index 69% rename from app/src/main/java/com/techcourse/handlerMapper/HandlerMapping.java rename to mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/handlerMapper/HandlerMapping.java index 7a2fbebc7f..390134e324 100644 --- a/app/src/main/java/com/techcourse/handlerMapper/HandlerMapping.java +++ b/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/handlerMapper/HandlerMapping.java @@ -1,4 +1,4 @@ -package com.techcourse.handlerMapper; +package webmvc.org.springframework.web.servlet.mvc.tobe.handlerMapper; import jakarta.servlet.http.HttpServletRequest; diff --git a/app/src/main/java/com/techcourse/handlerMapper/ManualHandlerAdapter.java b/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/handlerMapper/ManualHandlerAdapter.java similarity index 91% rename from app/src/main/java/com/techcourse/handlerMapper/ManualHandlerAdapter.java rename to mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/handlerMapper/ManualHandlerAdapter.java index 3c835259c7..7291202a3c 100644 --- a/app/src/main/java/com/techcourse/handlerMapper/ManualHandlerAdapter.java +++ b/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/handlerMapper/ManualHandlerAdapter.java @@ -1,4 +1,4 @@ -package com.techcourse.handlerMapper; +package webmvc.org.springframework.web.servlet.mvc.tobe.handlerMapper; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; diff --git a/app/src/main/java/com/techcourse/handlerMapper/ManualHandlerMapping.java b/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/handlerMapper/ManualHandlerMapping.java similarity index 82% rename from app/src/main/java/com/techcourse/handlerMapper/ManualHandlerMapping.java rename to mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/handlerMapper/ManualHandlerMapping.java index 05eaf701c1..51780f2e40 100644 --- a/app/src/main/java/com/techcourse/handlerMapper/ManualHandlerMapping.java +++ b/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/handlerMapper/ManualHandlerMapping.java @@ -1,6 +1,5 @@ -package com.techcourse.handlerMapper; +package webmvc.org.springframework.web.servlet.mvc.tobe.handlerMapper; -import com.techcourse.controller.LogoutController; import jakarta.servlet.http.HttpServletRequest; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -18,9 +17,6 @@ public class ManualHandlerMapping implements HandlerMapping { @Override public void initialize() { - controllers.put("/", new ForwardController("/index.jsp")); - controllers.put("/logout", new LogoutController()); - log.info("Initialized Handler Mapping!"); controllers.keySet() .forEach(path -> log.info("Path : {}, Controller : {}", path, controllers.get(path).getClass())); diff --git a/mvc/src/main/java/webmvc/org/springframework/web/servlet/view/JsonView.java b/mvc/src/main/java/webmvc/org/springframework/web/servlet/view/JsonView.java index b42c3466f0..5c729b2acf 100644 --- a/mvc/src/main/java/webmvc/org/springframework/web/servlet/view/JsonView.java +++ b/mvc/src/main/java/webmvc/org/springframework/web/servlet/view/JsonView.java @@ -1,14 +1,31 @@ package webmvc.org.springframework.web.servlet.view; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; +import web.org.springframework.http.MediaType; import webmvc.org.springframework.web.servlet.View; +import java.io.PrintWriter; import java.util.Map; public class JsonView implements View { @Override public void render(final Map model, final HttpServletRequest request, HttpServletResponse response) throws Exception { + ObjectMapper objectMapper = new ObjectMapper(); + String jsonString = extractJsonString(model, objectMapper); + PrintWriter printWriter = response.getWriter(); + response.setContentType(MediaType.APPLICATION_JSON_UTF8_VALUE); + printWriter.write(jsonString); + } + + private String extractJsonString(Map model, ObjectMapper objectMapper) throws JsonProcessingException { + if (model.size() == 1) { + Object value = model.values().toArray()[0]; + return objectMapper.writeValueAsString(value); + } + return objectMapper.writeValueAsString(model); } } diff --git a/mvc/src/test/java/webmvc/org/springframework/web/servlet/mvc/tobe/AnnotationHandlerMappingTest.java b/mvc/src/test/java/webmvc/org/springframework/web/servlet/mvc/tobe/AnnotationHandlerMappingTest.java index dcec215a3f..5ad17e6985 100644 --- a/mvc/src/test/java/webmvc/org/springframework/web/servlet/mvc/tobe/AnnotationHandlerMappingTest.java +++ b/mvc/src/test/java/webmvc/org/springframework/web/servlet/mvc/tobe/AnnotationHandlerMappingTest.java @@ -4,6 +4,8 @@ import jakarta.servlet.http.HttpServletResponse; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import webmvc.org.springframework.web.servlet.mvc.tobe.handlerMapper.AnnotationHandlerMapping; +import webmvc.org.springframework.web.servlet.mvc.tobe.handlerMapper.HandlerMapping; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.mock; @@ -11,7 +13,7 @@ class AnnotationHandlerMappingTest { - private AnnotationHandlerMapping handlerMapping; + private HandlerMapping handlerMapping; @BeforeEach void setUp() {