From 6623ea11b2abd7de90fe58426e2cad1476e923f9 Mon Sep 17 00:00:00 2001 From: jaeyeon kim Date: Sun, 24 Sep 2023 17:57:46 +0900 Subject: [PATCH 1/9] =?UTF-8?q?refactor=20:=20HandlerAdapter=20=EB=8F=84?= =?UTF-8?q?=EC=9E=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/techcourse/DispatcherServlet.java | 20 +++++----- .../java/com/techcourse/HandlerAdapters.java | 39 +++++++++++++++++++ .../java/com/techcourse/HandlerMappings.java | 7 ++-- .../com/techcourse/ManualHandlerMapping.java | 20 +--------- .../mvc/tobe/AnnotationHandlerAdapter.java | 25 ++++++++++++ .../mvc/tobe/AnnotationHandlerMapping.java | 4 +- .../web/servlet/mvc/tobe/HandlerAdapter.java | 13 +++++++ .../servlet/mvc/tobe/HandlerExecution.java | 15 +++---- .../web/servlet/mvc/tobe/HandlerMapping.java | 2 +- .../mvc/tobe/ManualHandlerAdapter.java | 31 +++++++++++++++ 10 files changed, 130 insertions(+), 46 deletions(-) create mode 100644 app/src/main/java/com/techcourse/HandlerAdapters.java create mode 100644 mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/AnnotationHandlerAdapter.java create mode 100644 mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/HandlerAdapter.java create mode 100644 mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/ManualHandlerAdapter.java diff --git a/app/src/main/java/com/techcourse/DispatcherServlet.java b/app/src/main/java/com/techcourse/DispatcherServlet.java index a6e6625f9b..5d7c5d8e31 100644 --- a/app/src/main/java/com/techcourse/DispatcherServlet.java +++ b/app/src/main/java/com/techcourse/DispatcherServlet.java @@ -4,13 +4,12 @@ import jakarta.servlet.http.HttpServlet; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; -import java.util.Optional; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import web.org.springframework.http.HttpStatus; import webmvc.org.springframework.web.servlet.ModelAndView; +import webmvc.org.springframework.web.servlet.mvc.tobe.AnnotationHandlerAdapter; import webmvc.org.springframework.web.servlet.mvc.tobe.AnnotationHandlerMapping; -import webmvc.org.springframework.web.servlet.mvc.tobe.HandlerExecution; +import webmvc.org.springframework.web.servlet.mvc.tobe.ManualHandlerAdapter; import webmvc.org.springframework.web.servlet.view.JspView; public class DispatcherServlet extends HttpServlet { @@ -19,6 +18,7 @@ public class DispatcherServlet extends HttpServlet { private static final Logger log = LoggerFactory.getLogger(DispatcherServlet.class); private HandlerMappings handlerMappings; + private HandlerAdapters handlerAdapters; public DispatcherServlet() { } @@ -28,6 +28,10 @@ public void init() { handlerMappings = new HandlerMappings(); handlerMappings.addHandlerMapping(new AnnotationHandlerMapping("com.techcourse.controller")); handlerMappings.addHandlerMapping(new ManualHandlerMapping()); + + handlerAdapters = new HandlerAdapters(); + handlerAdapters.addHandlerAdapter(new AnnotationHandlerAdapter()); + handlerAdapters.addHandlerAdapter(new ManualHandlerAdapter()); } @Override @@ -35,14 +39,8 @@ protected void service(final HttpServletRequest request, final HttpServletRespon log.debug("Method : {}, Request URI : {}", request.getMethod(), request.getRequestURI()); try { - Optional execution = handlerMappings.getHandler(request); - - if (execution.isEmpty()) { - response.setStatus(HttpStatus.NOT_FOUND.getValue()); - return; - } - - ModelAndView view = execution.get().handle(request, response); + Object object = handlerMappings.getHandler(request); + ModelAndView view = handlerAdapters.handle(object, request, response); move(view.getViewName(), request, response); } catch (Throwable e) { log.error("Exception : {}", e.getMessage(), e); diff --git a/app/src/main/java/com/techcourse/HandlerAdapters.java b/app/src/main/java/com/techcourse/HandlerAdapters.java new file mode 100644 index 0000000000..06e805f821 --- /dev/null +++ b/app/src/main/java/com/techcourse/HandlerAdapters.java @@ -0,0 +1,39 @@ +package com.techcourse; + +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import webmvc.org.springframework.web.servlet.ModelAndView; +import webmvc.org.springframework.web.servlet.mvc.tobe.HandlerAdapter; + +public class HandlerAdapters { + + private final List handlerAdapters; + + public HandlerAdapters(List handlerAdapters) { + this.handlerAdapters = new ArrayList<>(handlerAdapters); + } + + public HandlerAdapters() { + this(Collections.emptyList()); + } + + public void addHandlerAdapter(HandlerAdapter handlerAdapter) { + handlerAdapters.add(handlerAdapter); + } + + public ModelAndView handle( + Object object, + HttpServletRequest request, + HttpServletResponse response + ) { + return handlerAdapters.stream() + .filter(handlerAdapter -> handlerAdapter.isSupport(object)) + .map(handlerAdapter -> handlerAdapter.handle(object, request, response)) + .findFirst() + .orElseThrow(IllegalArgumentException::new); + } + +} diff --git a/app/src/main/java/com/techcourse/HandlerMappings.java b/app/src/main/java/com/techcourse/HandlerMappings.java index d1cb22556a..85d53e711d 100644 --- a/app/src/main/java/com/techcourse/HandlerMappings.java +++ b/app/src/main/java/com/techcourse/HandlerMappings.java @@ -5,8 +5,6 @@ import java.util.Collections; import java.util.List; import java.util.Objects; -import java.util.Optional; -import webmvc.org.springframework.web.servlet.mvc.tobe.HandlerExecution; import webmvc.org.springframework.web.servlet.mvc.tobe.HandlerMapping; public class HandlerMappings { @@ -26,11 +24,12 @@ public void addHandlerMapping(HandlerMapping handlerMapping) { handlerMappings.add(handlerMapping); } - public Optional getHandler(HttpServletRequest request) { + public Object getHandler(HttpServletRequest request) { return handlerMappings.stream() .map(handlerMapping -> handlerMapping.getHandler(request)) .filter(Objects::nonNull) - .findFirst(); + .findFirst() + .orElseThrow(IllegalArgumentException::new); } } diff --git a/app/src/main/java/com/techcourse/ManualHandlerMapping.java b/app/src/main/java/com/techcourse/ManualHandlerMapping.java index ba33db7263..8c7528d248 100644 --- a/app/src/main/java/com/techcourse/ManualHandlerMapping.java +++ b/app/src/main/java/com/techcourse/ManualHandlerMapping.java @@ -6,22 +6,17 @@ import com.techcourse.controller.RegisterController; import com.techcourse.controller.RegisterViewController; import jakarta.servlet.http.HttpServletRequest; -import java.lang.reflect.Method; -import java.util.Arrays; import java.util.HashMap; import java.util.Map; -import java.util.Objects; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import webmvc.org.springframework.web.servlet.mvc.asis.Controller; import webmvc.org.springframework.web.servlet.mvc.asis.ForwardController; -import webmvc.org.springframework.web.servlet.mvc.tobe.HandlerExecution; import webmvc.org.springframework.web.servlet.mvc.tobe.HandlerMapping; public class ManualHandlerMapping implements HandlerMapping { private static final Logger log = LoggerFactory.getLogger(ManualHandlerMapping.class); - private static final String IMPLEMENT_METHOD_NAME = "execute"; private final Map controllers = new HashMap<>(); @@ -40,21 +35,10 @@ public void initialize() { } @Override - public HandlerExecution getHandler(HttpServletRequest request) { + public Object getHandler(HttpServletRequest request) { log.debug("Request Mapping Uri : {}", request.getRequestURI()); - Controller controller = controllers.getOrDefault(request.getRequestURI(), null); - if (Objects.isNull(controller)) { - return null; - } - - Method execution = Arrays.stream(controller.getClass() - .getMethods()) - .filter(method -> method.getName().equals(IMPLEMENT_METHOD_NAME)) - .findFirst() - .orElseThrow(() -> new IllegalStateException("Execute 메서드가 구현되어 있지 않습니다.")); - - return new HandlerExecution(controller, execution); + return controllers.get(request.getRequestURI()); } } diff --git a/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/AnnotationHandlerAdapter.java b/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/AnnotationHandlerAdapter.java new file mode 100644 index 0000000000..5d0303647b --- /dev/null +++ b/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/AnnotationHandlerAdapter.java @@ -0,0 +1,25 @@ +package webmvc.org.springframework.web.servlet.mvc.tobe; + +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import webmvc.org.springframework.web.servlet.ModelAndView; + +public class AnnotationHandlerAdapter implements HandlerAdapter { + + @Override + public boolean isSupport(Object object) { + return object instanceof HandlerExecution; + } + + @Override + public ModelAndView handle( + Object object, + HttpServletRequest request, + HttpServletResponse response + ) { + HandlerExecution execution = (HandlerExecution) object; + + return execution.handle(request, response); + } + +} 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 index cf9b16597e..beec3b66b1 100644 --- 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 @@ -70,13 +70,13 @@ private void putHandlerExecutionByAnnotation(Object controller, Method method) { @Override - public HandlerExecution getHandler(final HttpServletRequest request) { + public Object getHandler(final HttpServletRequest request) { HandlerKey key = new HandlerKey( request.getRequestURI(), RequestMethod.from(request.getMethod()) ); - return handlerExecutions.getOrDefault(key, null); + return handlerExecutions.get(key); } } diff --git a/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/HandlerAdapter.java b/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/HandlerAdapter.java new file mode 100644 index 0000000000..915a4e8540 --- /dev/null +++ b/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/HandlerAdapter.java @@ -0,0 +1,13 @@ +package webmvc.org.springframework.web.servlet.mvc.tobe; + +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import webmvc.org.springframework.web.servlet.ModelAndView; + +public interface HandlerAdapter { + + boolean isSupport(Object object); + + ModelAndView handle(Object object, HttpServletRequest request, HttpServletResponse response); + +} diff --git a/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/HandlerExecution.java b/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/HandlerExecution.java index edc68d2fd2..766b894510 100644 --- a/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/HandlerExecution.java +++ b/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/HandlerExecution.java @@ -4,7 +4,6 @@ import jakarta.servlet.http.HttpServletResponse; import java.lang.reflect.Method; import webmvc.org.springframework.web.servlet.ModelAndView; -import webmvc.org.springframework.web.servlet.view.JspView; public class HandlerExecution { @@ -16,16 +15,12 @@ public HandlerExecution(Object instance, Method method) { this.method = method; } - public ModelAndView handle(final HttpServletRequest request, final HttpServletResponse response) throws Exception { - Object resultOfHandle = method.invoke(instance, request, response); - - if (resultOfHandle instanceof ModelAndView) { - return (ModelAndView) resultOfHandle; + public ModelAndView handle(HttpServletRequest request, HttpServletResponse response) { + try { + return (ModelAndView) method.invoke(instance, request, response); + } catch (Exception exception) { + throw new IllegalArgumentException(exception); } - - return new ModelAndView( - new JspView((String) resultOfHandle) - ); } } diff --git a/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/HandlerMapping.java b/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/HandlerMapping.java index 36e3b5f4ed..fbd88a7a65 100644 --- a/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/HandlerMapping.java +++ b/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/HandlerMapping.java @@ -6,6 +6,6 @@ public interface HandlerMapping { void initialize(); - HandlerExecution getHandler(HttpServletRequest request); + Object getHandler(HttpServletRequest request); } diff --git a/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/ManualHandlerAdapter.java b/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/ManualHandlerAdapter.java new file mode 100644 index 0000000000..92ddf3fc70 --- /dev/null +++ b/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/ManualHandlerAdapter.java @@ -0,0 +1,31 @@ +package webmvc.org.springframework.web.servlet.mvc.tobe; + +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import webmvc.org.springframework.web.servlet.ModelAndView; +import webmvc.org.springframework.web.servlet.mvc.asis.Controller; +import webmvc.org.springframework.web.servlet.view.JspView; + +public class ManualHandlerAdapter implements HandlerAdapter { + + @Override + public boolean isSupport(Object object) { + return object instanceof Controller; + } + + @Override + public ModelAndView handle( + Object object, + HttpServletRequest request, + HttpServletResponse response + ) { + try { + Controller controller = (Controller) object; + String viewName = controller.execute(request, response); + return new ModelAndView(new JspView(viewName)); + } catch (Exception exception) { + throw new IllegalArgumentException(exception); + } + } + +} From 96315ae6fa4bf5cb7341d70276a21e5cf4763a22 Mon Sep 17 00:00:00 2001 From: jaeyeon kim Date: Sun, 24 Sep 2023 18:12:15 +0900 Subject: [PATCH 2/9] =?UTF-8?q?refactor=20:=20view=20redering=20=EC=9D=84?= =?UTF-8?q?=20JspView=20=ED=81=B4=EB=9E=98=EC=8A=A4=EA=B0=80=20=ED=95=A0?= =?UTF-8?q?=20=EC=88=98=20=EC=9E=88=EB=8F=84=EB=A1=9D=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/techcourse/DispatcherServlet.java | 17 +---------- .../web/servlet/ModelAndView.java | 3 -- .../org/springframework/web/servlet/View.java | 2 -- .../web/servlet/view/JsonView.java | 8 +----- .../web/servlet/view/JspView.java | 28 +++++++++++++------ 5 files changed, 22 insertions(+), 36 deletions(-) diff --git a/app/src/main/java/com/techcourse/DispatcherServlet.java b/app/src/main/java/com/techcourse/DispatcherServlet.java index 5d7c5d8e31..a02adb8326 100644 --- a/app/src/main/java/com/techcourse/DispatcherServlet.java +++ b/app/src/main/java/com/techcourse/DispatcherServlet.java @@ -10,7 +10,6 @@ import webmvc.org.springframework.web.servlet.mvc.tobe.AnnotationHandlerAdapter; import webmvc.org.springframework.web.servlet.mvc.tobe.AnnotationHandlerMapping; import webmvc.org.springframework.web.servlet.mvc.tobe.ManualHandlerAdapter; -import webmvc.org.springframework.web.servlet.view.JspView; public class DispatcherServlet extends HttpServlet { @@ -41,25 +40,11 @@ protected void service(final HttpServletRequest request, final HttpServletRespon try { Object object = handlerMappings.getHandler(request); ModelAndView view = handlerAdapters.handle(object, request, response); - move(view.getViewName(), request, response); + view.getView().render(view.getModel(), request, response); } catch (Throwable e) { log.error("Exception : {}", e.getMessage(), e); throw new ServletException(e.getMessage()); } } - private void move( - String viewName, - HttpServletRequest request, - HttpServletResponse response - ) throws Exception { - if (viewName.startsWith(JspView.REDIRECT_PREFIX)) { - response.sendRedirect(viewName.substring(JspView.REDIRECT_PREFIX.length())); - return; - } - - final var requestDispatcher = request.getRequestDispatcher(viewName); - requestDispatcher.forward(request, response); - } - } diff --git a/mvc/src/main/java/webmvc/org/springframework/web/servlet/ModelAndView.java b/mvc/src/main/java/webmvc/org/springframework/web/servlet/ModelAndView.java index e6ce362282..0b2a0f4a45 100644 --- a/mvc/src/main/java/webmvc/org/springframework/web/servlet/ModelAndView.java +++ b/mvc/src/main/java/webmvc/org/springframework/web/servlet/ModelAndView.java @@ -31,7 +31,4 @@ public View getView() { return view; } - public String getViewName() { - return view.getViewName(); - } } diff --git a/mvc/src/main/java/webmvc/org/springframework/web/servlet/View.java b/mvc/src/main/java/webmvc/org/springframework/web/servlet/View.java index 0c15d553f9..e2638282c5 100644 --- a/mvc/src/main/java/webmvc/org/springframework/web/servlet/View.java +++ b/mvc/src/main/java/webmvc/org/springframework/web/servlet/View.java @@ -9,6 +9,4 @@ public interface View { void render(Map model, HttpServletRequest request, HttpServletResponse response) throws Exception; - String getViewName(); - } 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 191c9fcd6c..663ca1c8b4 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 @@ -2,9 +2,8 @@ import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; -import webmvc.org.springframework.web.servlet.View; - import java.util.Map; +import webmvc.org.springframework.web.servlet.View; public class JsonView implements View { @@ -12,9 +11,4 @@ public class JsonView implements View { public void render(final Map model, final HttpServletRequest request, HttpServletResponse response) throws Exception { } - @Override - public String getViewName() { - return ""; - } - } diff --git a/mvc/src/main/java/webmvc/org/springframework/web/servlet/view/JspView.java b/mvc/src/main/java/webmvc/org/springframework/web/servlet/view/JspView.java index 8e1923ee49..0a470f8fdb 100644 --- a/mvc/src/main/java/webmvc/org/springframework/web/servlet/view/JspView.java +++ b/mvc/src/main/java/webmvc/org/springframework/web/servlet/view/JspView.java @@ -1,5 +1,6 @@ package webmvc.org.springframework.web.servlet.view; +import jakarta.servlet.RequestDispatcher; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; import java.util.Map; @@ -19,20 +20,31 @@ public JspView(final String viewName) { } @Override - public void render(final Map model, final HttpServletRequest request, final HttpServletResponse response) throws Exception { - // todo + public void render( + Map model, + HttpServletRequest request, + HttpServletResponse response + ) throws Exception { + if (viewName.startsWith(JspView.REDIRECT_PREFIX)) { + response.sendRedirect(viewName.substring(JspView.REDIRECT_PREFIX.length())); + return; + } + + renderView(model, request, response); + } + private void renderView( + Map model, + HttpServletRequest request, + HttpServletResponse response + ) throws Exception { model.keySet().forEach(key -> { log.debug("attribute name : {}, value : {}", key, model.get(key)); request.setAttribute(key, model.get(key)); }); - // todo - } - - @Override - public String getViewName() { - return viewName; + RequestDispatcher requestDispatcher = request.getRequestDispatcher(viewName); + requestDispatcher.forward(request, response); } } From 50f4f2badd278ab0598fa7dc1545cb81825b516c Mon Sep 17 00:00:00 2001 From: jaeyeon kim Date: Sun, 24 Sep 2023 18:37:01 +0900 Subject: [PATCH 3/9] =?UTF-8?q?feat=20:=20Json=20View=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../techcourse/controller/UserController.java | 32 +++++++++++++++++++ .../mvc/tobe/AnnotationHandlerMapping.java | 2 +- .../web/servlet/view/JsonView.java | 14 +++++++- 3 files changed, 46 insertions(+), 2 deletions(-) create mode 100644 app/src/main/java/com/techcourse/controller/UserController.java 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..4396b55255 --- /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) { + String account = request.getParameter("account"); + log.debug("user id : {}", account); + + ModelAndView modelAndView = new ModelAndView(new JsonView()); + User user = InMemoryUserRepository.findByAccount(account) + .orElseThrow(); + + modelAndView.addObject("user", user); + return modelAndView; + } +} \ No newline at end of file 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 index beec3b66b1..3c393edf59 100644 --- 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 @@ -70,7 +70,7 @@ private void putHandlerExecutionByAnnotation(Object controller, Method method) { @Override - public Object getHandler(final HttpServletRequest request) { + public Object getHandler(HttpServletRequest request) { HandlerKey key = new HandlerKey( request.getRequestURI(), RequestMethod.from(request.getMethod()) 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 663ca1c8b4..4bc1509ac7 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,26 @@ package webmvc.org.springframework.web.servlet.view; +import com.fasterxml.jackson.databind.ObjectMapper; +import jakarta.servlet.ServletOutputStream; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; import java.util.Map; +import web.org.springframework.http.MediaType; import webmvc.org.springframework.web.servlet.View; public class JsonView implements View { @Override - public void render(final Map model, final HttpServletRequest request, HttpServletResponse response) throws Exception { + public void render( + Map model, + HttpServletRequest request, + HttpServletResponse response + ) throws Exception { + ObjectMapper objectMapper = new ObjectMapper(); + ServletOutputStream outputStream = response.getOutputStream(); + String responseBody = objectMapper.writeValueAsString(model); + response.setContentType(MediaType.APPLICATION_JSON_UTF8_VALUE); + outputStream.write(responseBody.getBytes()); } } From fe96f5f56c9f1a367bba1c77745023a75fd67859 Mon Sep 17 00:00:00 2001 From: jaeyeon kim Date: Sun, 24 Sep 2023 18:56:03 +0900 Subject: [PATCH 4/9] =?UTF-8?q?refactor=20:=20legacy=20=EC=BD=94=EB=93=9C?= =?UTF-8?q?=20=EC=A0=9C=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/techcourse/DispatcherServlet.java | 3 -- .../com/techcourse/ManualHandlerMapping.java | 44 ------------------- .../controller/IndexController.java | 19 ++++++++ .../controller/LoginController.java | 36 ++++++++++----- .../controller/LoginViewController.java | 22 ---------- .../controller/LogoutController.java | 16 ++++--- .../RegisterAnnotationController.java | 39 ---------------- .../controller/RegisterController.java | 25 ++++++++--- .../controller/RegisterViewController.java | 14 ------ .../techcourse/controller/UserController.java | 1 + app/src/main/webapp/index.html | 2 +- app/src/main/webapp/index.jsp | 2 +- app/src/main/webapp/register.jsp | 2 +- .../web/servlet/mvc/asis/Controller.java | 10 ----- .../servlet/mvc/asis/ForwardController.java | 20 --------- .../mvc/tobe/ManualHandlerAdapter.java | 31 ------------- 16 files changed, 80 insertions(+), 206 deletions(-) delete mode 100644 app/src/main/java/com/techcourse/ManualHandlerMapping.java 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/RegisterAnnotationController.java delete mode 100644 app/src/main/java/com/techcourse/controller/RegisterViewController.java delete mode 100644 mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/asis/Controller.java delete mode 100644 mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/asis/ForwardController.java delete mode 100644 mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/ManualHandlerAdapter.java diff --git a/app/src/main/java/com/techcourse/DispatcherServlet.java b/app/src/main/java/com/techcourse/DispatcherServlet.java index a02adb8326..15e951f650 100644 --- a/app/src/main/java/com/techcourse/DispatcherServlet.java +++ b/app/src/main/java/com/techcourse/DispatcherServlet.java @@ -9,7 +9,6 @@ import webmvc.org.springframework.web.servlet.ModelAndView; import webmvc.org.springframework.web.servlet.mvc.tobe.AnnotationHandlerAdapter; import webmvc.org.springframework.web.servlet.mvc.tobe.AnnotationHandlerMapping; -import webmvc.org.springframework.web.servlet.mvc.tobe.ManualHandlerAdapter; public class DispatcherServlet extends HttpServlet { @@ -26,11 +25,9 @@ public DispatcherServlet() { public void init() { handlerMappings = new HandlerMappings(); handlerMappings.addHandlerMapping(new AnnotationHandlerMapping("com.techcourse.controller")); - handlerMappings.addHandlerMapping(new ManualHandlerMapping()); handlerAdapters = new HandlerAdapters(); handlerAdapters.addHandlerAdapter(new AnnotationHandlerAdapter()); - handlerAdapters.addHandlerAdapter(new ManualHandlerAdapter()); } @Override diff --git a/app/src/main/java/com/techcourse/ManualHandlerMapping.java b/app/src/main/java/com/techcourse/ManualHandlerMapping.java deleted file mode 100644 index 8c7528d248..0000000000 --- a/app/src/main/java/com/techcourse/ManualHandlerMapping.java +++ /dev/null @@ -1,44 +0,0 @@ -package com.techcourse; - -import com.techcourse.controller.LoginController; -import com.techcourse.controller.LoginViewController; -import com.techcourse.controller.LogoutController; -import com.techcourse.controller.RegisterController; -import com.techcourse.controller.RegisterViewController; -import jakarta.servlet.http.HttpServletRequest; -import java.util.HashMap; -import java.util.Map; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import webmvc.org.springframework.web.servlet.mvc.asis.Controller; -import webmvc.org.springframework.web.servlet.mvc.asis.ForwardController; -import webmvc.org.springframework.web.servlet.mvc.tobe.HandlerMapping; - -public class ManualHandlerMapping implements HandlerMapping { - - private static final Logger log = LoggerFactory.getLogger(ManualHandlerMapping.class); - - private final Map controllers = new HashMap<>(); - - @Override - public void initialize() { - controllers.put("/", new ForwardController("/index.jsp")); - controllers.put("/login", new LoginController()); - controllers.put("/login/view", new LoginViewController()); - controllers.put("/logout", new LogoutController()); - controllers.put("/register/view", new RegisterViewController()); - controllers.put("/register", new RegisterController()); - - log.info("Initialized Manual Handler Mapping!"); - controllers.keySet() - .forEach(path -> log.info("Path : {}, Controller : {}", path, controllers.get(path).getClass())); - } - - @Override - public Object getHandler(HttpServletRequest request) { - log.debug("Request Mapping Uri : {}", request.getRequestURI()); - - return controllers.get(request.getRequestURI()); - } - -} 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..dbc3685182 --- /dev/null +++ b/app/src/main/java/com/techcourse/controller/IndexController.java @@ -0,0 +1,19 @@ +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 index(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 0428fe109e..57aeeeb107 100644 --- a/app/src/main/java/com/techcourse/controller/LoginController.java +++ b/app/src/main/java/com/techcourse/controller/LoginController.java @@ -2,36 +2,52 @@ 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 webmvc.org.springframework.web.servlet.mvc.asis.Controller; 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.JspView; -public class LoginController implements Controller { +@Controller +public class LoginController { private static final Logger log = LoggerFactory.getLogger(LoginController.class); - @Override - public String execute(final HttpServletRequest req, final HttpServletResponse res) throws Exception { + @RequestMapping(value = "/login", method = RequestMethod.POST) + public ModelAndView login(final HttpServletRequest req, final HttpServletResponse res) throws Exception { if (UserSession.isLoggedIn(req.getSession())) { - return "redirect:/index.jsp"; + return new ModelAndView(new JspView("redirect:/index.jsp")); } return InMemoryUserRepository.findByAccount(req.getParameter("account")) .map(user -> { log.info("User : {}", user); - return login(req, user); + return redirectAfterCheckAccount(req, user); }) - .orElse("redirect:/401.jsp"); + .orElse(new ModelAndView(new JspView("redirect:/401.jsp"))); } - private String login(final HttpServletRequest request, final User user) { + private ModelAndView redirectAfterCheckAccount(final HttpServletRequest request, final User user) { if (user.checkPassword(request.getParameter("password"))) { final var session = request.getSession(); session.setAttribute(UserSession.SESSION_KEY, user); - return "redirect:/index.jsp"; + return new ModelAndView(new JspView("redirect:/index.jsp")); } - return "redirect:/401.jsp"; + return new ModelAndView(new JspView("redirect:/401.jsp")); } + + @RequestMapping(value = "/login", method = RequestMethod.GET) + public ModelAndView loginPage(final HttpServletRequest req, final HttpServletResponse res) throws Exception { + return UserSession.getUserFrom(req.getSession()) + .map(user -> { + log.info("logged in {}", user.getAccount()); + return new ModelAndView(new JspView("redirect:/index.jsp")); + }) + .orElse(new ModelAndView(new JspView("/login.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..f44208de5a 100644 --- a/app/src/main/java/com/techcourse/controller/LogoutController.java +++ b/app/src/main/java/com/techcourse/controller/LogoutController.java @@ -1,15 +1,21 @@ 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 { + @RequestMapping(value = "/logout", method = RequestMethod.GET) + public ModelAndView logout(final HttpServletRequest req, final HttpServletResponse res) throws Exception { final var session = req.getSession(); session.removeAttribute(UserSession.SESSION_KEY); - return "redirect:/"; + return new ModelAndView(new JspView("redirect:/")); } + } diff --git a/app/src/main/java/com/techcourse/controller/RegisterAnnotationController.java b/app/src/main/java/com/techcourse/controller/RegisterAnnotationController.java deleted file mode 100644 index a1a311d665..0000000000 --- a/app/src/main/java/com/techcourse/controller/RegisterAnnotationController.java +++ /dev/null @@ -1,39 +0,0 @@ -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 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 RegisterAnnotationController { - - @RequestMapping(value = "/register", method = RequestMethod.POST) - public ModelAndView save(HttpServletRequest req, HttpServletResponse res) { - User user = new User( - 2, - req.getParameter("account"), - req.getParameter("password"), - req.getParameter("email") - ); - - InMemoryUserRepository.save(user); - - return new ModelAndView( - new JspView("redirect:/index.jsp") - ); - } - - @RequestMapping(value = "/register", method = RequestMethod.GET) - public ModelAndView show(HttpServletRequest req, HttpServletResponse res) { - return new ModelAndView( - new JspView("/register.jsp") - ); - } - -} diff --git a/app/src/main/java/com/techcourse/controller/RegisterController.java b/app/src/main/java/com/techcourse/controller/RegisterController.java index ec0ac5c0be..dcc2f49f7b 100644 --- a/app/src/main/java/com/techcourse/controller/RegisterController.java +++ b/app/src/main/java/com/techcourse/controller/RegisterController.java @@ -2,14 +2,19 @@ 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 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 RegisterController implements Controller { +@Controller +public class RegisterController { - @Override - public String execute(final HttpServletRequest req, final HttpServletResponse res) throws Exception { + @RequestMapping(value = "/register", method = RequestMethod.POST) + public ModelAndView save(HttpServletRequest req, HttpServletResponse res) { User user = new User( 2, req.getParameter("account"), @@ -18,7 +23,17 @@ public String execute(final HttpServletRequest req, final HttpServletResponse re ); InMemoryUserRepository.save(user); - return "redirect:/index.jsp"; + + return new ModelAndView( + new JspView("redirect:/index.jsp") + ); + } + + @RequestMapping(value = "/register", method = RequestMethod.GET) + public ModelAndView show(HttpServletRequest req, HttpServletResponse res) { + 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 328e984d75..0000000000 --- a/app/src/main/java/com/techcourse/controller/RegisterViewController.java +++ /dev/null @@ -1,14 +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 index 4396b55255..419025db56 100644 --- a/app/src/main/java/com/techcourse/controller/UserController.java +++ b/app/src/main/java/com/techcourse/controller/UserController.java @@ -29,4 +29,5 @@ public ModelAndView show(HttpServletRequest request, HttpServletResponse respons modelAndView.addObject("user", user); return modelAndView; } + } \ No newline at end of file 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/register.jsp b/app/src/main/webapp/register.jsp index 7e68995a56..b42e5606be 100644 --- a/app/src/main/webapp/register.jsp +++ b/app/src/main/webapp/register.jsp @@ -36,7 +36,7 @@ diff --git a/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/asis/Controller.java b/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/asis/Controller.java deleted file mode 100644 index a1420222df..0000000000 --- a/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/asis/Controller.java +++ /dev/null @@ -1,10 +0,0 @@ -package webmvc.org.springframework.web.servlet.mvc.asis; - -import jakarta.servlet.http.HttpServletRequest; -import jakarta.servlet.http.HttpServletResponse; - -public interface Controller { - - String execute(final HttpServletRequest req, final HttpServletResponse res) throws Exception; - -} diff --git a/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/asis/ForwardController.java b/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/asis/ForwardController.java deleted file mode 100644 index cd8f1ef371..0000000000 --- a/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/asis/ForwardController.java +++ /dev/null @@ -1,20 +0,0 @@ -package webmvc.org.springframework.web.servlet.mvc.asis; - -import jakarta.servlet.http.HttpServletRequest; -import jakarta.servlet.http.HttpServletResponse; - -import java.util.Objects; - -public class ForwardController implements Controller { - - private final String path; - - public ForwardController(final String path) { - this.path = Objects.requireNonNull(path); - } - - @Override - public String execute(final HttpServletRequest request, final HttpServletResponse response) { - return path; - } -} diff --git a/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/ManualHandlerAdapter.java b/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/ManualHandlerAdapter.java deleted file mode 100644 index 92ddf3fc70..0000000000 --- a/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/ManualHandlerAdapter.java +++ /dev/null @@ -1,31 +0,0 @@ -package webmvc.org.springframework.web.servlet.mvc.tobe; - -import jakarta.servlet.http.HttpServletRequest; -import jakarta.servlet.http.HttpServletResponse; -import webmvc.org.springframework.web.servlet.ModelAndView; -import webmvc.org.springframework.web.servlet.mvc.asis.Controller; -import webmvc.org.springframework.web.servlet.view.JspView; - -public class ManualHandlerAdapter implements HandlerAdapter { - - @Override - public boolean isSupport(Object object) { - return object instanceof Controller; - } - - @Override - public ModelAndView handle( - Object object, - HttpServletRequest request, - HttpServletResponse response - ) { - try { - Controller controller = (Controller) object; - String viewName = controller.execute(request, response); - return new ModelAndView(new JspView(viewName)); - } catch (Exception exception) { - throw new IllegalArgumentException(exception); - } - } - -} From 91c41c807a94177a3c4aeea389c3b4fe7b33b6b7 Mon Sep 17 00:00:00 2001 From: jaeyeon kim Date: Sun, 24 Sep 2023 18:59:34 +0900 Subject: [PATCH 5/9] =?UTF-8?q?refactor=20:=20=ED=94=84=EB=A0=88=EC=9E=84?= =?UTF-8?q?=EC=9B=8C=ED=81=AC=20=EA=B4=80=EB=A0=A8=20=EC=BD=94=EB=93=9C,?= =?UTF-8?q?=20mvc=20=ED=8C=A8=ED=82=A4=EC=A7=80=EB=A1=9C=20=EC=9D=B4?= =?UTF-8?q?=EB=8F=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/techcourse/DispatcherServletInitializer.java | 1 + .../java/com/techcourse/controller/LoginController.java | 2 +- .../web/org/springframework/web}/DispatcherServlet.java | 6 ++++-- .../web/servlet/mvc/tobe/AnnotationHandlerMapping.java | 1 - .../web/servlet/mvc/tobe}/HandlerAdapters.java | 3 +-- .../web/servlet/mvc/tobe}/HandlerMappings.java | 3 +-- 6 files changed, 8 insertions(+), 8 deletions(-) rename {app/src/main/java/com/techcourse => mvc/src/main/java/web/org/springframework/web}/DispatcherServlet.java (84%) rename {app/src/main/java/com/techcourse => mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe}/HandlerAdapters.java (92%) rename {app/src/main/java/com/techcourse => mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe}/HandlerMappings.java (90%) diff --git a/app/src/main/java/com/techcourse/DispatcherServletInitializer.java b/app/src/main/java/com/techcourse/DispatcherServletInitializer.java index 6e814cdd25..c58f02ad6c 100644 --- a/app/src/main/java/com/techcourse/DispatcherServletInitializer.java +++ b/app/src/main/java/com/techcourse/DispatcherServletInitializer.java @@ -3,6 +3,7 @@ import jakarta.servlet.ServletContext; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import web.org.springframework.web.DispatcherServlet; import web.org.springframework.web.WebApplicationInitializer; /** diff --git a/app/src/main/java/com/techcourse/controller/LoginController.java b/app/src/main/java/com/techcourse/controller/LoginController.java index 57aeeeb107..3a3f17d790 100644 --- a/app/src/main/java/com/techcourse/controller/LoginController.java +++ b/app/src/main/java/com/techcourse/controller/LoginController.java @@ -41,7 +41,7 @@ private ModelAndView redirectAfterCheckAccount(final HttpServletRequest request, } @RequestMapping(value = "/login", method = RequestMethod.GET) - public ModelAndView loginPage(final HttpServletRequest req, final HttpServletResponse res) throws Exception { + public ModelAndView loginView(final HttpServletRequest req, final HttpServletResponse res) throws Exception { return UserSession.getUserFrom(req.getSession()) .map(user -> { log.info("logged in {}", user.getAccount()); diff --git a/app/src/main/java/com/techcourse/DispatcherServlet.java b/mvc/src/main/java/web/org/springframework/web/DispatcherServlet.java similarity index 84% rename from app/src/main/java/com/techcourse/DispatcherServlet.java rename to mvc/src/main/java/web/org/springframework/web/DispatcherServlet.java index 15e951f650..2ecbdb7af1 100644 --- a/app/src/main/java/com/techcourse/DispatcherServlet.java +++ b/mvc/src/main/java/web/org/springframework/web/DispatcherServlet.java @@ -1,4 +1,4 @@ -package com.techcourse; +package web.org.springframework.web; import jakarta.servlet.ServletException; import jakarta.servlet.http.HttpServlet; @@ -9,6 +9,8 @@ import webmvc.org.springframework.web.servlet.ModelAndView; import webmvc.org.springframework.web.servlet.mvc.tobe.AnnotationHandlerAdapter; import webmvc.org.springframework.web.servlet.mvc.tobe.AnnotationHandlerMapping; +import webmvc.org.springframework.web.servlet.mvc.tobe.HandlerAdapters; +import webmvc.org.springframework.web.servlet.mvc.tobe.HandlerMappings; public class DispatcherServlet extends HttpServlet { @@ -31,7 +33,7 @@ public void init() { } @Override - protected void service(final HttpServletRequest request, final HttpServletResponse response) throws ServletException { + protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException { log.debug("Method : {}, Request URI : {}", request.getMethod(), request.getRequestURI()); try { 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 index 3c393edf59..31378c0f13 100644 --- 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 @@ -68,7 +68,6 @@ private void putHandlerExecutionByAnnotation(Object controller, Method method) { } } - @Override public Object getHandler(HttpServletRequest request) { HandlerKey key = new HandlerKey( diff --git a/app/src/main/java/com/techcourse/HandlerAdapters.java b/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/HandlerAdapters.java similarity index 92% rename from app/src/main/java/com/techcourse/HandlerAdapters.java rename to mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/HandlerAdapters.java index 06e805f821..6c835f4dff 100644 --- a/app/src/main/java/com/techcourse/HandlerAdapters.java +++ b/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/HandlerAdapters.java @@ -1,4 +1,4 @@ -package com.techcourse; +package webmvc.org.springframework.web.servlet.mvc.tobe; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; @@ -6,7 +6,6 @@ import java.util.Collections; import java.util.List; import webmvc.org.springframework.web.servlet.ModelAndView; -import webmvc.org.springframework.web.servlet.mvc.tobe.HandlerAdapter; public class HandlerAdapters { diff --git a/app/src/main/java/com/techcourse/HandlerMappings.java b/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/HandlerMappings.java similarity index 90% rename from app/src/main/java/com/techcourse/HandlerMappings.java rename to mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/HandlerMappings.java index 85d53e711d..cd156b1670 100644 --- a/app/src/main/java/com/techcourse/HandlerMappings.java +++ b/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/HandlerMappings.java @@ -1,11 +1,10 @@ -package com.techcourse; +package webmvc.org.springframework.web.servlet.mvc.tobe; import jakarta.servlet.http.HttpServletRequest; import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Objects; -import webmvc.org.springframework.web.servlet.mvc.tobe.HandlerMapping; public class HandlerMappings { From 63223b3efe3b6c96f7554673a44fe00526dedc9f Mon Sep 17 00:00:00 2001 From: jaeyeon kim Date: Tue, 26 Sep 2023 12:14:46 +0900 Subject: [PATCH 6/9] =?UTF-8?q?refactor=20:=20JsonView=20=EC=97=90?= =?UTF-8?q?=EC=84=9C=20=EC=9D=91=EB=8B=B5=EC=9D=84=20=EB=B0=98=ED=99=98?= =?UTF-8?q?=ED=95=A0=20=EB=95=8C=20PrintWriter=20=EB=A5=BC=20=EC=82=AC?= =?UTF-8?q?=EC=9A=A9=ED=95=98=EC=97=AC=20=EB=B0=98=ED=99=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../springframework/web/servlet/view/JsonView.java | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) 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 4bc1509ac7..ae9acbb12f 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,26 +1,27 @@ package webmvc.org.springframework.web.servlet.view; import com.fasterxml.jackson.databind.ObjectMapper; -import jakarta.servlet.ServletOutputStream; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; +import java.io.PrintWriter; import java.util.Map; import web.org.springframework.http.MediaType; import webmvc.org.springframework.web.servlet.View; public class JsonView implements View { + private final ObjectMapper objectMapper = new ObjectMapper(); + @Override public void render( Map model, HttpServletRequest request, HttpServletResponse response ) throws Exception { - ObjectMapper objectMapper = new ObjectMapper(); - ServletOutputStream outputStream = response.getOutputStream(); - String responseBody = objectMapper.writeValueAsString(model); response.setContentType(MediaType.APPLICATION_JSON_UTF8_VALUE); - outputStream.write(responseBody.getBytes()); + String responseBody = objectMapper.writeValueAsString(model); + PrintWriter printWriter = new PrintWriter(response.getOutputStream()); + printWriter.write(responseBody); } } From 9f4c31d58298fa2816f89f0b5724781a1f83f90c Mon Sep 17 00:00:00 2001 From: jaeyeon kim Date: Tue, 26 Sep 2023 12:19:03 +0900 Subject: [PATCH 7/9] =?UTF-8?q?refactor=20:=20AnnotationHandlerMappingTest?= =?UTF-8?q?=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mvc/tobe/AnnotationHandlerMappingTest.java | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) 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 8e4472cdbc..e7e04b8f6e 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,7 @@ import jakarta.servlet.http.HttpServletResponse; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import webmvc.org.springframework.web.servlet.ModelAndView; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.mock; @@ -12,11 +13,13 @@ class AnnotationHandlerMappingTest { private AnnotationHandlerMapping handlerMapping; + private AnnotationHandlerAdapter annotationHandlerAdapter; @BeforeEach void setUp() { handlerMapping = new AnnotationHandlerMapping("samples"); handlerMapping.initialize(); + annotationHandlerAdapter = new AnnotationHandlerAdapter(); } @Test @@ -28,8 +31,8 @@ void get() throws Exception { when(request.getRequestURI()).thenReturn("/get-test"); when(request.getMethod()).thenReturn("GET"); - final var handlerExecution = handlerMapping.getHandler(request); - final var modelAndView = handlerExecution.handle(request, response); + final var handler = handlerMapping.getHandler(request); + final var modelAndView = annotationHandlerAdapter.handle(handler, request, response); assertThat(modelAndView.getObject("id")).isEqualTo("gugu"); } @@ -43,8 +46,8 @@ void post() throws Exception { when(request.getRequestURI()).thenReturn("/post-test"); when(request.getMethod()).thenReturn("POST"); - final var handlerExecution = handlerMapping.getHandler(request); - final var modelAndView = handlerExecution.handle(request, response); + final var handler = handlerMapping.getHandler(request); + final var modelAndView = annotationHandlerAdapter.handle(handler, request, response); assertThat(modelAndView.getObject("id")).isEqualTo("gugu"); } From e402442bf6d2d0f59f08195dbfd71b1e59a0e812 Mon Sep 17 00:00:00 2001 From: jaeyeon kim Date: Tue, 26 Sep 2023 12:29:14 +0900 Subject: [PATCH 8/9] =?UTF-8?q?refactor=20:=20PrintWriter=20=EB=A5=BC=20?= =?UTF-8?q?=EC=9D=B4=EC=9A=A9=ED=95=B4=20response=20=EC=9D=91=EB=8B=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../webmvc/org/springframework/web/servlet/view/JsonView.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) 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 ae9acbb12f..304e8bdf65 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 @@ -3,7 +3,6 @@ import com.fasterxml.jackson.databind.ObjectMapper; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; -import java.io.PrintWriter; import java.util.Map; import web.org.springframework.http.MediaType; import webmvc.org.springframework.web.servlet.View; @@ -20,8 +19,7 @@ public void render( ) throws Exception { response.setContentType(MediaType.APPLICATION_JSON_UTF8_VALUE); String responseBody = objectMapper.writeValueAsString(model); - PrintWriter printWriter = new PrintWriter(response.getOutputStream()); - printWriter.write(responseBody); + response.getWriter().write(responseBody); } } From 4823644414d94d44d62e332bd6276127e4b46dc6 Mon Sep 17 00:00:00 2001 From: jaeyeon kim Date: Tue, 26 Sep 2023 20:17:35 +0900 Subject: [PATCH 9/9] =?UTF-8?q?refactor=20:=20User=20=EC=97=90=20Getter=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80=20=EB=B0=8F=20Model=20=EC=9D=98=20size=20?= =?UTF-8?q?=EA=B0=80=201=20=EC=9D=B8=20=EA=B2=BD=EC=9A=B0=EB=8A=94=20?= =?UTF-8?q?=EB=82=B4=EB=B6=80=EC=9D=98=20=EA=B0=92=EC=9D=84=20=EA=BA=BC?= =?UTF-8?q?=EB=82=B4=EC=84=9C=20Json=20=EC=9C=BC=EB=A1=9C=20=EB=B3=80?= =?UTF-8?q?=ED=99=98=ED=95=98=EB=8F=84=EB=A1=9D=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/src/main/java/com/techcourse/domain/User.java | 12 ++++++++++++ .../springframework/web/servlet/view/JsonView.java | 13 ++++++++++++- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/com/techcourse/domain/User.java b/app/src/main/java/com/techcourse/domain/User.java index beb0919b7e..fd7d45dde8 100644 --- a/app/src/main/java/com/techcourse/domain/User.java +++ b/app/src/main/java/com/techcourse/domain/User.java @@ -22,6 +22,18 @@ public String getAccount() { return account; } + public long getId() { + return id; + } + + public String getPassword() { + return password; + } + + public String getEmail() { + return email; + } + @Override public String toString() { return "User{" + 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 304e8bdf65..fa659870e7 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 @@ -18,8 +18,19 @@ public void render( HttpServletResponse response ) throws Exception { response.setContentType(MediaType.APPLICATION_JSON_UTF8_VALUE); - String responseBody = objectMapper.writeValueAsString(model); + String responseBody = modelToJson(model); response.getWriter().write(responseBody); } + private String modelToJson(final Map model) throws Exception { + if (model.size() == 1) { + Object next = model.values() + .iterator() + .next(); + return objectMapper.writeValueAsString(next); + } + + return objectMapper.writeValueAsString(model); + } + }