From 9f3b23de445a2848e0384bbfae26ee5cfeee0f07 Mon Sep 17 00:00:00 2001 From: java-saeng Date: Mon, 18 Sep 2023 14:32:35 +0900 Subject: [PATCH 01/11] =?UTF-8?q?feat=20:=20HandlerMapping=20=EC=9D=B8?= =?UTF-8?q?=ED=84=B0=ED=8E=98=EC=9D=B4=EC=8A=A4=EB=A5=BC=20=EC=83=9D?= =?UTF-8?q?=EC=84=B1=ED=95=98=EC=97=AC=20HandlerMapping=20=EC=97=AD?= =?UTF-8?q?=ED=95=A0=EC=9D=84=20=EA=B0=80=EC=A7=84=20=ED=81=B4=EB=9E=98?= =?UTF-8?q?=EC=8A=A4=EB=93=A4=EC=9D=84=20=EA=B5=AC=ED=98=84=ED=95=98?= =?UTF-8?q?=EB=8F=84=EB=A1=9D=20=ED=95=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/com/techcourse/DispatcherServlet.java | 3 ++- app/src/main/java/com/techcourse/HandlerMapping.java | 10 ++++++++++ .../main/java/com/techcourse/ManualHandlerMapping.java | 8 ++++++-- 3 files changed, 18 insertions(+), 3 deletions(-) create mode 100644 app/src/main/java/com/techcourse/HandlerMapping.java diff --git a/app/src/main/java/com/techcourse/DispatcherServlet.java b/app/src/main/java/com/techcourse/DispatcherServlet.java index 277d8eed9a..e78ff46147 100644 --- a/app/src/main/java/com/techcourse/DispatcherServlet.java +++ b/app/src/main/java/com/techcourse/DispatcherServlet.java @@ -6,6 +6,7 @@ import jakarta.servlet.http.HttpServletResponse; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import webmvc.org.springframework.web.servlet.mvc.asis.Controller; import webmvc.org.springframework.web.servlet.view.JspView; public class DispatcherServlet extends HttpServlet { @@ -30,7 +31,7 @@ protected void service(final HttpServletRequest request, final HttpServletRespon log.debug("Method : {}, Request URI : {}", request.getMethod(), requestURI); try { - final var controller = manualHandlerMapping.getHandler(requestURI); + final var controller = (Controller) manualHandlerMapping.getHandler(request); final var viewName = controller.execute(request, response); move(viewName, request, response); } catch (Throwable e) { diff --git a/app/src/main/java/com/techcourse/HandlerMapping.java b/app/src/main/java/com/techcourse/HandlerMapping.java new file mode 100644 index 0000000000..8b8a5ad338 --- /dev/null +++ b/app/src/main/java/com/techcourse/HandlerMapping.java @@ -0,0 +1,10 @@ +package com.techcourse; + +import jakarta.servlet.http.HttpServletRequest; + +public interface HandlerMapping { + + void initialize(); + + Object getHandler(final HttpServletRequest httpServletRequest); +} diff --git a/app/src/main/java/com/techcourse/ManualHandlerMapping.java b/app/src/main/java/com/techcourse/ManualHandlerMapping.java index a54863caf8..3c25be0e43 100644 --- a/app/src/main/java/com/techcourse/ManualHandlerMapping.java +++ b/app/src/main/java/com/techcourse/ManualHandlerMapping.java @@ -1,6 +1,7 @@ package com.techcourse; import com.techcourse.controller.*; +import jakarta.servlet.http.HttpServletRequest; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import webmvc.org.springframework.web.servlet.mvc.asis.Controller; @@ -9,12 +10,13 @@ import java.util.HashMap; import java.util.Map; -public class ManualHandlerMapping { +public class ManualHandlerMapping implements HandlerMapping { private static final Logger log = LoggerFactory.getLogger(ManualHandlerMapping.class); private static final Map controllers = new HashMap<>(); + @Override public void initialize() { controllers.put("/", new ForwardController("/index.jsp")); controllers.put("/login", new LoginController()); @@ -28,7 +30,9 @@ public void initialize() { .forEach(path -> log.info("Path : {}, Controller : {}", path, controllers.get(path).getClass())); } - public Controller getHandler(final String requestURI) { + @Override + public Object getHandler(final HttpServletRequest httpServletRequest) { + final String requestURI = httpServletRequest.getRequestURI(); log.debug("Request Mapping Uri : {}", requestURI); return controllers.get(requestURI); } From f4af913460e59af7d67f3ae45b1f7a7367a1cdac Mon Sep 17 00:00:00 2001 From: java-saeng Date: Mon, 18 Sep 2023 14:41:39 +0900 Subject: [PATCH 02/11] =?UTF-8?q?feat=20:=20HandlerMapping=20=EC=9D=84=20?= =?UTF-8?q?=EA=B0=80=EC=A7=80=EA=B3=A0=20=EC=9E=88=EB=8A=94=20=EC=9D=BC?= =?UTF-8?q?=EA=B8=89=20=EC=BB=AC=EB=A0=89=EC=85=98=20=EC=83=9D=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/techcourse/DispatcherServlet.java | 76 ++++++++++--------- .../java/com/techcourse/HandlerMappings.java | 27 +++++++ 2 files changed, 69 insertions(+), 34 deletions(-) create mode 100644 app/src/main/java/com/techcourse/HandlerMappings.java diff --git a/app/src/main/java/com/techcourse/DispatcherServlet.java b/app/src/main/java/com/techcourse/DispatcherServlet.java index e78ff46147..bbe16ad2b2 100644 --- a/app/src/main/java/com/techcourse/DispatcherServlet.java +++ b/app/src/main/java/com/techcourse/DispatcherServlet.java @@ -4,6 +4,7 @@ import jakarta.servlet.http.HttpServlet; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; +import java.util.List; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import webmvc.org.springframework.web.servlet.mvc.asis.Controller; @@ -11,42 +12,49 @@ public class DispatcherServlet extends HttpServlet { - private static final long serialVersionUID = 1L; - private static final Logger log = LoggerFactory.getLogger(DispatcherServlet.class); - - private ManualHandlerMapping manualHandlerMapping; - - public DispatcherServlet() { - } - - @Override - public void init() { - manualHandlerMapping = new ManualHandlerMapping(); - manualHandlerMapping.initialize(); + private static final long serialVersionUID = 1L; + private static final Logger log = LoggerFactory.getLogger(DispatcherServlet.class); + + private HandlerMapping handlerMappingComposite; + + public DispatcherServlet() { + } + + @Override + public void init() { + handlerMappingComposite = new HandlerMappings( + List.of( + new ManualHandlerMapping() + ) + ); + + handlerMappingComposite.initialize(); + } + + @Override + protected void service(final HttpServletRequest request, final HttpServletResponse response) + throws ServletException { + final String requestURI = request.getRequestURI(); + log.debug("Method : {}, Request URI : {}", request.getMethod(), requestURI); + + try { + final Controller controller = (Controller) handlerMappingComposite.getHandler(request); + final var viewName = controller.execute(request, response); + move(viewName, request, response); + } catch (Throwable e) { + log.error("Exception : {}", e.getMessage(), e); + throw new ServletException(e.getMessage()); } + } - @Override - protected void service(final HttpServletRequest request, final HttpServletResponse response) throws ServletException { - final String requestURI = request.getRequestURI(); - log.debug("Method : {}, Request URI : {}", request.getMethod(), requestURI); - - try { - final var controller = (Controller) manualHandlerMapping.getHandler(request); - final var viewName = controller.execute(request, response); - move(viewName, request, response); - } catch (Throwable e) { - log.error("Exception : {}", e.getMessage(), e); - throw new ServletException(e.getMessage()); - } + private void move(final String viewName, final HttpServletRequest request, + final HttpServletResponse response) throws Exception { + if (viewName.startsWith(JspView.REDIRECT_PREFIX)) { + response.sendRedirect(viewName.substring(JspView.REDIRECT_PREFIX.length())); + return; } - private void move(final String viewName, final HttpServletRequest request, final 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); - } + final var requestDispatcher = request.getRequestDispatcher(viewName); + requestDispatcher.forward(request, response); + } } diff --git a/app/src/main/java/com/techcourse/HandlerMappings.java b/app/src/main/java/com/techcourse/HandlerMappings.java new file mode 100644 index 0000000000..2dd3d96a15 --- /dev/null +++ b/app/src/main/java/com/techcourse/HandlerMappings.java @@ -0,0 +1,27 @@ +package com.techcourse; + +import jakarta.servlet.http.HttpServletRequest; +import java.util.List; + +public class HandlerMappings implements HandlerMapping { + + private final List handlerMappings; + + public HandlerMappings(final List handlerMappings) { + this.handlerMappings = handlerMappings; + } + + @Override + public void initialize() { + handlerMappings.forEach(HandlerMapping::initialize); + } + + @Override + public Object getHandler(final HttpServletRequest httpServletRequest) { + for (final HandlerMapping handlerMapping : handlerMappings) { + return handlerMapping.getHandler(httpServletRequest); + } + + throw new IllegalArgumentException("요청을 처리할 수 있는 Handler가 존재하지 않습니다."); + } +} From 2d15844ad1cd5c4dbb2cb08e3077fb269f08cf1e Mon Sep 17 00:00:00 2001 From: java-saeng Date: Mon, 18 Sep 2023 15:40:55 +0900 Subject: [PATCH 03/11] =?UTF-8?q?refactor=20:=20HandlerMapping=20=ED=8C=A8?= =?UTF-8?q?=ED=82=A4=EC=A7=80=20=EC=9D=B4=EB=8F=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/src/main/java/com/techcourse/DispatcherServlet.java | 2 ++ app/src/main/java/com/techcourse/ManualHandlerMapping.java | 1 + .../springframework/web/servlet/mvc/tobe}/HandlerMapping.java | 2 +- .../springframework/web/servlet/mvc/tobe}/HandlerMappings.java | 3 ++- 4 files changed, 6 insertions(+), 2 deletions(-) rename {app/src/main/java/com/techcourse => mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe}/HandlerMapping.java (75%) rename {app/src/main/java/com/techcourse => mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe}/HandlerMappings.java (85%) diff --git a/app/src/main/java/com/techcourse/DispatcherServlet.java b/app/src/main/java/com/techcourse/DispatcherServlet.java index bbe16ad2b2..43eaa2d3ef 100644 --- a/app/src/main/java/com/techcourse/DispatcherServlet.java +++ b/app/src/main/java/com/techcourse/DispatcherServlet.java @@ -8,6 +8,8 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import webmvc.org.springframework.web.servlet.mvc.asis.Controller; +import webmvc.org.springframework.web.servlet.mvc.tobe.HandlerMapping; +import webmvc.org.springframework.web.servlet.mvc.tobe.HandlerMappings; import webmvc.org.springframework.web.servlet.view.JspView; public class DispatcherServlet extends HttpServlet { diff --git a/app/src/main/java/com/techcourse/ManualHandlerMapping.java b/app/src/main/java/com/techcourse/ManualHandlerMapping.java index 3c25be0e43..5afc9affa7 100644 --- a/app/src/main/java/com/techcourse/ManualHandlerMapping.java +++ b/app/src/main/java/com/techcourse/ManualHandlerMapping.java @@ -9,6 +9,7 @@ import java.util.HashMap; import java.util.Map; +import webmvc.org.springframework.web.servlet.mvc.tobe.HandlerMapping; public class ManualHandlerMapping implements HandlerMapping { diff --git a/app/src/main/java/com/techcourse/HandlerMapping.java b/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/HandlerMapping.java similarity index 75% rename from app/src/main/java/com/techcourse/HandlerMapping.java rename to mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/HandlerMapping.java index 8b8a5ad338..f2603d0137 100644 --- a/app/src/main/java/com/techcourse/HandlerMapping.java +++ b/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/HandlerMapping.java @@ -1,4 +1,4 @@ -package com.techcourse; +package webmvc.org.springframework.web.servlet.mvc.tobe; import jakarta.servlet.http.HttpServletRequest; 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 85% 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 2dd3d96a15..155ee41e2d 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,7 +1,8 @@ -package com.techcourse; +package webmvc.org.springframework.web.servlet.mvc.tobe; import jakarta.servlet.http.HttpServletRequest; import java.util.List; +import webmvc.org.springframework.web.servlet.mvc.tobe.HandlerMapping; public class HandlerMappings implements HandlerMapping { From 2a5157574759ac497161b370a4f9b4e0f8f7604e Mon Sep 17 00:00:00 2001 From: java-saeng Date: Mon, 18 Sep 2023 16:26:13 +0900 Subject: [PATCH 04/11] =?UTF-8?q?feat=20:=20HandlerAdapter=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/techcourse/DispatcherServlet.java | 33 ++++++++++++++++--- .../mvc/tobe/AnnotationHandlerAdapter.java | 21 ++++++++++++ .../mvc/tobe/AnnotationHandlerMapping.java | 23 ++++++++++--- .../web/servlet/mvc/tobe/HandlerAdapter.java | 15 +++++++++ .../web/servlet/mvc/tobe/HandlerAdapters.java | 32 ++++++++++++++++++ .../servlet/mvc/tobe/HandlerExecution.java | 8 +++-- .../mvc/tobe/ManualHandlerAdapter.java | 26 +++++++++++++++ 7 files changed, 147 insertions(+), 11 deletions(-) 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/HandlerAdapters.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 43eaa2d3ef..f1f2671a49 100644 --- a/app/src/main/java/com/techcourse/DispatcherServlet.java +++ b/app/src/main/java/com/techcourse/DispatcherServlet.java @@ -7,9 +7,15 @@ import java.util.List; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import webmvc.org.springframework.web.servlet.ModelAndView; import webmvc.org.springframework.web.servlet.mvc.asis.Controller; +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.HandlerAdapter; +import webmvc.org.springframework.web.servlet.mvc.tobe.HandlerAdapters; import webmvc.org.springframework.web.servlet.mvc.tobe.HandlerMapping; import webmvc.org.springframework.web.servlet.mvc.tobe.HandlerMappings; +import webmvc.org.springframework.web.servlet.mvc.tobe.ManualHandlerAdapter; import webmvc.org.springframework.web.servlet.view.JspView; public class DispatcherServlet extends HttpServlet { @@ -18,6 +24,7 @@ public class DispatcherServlet extends HttpServlet { private static final Logger log = LoggerFactory.getLogger(DispatcherServlet.class); private HandlerMapping handlerMappingComposite; + private HandlerAdapter handlerAdapterComposite; public DispatcherServlet() { } @@ -26,11 +33,18 @@ public DispatcherServlet() { public void init() { handlerMappingComposite = new HandlerMappings( List.of( - new ManualHandlerMapping() + new ManualHandlerMapping(), + new AnnotationHandlerMapping() ) ); - handlerMappingComposite.initialize(); + + handlerAdapterComposite = new HandlerAdapters( + List.of( + new ManualHandlerAdapter(), + new AnnotationHandlerAdapter() + ) + ); } @Override @@ -40,9 +54,18 @@ protected void service(final HttpServletRequest request, final HttpServletRespon log.debug("Method : {}, Request URI : {}", request.getMethod(), requestURI); try { - final Controller controller = (Controller) handlerMappingComposite.getHandler(request); - final var viewName = controller.execute(request, response); - move(viewName, request, response); + final Object handler = handlerMappingComposite.getHandler(request); + final Object handleValue = handlerAdapterComposite.handle(request, response, handler); + + if (handleValue instanceof ModelAndView) { + // TODO : 3단계 리팩터링 + } + + if (handleValue instanceof String) { + final String viewName = (String) handleValue; + move(viewName, request, response); + } + } catch (Throwable e) { log.error("Exception : {}", e.getMessage(), e); throw new ServletException(e.getMessage()); 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..56e1b088a6 --- /dev/null +++ b/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/AnnotationHandlerAdapter.java @@ -0,0 +1,21 @@ +package webmvc.org.springframework.web.servlet.mvc.tobe; + +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; + +public class AnnotationHandlerAdapter implements HandlerAdapter { + + @Override + public boolean isSupport(final Object handler) { + return handler instanceof HandlerExecution; + } + + @Override + public Object handle( + final HttpServletRequest request, + final HttpServletResponse response, + final Object handler + ) { + return ((HandlerExecution) handler).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 30924cde17..d1002a9688 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 @@ -14,7 +14,7 @@ import web.org.springframework.web.bind.annotation.RequestMapping; import web.org.springframework.web.bind.annotation.RequestMethod; -public class AnnotationHandlerMapping { +public class AnnotationHandlerMapping implements HandlerMapping { private static final Logger log = LoggerFactory.getLogger(AnnotationHandlerMapping.class); @@ -26,8 +26,8 @@ public AnnotationHandlerMapping(final Object... basePackages) { this.handlerExecutions = new HashMap<>(); } - public void initialize() - throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException { + @Override + public void initialize() { log.info("Initialized AnnotationHandlerMapping!"); final Reflections reflections = new Reflections(basePackages); @@ -36,7 +36,7 @@ public void initialize() reflections.getTypesAnnotatedWith(Controller.class); for (Class controllerAnnotationClass : controllerAnnotationClasses) { - final Object instance = controllerAnnotationClass.getDeclaredConstructor().newInstance(); + final Object instance = getControllerInstance(controllerAnnotationClass); final Method[] methods = controllerAnnotationClass.getDeclaredMethods(); Arrays.stream(methods) @@ -45,6 +45,20 @@ public void initialize() } } + private Object getControllerInstance(final Class controllerAnnotationClass) { + final Object instance; + try { + instance = controllerAnnotationClass.getDeclaredConstructor().newInstance(); + } catch (InstantiationException + | IllegalAccessException + | InvocationTargetException + | NoSuchMethodException e + ) { + throw new IllegalArgumentException("AnnotationHandlerMapping Reflection Exception"); + } + return instance; + } + private void putHandlerExecutions(final Method method, final Object target) { final RequestMapping requestMapping = method.getAnnotation(RequestMapping.class); @@ -55,6 +69,7 @@ private void putHandlerExecutions(final Method method, final Object target) { } } + @Override public Object getHandler(final HttpServletRequest request) { final String requestURI = request.getRequestURI(); final String method = request.getMethod(); 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..b9be799e7d --- /dev/null +++ b/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/HandlerAdapter.java @@ -0,0 +1,15 @@ +package webmvc.org.springframework.web.servlet.mvc.tobe; + +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; + +public interface HandlerAdapter { + + boolean isSupport(final Object handler); + + Object handle( + final HttpServletRequest request, + final HttpServletResponse response, + final Object handler + ); +} diff --git a/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/HandlerAdapters.java b/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/HandlerAdapters.java new file mode 100644 index 0000000000..4cc00caab8 --- /dev/null +++ b/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/HandlerAdapters.java @@ -0,0 +1,32 @@ +package webmvc.org.springframework.web.servlet.mvc.tobe; + +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import java.util.List; + +public class HandlerAdapters implements HandlerAdapter { + + private final List handlerAdapters; + + public HandlerAdapters(final List handlerAdapters) { + this.handlerAdapters = handlerAdapters; + } + + @Override + public boolean isSupport(final Object handler) { + throw new UnsupportedOperationException(); + } + + @Override + public Object handle( + final HttpServletRequest request, + final HttpServletResponse response, + final Object handler + ) { + return handlerAdapters.stream() + .filter(handlerAdapter -> handlerAdapter.isSupport(handler)) + .map(handlerAdapter -> handlerAdapter.handle(request, response, handler)) + .findAny() + .orElseThrow(() -> new IllegalArgumentException("해당 요청은 처리할 수 없습니다.")); + } +} 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 468e7ce274..3ec99dfd7a 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 @@ -19,7 +19,11 @@ public HandlerExecution(final Method method, final Object target) { public ModelAndView handle( final HttpServletRequest request, final HttpServletResponse response - ) throws InvocationTargetException, IllegalAccessException { - return (ModelAndView) method.invoke(target, request, response); + ) { + try { + return (ModelAndView) method.invoke(target, request, response); + } catch (IllegalAccessException | InvocationTargetException e) { + throw new IllegalArgumentException("Reflection Exception"); + } } } 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..6e569a9b5e --- /dev/null +++ b/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/ManualHandlerAdapter.java @@ -0,0 +1,26 @@ +package webmvc.org.springframework.web.servlet.mvc.tobe; + +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import webmvc.org.springframework.web.servlet.mvc.asis.Controller; + +public class ManualHandlerAdapter implements HandlerAdapter { + + @Override + public boolean isSupport(final Object handler) { + return handler instanceof Controller; + } + + @Override + public Object handle( + final HttpServletRequest request, + final HttpServletResponse response, + final Object handler + ) { + try { + return ((Controller) handler).execute(request, response); + } catch (Exception e) { + throw new IllegalArgumentException("ManualHandlerAdapter execute Exception"); + } + } +} From 2f59764bb69df63a577b2ef0b461be8318e3a0fe Mon Sep 17 00:00:00 2001 From: java-saeng Date: Mon, 18 Sep 2023 16:28:24 +0900 Subject: [PATCH 05/11] =?UTF-8?q?refactor=20:=20Handler=20=EC=B0=BE?= =?UTF-8?q?=EB=8A=94=20=EB=A9=94=EC=84=9C=EB=93=9C=20=EC=8A=A4=ED=8A=B8?= =?UTF-8?q?=EB=A6=BC=EC=9C=BC=EB=A1=9C=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../web/servlet/mvc/tobe/HandlerMappings.java | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/HandlerMappings.java b/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/HandlerMappings.java index 155ee41e2d..017aef3850 100644 --- a/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/HandlerMappings.java +++ b/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/HandlerMappings.java @@ -19,10 +19,9 @@ public void initialize() { @Override public Object getHandler(final HttpServletRequest httpServletRequest) { - for (final HandlerMapping handlerMapping : handlerMappings) { - return handlerMapping.getHandler(httpServletRequest); - } - - throw new IllegalArgumentException("요청을 처리할 수 있는 Handler가 존재하지 않습니다."); + return handlerMappings.stream() + .map(handlerMapping -> handlerMapping.getHandler(httpServletRequest)) + .findFirst() + .orElseThrow(() -> new IllegalArgumentException("요청을 처리할 수 있는 Handler가 존재하지 않습니다.")); } } From 151f2c11d5d58119a9d8dea5d7127b0cc52fdf5f Mon Sep 17 00:00:00 2001 From: java-saeng Date: Mon, 18 Sep 2023 16:34:29 +0900 Subject: [PATCH 06/11] =?UTF-8?q?refactor=20:=20=EB=A6=AC=ED=8C=A9?= =?UTF-8?q?=ED=84=B0=EB=A7=81=20=ED=95=A0=20=EB=B6=80=EB=B6=84=20=EB=A9=94?= =?UTF-8?q?=EC=84=9C=EB=93=9C=20extract?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/techcourse/DispatcherServlet.java | 24 ++++++++++++------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/com/techcourse/DispatcherServlet.java b/app/src/main/java/com/techcourse/DispatcherServlet.java index f1f2671a49..7f1c60dd3e 100644 --- a/app/src/main/java/com/techcourse/DispatcherServlet.java +++ b/app/src/main/java/com/techcourse/DispatcherServlet.java @@ -8,7 +8,6 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import webmvc.org.springframework.web.servlet.ModelAndView; -import webmvc.org.springframework.web.servlet.mvc.asis.Controller; 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.HandlerAdapter; @@ -57,14 +56,8 @@ protected void service(final HttpServletRequest request, final HttpServletRespon final Object handler = handlerMappingComposite.getHandler(request); final Object handleValue = handlerAdapterComposite.handle(request, response, handler); - if (handleValue instanceof ModelAndView) { - // TODO : 3단계 리팩터링 - } - - if (handleValue instanceof String) { - final String viewName = (String) handleValue; - move(viewName, request, response); - } + // TODO : 3단계 리팩터링 + renderByHandlerValueType(request, response, handleValue); } catch (Throwable e) { log.error("Exception : {}", e.getMessage(), e); @@ -72,6 +65,19 @@ protected void service(final HttpServletRequest request, final HttpServletRespon } } + private void renderByHandlerValueType( + final HttpServletRequest request, + final HttpServletResponse response, + final Object handleValue + ) throws Exception { + if (handleValue instanceof ModelAndView) {} + + if (handleValue instanceof String) { + final String viewName = (String) handleValue; + move(viewName, request, response); + } + } + private void move(final String viewName, final HttpServletRequest request, final HttpServletResponse response) throws Exception { if (viewName.startsWith(JspView.REDIRECT_PREFIX)) { From 9dde92c8dc3d651bda246d853bcaa8ceee7d31c4 Mon Sep 17 00:00:00 2001 From: java-saeng Date: Mon, 18 Sep 2023 16:54:46 +0900 Subject: [PATCH 07/11] =?UTF-8?q?test=20:=20HandlerAdaptersTest=20?= =?UTF-8?q?=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../servlet/mvc/tobe/HandlerAdaptersTest.java | 65 +++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 mvc/src/test/java/webmvc/org/springframework/web/servlet/mvc/tobe/HandlerAdaptersTest.java diff --git a/mvc/src/test/java/webmvc/org/springframework/web/servlet/mvc/tobe/HandlerAdaptersTest.java b/mvc/src/test/java/webmvc/org/springframework/web/servlet/mvc/tobe/HandlerAdaptersTest.java new file mode 100644 index 0000000000..a9a5d35efc --- /dev/null +++ b/mvc/src/test/java/webmvc/org/springframework/web/servlet/mvc/tobe/HandlerAdaptersTest.java @@ -0,0 +1,65 @@ +package webmvc.org.springframework.web.servlet.mvc.tobe; + +import static org.assertj.core.api.Assertions.*; +import static org.mockito.Mockito.mock; + +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import java.lang.reflect.Method; +import java.util.List; +import org.assertj.core.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import samples.TestController; +import webmvc.org.springframework.web.servlet.ModelAndView; +import webmvc.org.springframework.web.servlet.mvc.asis.Controller; +import webmvc.org.springframework.web.servlet.mvc.asis.ForwardController; + +class HandlerAdaptersTest { + + private HandlerAdapter handlerAdapters; + private HttpServletRequest request; + private HttpServletResponse response; + + @BeforeEach + void setUp() { + handlerAdapters = new HandlerAdapters( + List.of( + new ManualHandlerAdapter(), + new AnnotationHandlerAdapter() + ) + ); + + request = mock(HttpServletRequest.class); + response = mock(HttpServletResponse.class); + } + + @Test + @DisplayName("handle() : Controller Interface의 adapter를 찾아 실행할 수 있다.") + void test_handle_controller_interface() throws Exception { + //given + Controller controller = new ForwardController("path"); + + //when + final Object handle = handlerAdapters.handle(request, response, controller); + + //then + assertThat(handle).isInstanceOf(String.class); + } + + @Test + @DisplayName("handle() : 어노테이션 기반의 adapter를 찾아 실행할 수 있다.") + void test_handle_annotation() throws Exception { + //given + final TestController controller = new TestController(); + final HandlerExecution handlerExecution = new HandlerExecution(controller.getClass() + .getMethod("findUserId", HttpServletRequest.class, HttpServletResponse.class), controller); + + //when + final Object handle = handlerAdapters.handle(request, response, handlerExecution); + + //then + assertThat(handle).isInstanceOf(ModelAndView.class); + } +} From 68a770820b9eebcec1e7a1516657270c031ee584 Mon Sep 17 00:00:00 2001 From: java-saeng Date: Mon, 18 Sep 2023 19:15:26 +0900 Subject: [PATCH 08/11] =?UTF-8?q?refactor=20:=20RegisterViewController,=20?= =?UTF-8?q?RegisterController=20=EB=A5=BC=20=EC=96=B4=EB=85=B8=ED=85=8C?= =?UTF-8?q?=EC=9D=B4=EC=85=98=EC=9C=BC=EB=A1=9C=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/RegisterController.java | 16 +++++++++----- .../controller/RegisterViewController.java | 21 ++++++++++++++----- 2 files changed, 27 insertions(+), 10 deletions(-) diff --git a/app/src/main/java/com/techcourse/controller/RegisterController.java b/app/src/main/java/com/techcourse/controller/RegisterController.java index da62e5e8e9..7523c66cb7 100644 --- a/app/src/main/java/com/techcourse/controller/RegisterController.java +++ b/app/src/main/java/com/techcourse/controller/RegisterController.java @@ -2,20 +2,26 @@ 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 execute(final HttpServletRequest req, final HttpServletResponse res) throws Exception { final var user = new User(2, req.getParameter("account"), req.getParameter("password"), req.getParameter("email")); InMemoryUserRepository.save(user); - return "redirect:/index.jsp"; + final ModelAndView modelAndView = new ModelAndView(new JspView("redirect:/index.jsp")); + return modelAndView; } } diff --git a/app/src/main/java/com/techcourse/controller/RegisterViewController.java b/app/src/main/java/com/techcourse/controller/RegisterViewController.java index 136962136d..2f190be97c 100644 --- a/app/src/main/java/com/techcourse/controller/RegisterViewController.java +++ b/app/src/main/java/com/techcourse/controller/RegisterViewController.java @@ -1,13 +1,24 @@ 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 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 RegisterViewController implements Controller { +@Controller +public class RegisterViewController { - @Override - public String execute(final HttpServletRequest req, final HttpServletResponse res) throws Exception { - return "/register.jsp"; + private static final Logger log = LoggerFactory.getLogger(RegisterViewController.class); + + @RequestMapping(value = "/register", method = RequestMethod.GET) + public ModelAndView execute(final HttpServletRequest req, final HttpServletResponse res) { + log.info("RegisterViewController GET method"); + + return new ModelAndView(new JspView("/register.jsp")); } } From 157eca18e9341897a3f791a60e26aa94e12d080f Mon Sep 17 00:00:00 2001 From: java-saeng Date: Mon, 18 Sep 2023 19:17:01 +0900 Subject: [PATCH 09/11] =?UTF-8?q?feat=20:=20=EA=B8=B0=EC=A1=B4=20interface?= =?UTF-8?q?=20=EA=B8=B0=EB=B0=98=20controller=EB=A5=BC=20annotation=20?= =?UTF-8?q?=ED=98=95=EC=8B=9D=EC=9C=BC=EB=A1=9C=20=EB=B3=80=EA=B2=BD?= =?UTF-8?q?=ED=95=9C=20=ED=9B=84=20DispatcherServlet=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/src/main/java/com/techcourse/DispatcherServlet.java | 7 +++++-- .../main/java/com/techcourse/ManualHandlerMapping.java | 2 -- .../java/webmvc/org/springframework/web/servlet/View.java | 2 ++ .../web/servlet/mvc/tobe/AnnotationHandlerMapping.java | 8 ++++++-- .../web/servlet/mvc/tobe/HandlerMappings.java | 5 +++-- .../org/springframework/web/servlet/view/JsonView.java | 5 +++++ .../org/springframework/web/servlet/view/JspView.java | 8 ++++++++ 7 files changed, 29 insertions(+), 8 deletions(-) diff --git a/app/src/main/java/com/techcourse/DispatcherServlet.java b/app/src/main/java/com/techcourse/DispatcherServlet.java index 7f1c60dd3e..3f4b8c1cfa 100644 --- a/app/src/main/java/com/techcourse/DispatcherServlet.java +++ b/app/src/main/java/com/techcourse/DispatcherServlet.java @@ -33,7 +33,7 @@ public void init() { handlerMappingComposite = new HandlerMappings( List.of( new ManualHandlerMapping(), - new AnnotationHandlerMapping() + new AnnotationHandlerMapping("com.techcourse") ) ); handlerMappingComposite.initialize(); @@ -70,7 +70,10 @@ private void renderByHandlerValueType( final HttpServletResponse response, final Object handleValue ) throws Exception { - if (handleValue instanceof ModelAndView) {} + if (handleValue instanceof ModelAndView) { + final ModelAndView modelAndView = (ModelAndView) handleValue; + move(modelAndView.getView().getName(), request, response); + } if (handleValue instanceof String) { final String viewName = (String) handleValue; diff --git a/app/src/main/java/com/techcourse/ManualHandlerMapping.java b/app/src/main/java/com/techcourse/ManualHandlerMapping.java index 5afc9affa7..ba14234b7e 100644 --- a/app/src/main/java/com/techcourse/ManualHandlerMapping.java +++ b/app/src/main/java/com/techcourse/ManualHandlerMapping.java @@ -23,8 +23,6 @@ public void initialize() { 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 Handler Mapping!"); controllers.keySet() 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 4499f36866..27833b3cd1 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 @@ -7,4 +7,6 @@ public interface View { void render(Map model, HttpServletRequest request, HttpServletResponse response) throws Exception; + + String getName(); } 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 d1002a9688..8fca3b6974 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 @@ -28,8 +28,6 @@ public AnnotationHandlerMapping(final Object... basePackages) { @Override public void initialize() { - log.info("Initialized AnnotationHandlerMapping!"); - final Reflections reflections = new Reflections(basePackages); final Set> controllerAnnotationClasses = @@ -43,6 +41,12 @@ public void initialize() { .filter(method -> method.isAnnotationPresent(RequestMapping.class)) .forEach(method -> putHandlerExecutions(method, instance)); } + + log.info("Initialized AnnotationHandlerMapping!"); + + handlerExecutions.keySet() + .forEach(handlerKey -> log.info("handlerKey : {}, Controller : {}", handlerKey, + handlerExecutions.get(handlerKey))); } private Object getControllerInstance(final Class controllerAnnotationClass) { diff --git a/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/HandlerMappings.java b/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/HandlerMappings.java index 017aef3850..db37cda2d7 100644 --- a/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/HandlerMappings.java +++ b/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/HandlerMappings.java @@ -2,7 +2,7 @@ import jakarta.servlet.http.HttpServletRequest; import java.util.List; -import webmvc.org.springframework.web.servlet.mvc.tobe.HandlerMapping; +import java.util.Objects; public class HandlerMappings implements HandlerMapping { @@ -21,7 +21,8 @@ public void initialize() { public Object getHandler(final HttpServletRequest httpServletRequest) { return handlerMappings.stream() .map(handlerMapping -> handlerMapping.getHandler(httpServletRequest)) - .findFirst() + .filter(Objects::nonNull) + .findAny() .orElseThrow(() -> new IllegalArgumentException("요청을 처리할 수 있는 Handler가 존재하지 않습니다.")); } } 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..2c0ba3fc3b 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 @@ -11,4 +11,9 @@ public class JsonView implements View { @Override public void render(final Map model, final HttpServletRequest request, HttpServletResponse response) throws Exception { } + + @Override + public String getName() { + return null; + } } 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 3f4cc906ff..7a2f4c1397 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 @@ -14,7 +14,10 @@ public class JspView implements View { public static final String REDIRECT_PREFIX = "redirect:"; + private final String viewName; + public JspView(final String viewName) { + this.viewName = viewName; } @Override @@ -28,4 +31,9 @@ public void render(final Map model, final HttpServletRequest request, // todo } + + @Override + public String getName() { + return viewName; + } } From d298d5eb815fac0d5a40772a61687bc4992eb65a Mon Sep 17 00:00:00 2001 From: java-saeng Date: Mon, 18 Sep 2023 19:19:43 +0900 Subject: [PATCH 10/11] =?UTF-8?q?refactor=20:=20Enum=20valueOf=20=EC=82=AC?= =?UTF-8?q?=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../web/bind/annotation/RequestMethod.java | 9 ----- .../mvc/tobe/AnnotationHandlerMapping.java | 2 +- .../bind/annotation/RequestMethodTest.java | 34 ------------------- 3 files changed, 1 insertion(+), 44 deletions(-) delete mode 100644 mvc/src/test/java/web/org/springframework/web/bind/annotation/RequestMethodTest.java diff --git a/mvc/src/main/java/web/org/springframework/web/bind/annotation/RequestMethod.java b/mvc/src/main/java/web/org/springframework/web/bind/annotation/RequestMethod.java index 78f678cee3..ecc85590ab 100644 --- a/mvc/src/main/java/web/org/springframework/web/bind/annotation/RequestMethod.java +++ b/mvc/src/main/java/web/org/springframework/web/bind/annotation/RequestMethod.java @@ -1,7 +1,5 @@ package web.org.springframework.web.bind.annotation; -import java.util.Arrays; - public enum RequestMethod { GET, HEAD, @@ -12,11 +10,4 @@ public enum RequestMethod { OPTIONS, TRACE ; - - public static RequestMethod find(final String method) { - return Arrays.stream(values()) - .filter(it -> it.name().equals(method.toUpperCase())) - .findAny() - .orElseThrow(() -> new IllegalArgumentException("지원하지 않는 RequestMethod입니다.")); - } } 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 8fca3b6974..81259ede5a 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 @@ -78,7 +78,7 @@ public Object getHandler(final HttpServletRequest request) { final String requestURI = request.getRequestURI(); final String method = request.getMethod(); - final HandlerKey handlerKey = new HandlerKey(requestURI, RequestMethod.find(method)); + final HandlerKey handlerKey = new HandlerKey(requestURI, RequestMethod.valueOf(method)); return handlerExecutions.get(handlerKey); } diff --git a/mvc/src/test/java/web/org/springframework/web/bind/annotation/RequestMethodTest.java b/mvc/src/test/java/web/org/springframework/web/bind/annotation/RequestMethodTest.java deleted file mode 100644 index 111dab8950..0000000000 --- a/mvc/src/test/java/web/org/springframework/web/bind/annotation/RequestMethodTest.java +++ /dev/null @@ -1,34 +0,0 @@ -package web.org.springframework.web.bind.annotation; - -import static org.assertj.core.api.Assertions.assertThatThrownBy; -import static org.junit.jupiter.api.Assertions.assertEquals; - -import java.util.List; -import org.junit.jupiter.api.DisplayName; -import org.junit.jupiter.api.Test; - -class RequestMethodTest { - - @Test - @DisplayName("find() : String으로 받은 HTTP Method 를 ENUM RequestHttpMethod 찾을 수 있다.") - void test_find() throws Exception { - //given - final List methods = List.of("GET", "get", "Get", "GEt"); - - //when & then - for (String method : methods) { - assertEquals(RequestMethod.GET, RequestMethod.find(method)); - } - } - - @Test - @DisplayName("find() : RequestMethod에 없는 String method일 경우 IllegalArgumentException이 반환될 수 있다.") - void test_find_IllegalArgumentException() throws Exception { - //given - final String method = "POSTING"; - - //when & then - assertThatThrownBy(() -> RequestMethod.find(method)) - .isInstanceOf(IllegalArgumentException.class); - } -} From 3ccf2695d03c928efc94bb6f680021feb4bd29ba Mon Sep 17 00:00:00 2001 From: java-saeng Date: Thu, 21 Sep 2023 09:50:33 +0900 Subject: [PATCH 11/11] =?UTF-8?q?refactor=20:=20HandlerMappings=EB=A5=BC?= =?UTF-8?q?=20=EC=B4=88=EA=B8=B0=ED=99=94=ED=95=B4=EC=A3=BC=EB=8A=94=20?= =?UTF-8?q?=EC=BD=94=EB=93=9C=EB=A5=BC=20DispatcherServlet=EC=9D=B4=20?= =?UTF-8?q?=EC=95=84=EB=8B=8C=20HandlerMappings=20=EC=83=9D=EC=84=B1?= =?UTF-8?q?=EC=9E=90=EC=97=90=EC=84=9C=20=EC=88=98=ED=96=89=ED=95=98?= =?UTF-8?q?=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/DispatcherServlet.java | 1 - .../springframework/web/servlet/mvc/tobe/HandlerMappings.java | 2 ++ 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/com/techcourse/DispatcherServlet.java b/app/src/main/java/com/techcourse/DispatcherServlet.java index 3f4b8c1cfa..5ddb76d33f 100644 --- a/app/src/main/java/com/techcourse/DispatcherServlet.java +++ b/app/src/main/java/com/techcourse/DispatcherServlet.java @@ -36,7 +36,6 @@ public void init() { new AnnotationHandlerMapping("com.techcourse") ) ); - handlerMappingComposite.initialize(); handlerAdapterComposite = new HandlerAdapters( List.of( diff --git a/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/HandlerMappings.java b/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/HandlerMappings.java index db37cda2d7..3c3212cd39 100644 --- a/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/HandlerMappings.java +++ b/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/HandlerMappings.java @@ -10,6 +10,8 @@ public class HandlerMappings implements HandlerMapping { public HandlerMappings(final List handlerMappings) { this.handlerMappings = handlerMappings; + + initialize(); } @Override