diff --git a/app/src/main/java/com/techcourse/DispatcherServletInitializer.java b/app/src/main/java/com/techcourse/DispatcherServletInitializer.java index a5ca691f2d..4e18625a40 100644 --- a/app/src/main/java/com/techcourse/DispatcherServletInitializer.java +++ b/app/src/main/java/com/techcourse/DispatcherServletInitializer.java @@ -4,7 +4,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import web.org.springframework.web.WebApplicationInitializer; -import webmvc.org.springframework.web.servlet.mvc.asis.ControllerHandlerAdapter; +import webmvc.org.springframework.web.servlet.mvc.DispatcherServlet; import webmvc.org.springframework.web.servlet.mvc.tobe.AnnotationHandlerMapping; import webmvc.org.springframework.web.servlet.mvc.tobe.HandlerExecutionHandlerAdapter; @@ -21,9 +21,7 @@ public class DispatcherServletInitializer implements WebApplicationInitializer { @Override public void onStartup(final ServletContext servletContext) { final DispatcherServlet dispatcherServlet = new DispatcherServlet(); - dispatcherServlet.addHandlerMapping(new ManualHandlerMapping()); dispatcherServlet.addHandlerMapping(new AnnotationHandlerMapping("com")); - dispatcherServlet.addHandlerAdapter(new ControllerHandlerAdapter()); dispatcherServlet.addHandlerAdapter(new HandlerExecutionHandlerAdapter()); final var registration = servletContext.addServlet(DEFAULT_SERVLET_NAME, dispatcherServlet); 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 cb8aafe043..0000000000 --- a/app/src/main/java/com/techcourse/ManualHandlerMapping.java +++ /dev/null @@ -1,40 +0,0 @@ -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.HandlerMapping; -import webmvc.org.springframework.web.servlet.mvc.asis.Controller; -import webmvc.org.springframework.web.servlet.mvc.asis.ForwardController; - -import java.util.HashMap; -import java.util.Map; - -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()); - 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() - .forEach(path -> log.info("Path : {}, Controller : {}", path, controllers.get(path).getClass())); - } - - @Override - public Controller getHandler(final HttpServletRequest request) { - String requestURI = request.getRequestURI(); - log.debug("Request Mapping Uri : {}", requestURI); - return controllers.get(requestURI); - } -} 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..a84d7ce2f6 --- /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(final HttpServletRequest req, + final HttpServletResponse res) { + 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 35bac1b026..880c64923b 100644 --- a/app/src/main/java/com/techcourse/controller/LoginController.java +++ b/app/src/main/java/com/techcourse/controller/LoginController.java @@ -2,35 +2,45 @@ 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.mvc.asis.Controller; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; 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) { - if (UserSession.isLoggedIn(req.getSession())) { + + @RequestMapping(value = "/login", method = RequestMethod.POST) + public ModelAndView login(final HttpServletRequest request, + final HttpServletResponse response) { + final String path = execute(request, response); + return new ModelAndView(new JspView(path)); + } + + private String execute(final HttpServletRequest request, + final HttpServletResponse response) { + if (UserSession.isLoggedIn(request.getSession())) { return "redirect:/index.jsp"; } - return InMemoryUserRepository.findByAccount(req.getParameter("account")) + return InMemoryUserRepository.findByAccount(request.getParameter("account")) .map(user -> { log.info("User : {}", user); - return login(req, user); + return login(request, user); }) .orElse("redirect:/401.jsp"); } - private String login(final HttpServletRequest request, final User user) { + private String login(final HttpServletRequest request, + final User user) { if (user.checkPassword(request.getParameter("password"))) { final var session = request.getSession(); session.setAttribute(UserSession.SESSION_KEY, user); @@ -39,8 +49,4 @@ private String login(final HttpServletRequest request, final User user) { return "redirect:/401.jsp"; } - @RequestMapping(value = "/login", method = RequestMethod.GET) - public ModelAndView login(HttpServletRequest req, HttpServletResponse res) { - return new ModelAndView(new JspView(execute(req, res))); - } } diff --git a/app/src/main/java/com/techcourse/controller/LoginViewController.java b/app/src/main/java/com/techcourse/controller/LoginViewController.java index e581f172de..c550a608ad 100644 --- a/app/src/main/java/com/techcourse/controller/LoginViewController.java +++ b/app/src/main/java/com/techcourse/controller/LoginViewController.java @@ -1,5 +1,6 @@ package com.techcourse.controller; +import context.org.springframework.stereotype.Controller; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; import org.slf4j.Logger; @@ -7,26 +8,27 @@ 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.mvc.asis.Controller; import webmvc.org.springframework.web.servlet.view.JspView; -public class LoginViewController implements Controller { +@Controller +public class LoginViewController { private static final Logger log = LoggerFactory.getLogger(LoginViewController.class); - @Override - public String execute(final HttpServletRequest req, final HttpServletResponse res) { - return UserSession.getUserFrom(req.getSession()) + @RequestMapping(value = "/login/view", method = RequestMethod.GET) + public ModelAndView loginView(final HttpServletRequest request, + final HttpServletResponse response) { + final String path = execute(request, response); + return new ModelAndView(new JspView(path)); + } + + private String execute(final HttpServletRequest request, + final HttpServletResponse response) { + return UserSession.getUserFrom(request.getSession()) .map(user -> { log.info("logged in {}", user.getAccount()); return "redirect:/index.jsp"; }) .orElse("/login.jsp"); } - - @RequestMapping(value = "/login/view", method = RequestMethod.GET) - public ModelAndView loginView(HttpServletRequest req, HttpServletResponse res) { - String path = execute(req, res); - return new ModelAndView(new JspView(path)); - } } diff --git a/app/src/main/java/com/techcourse/controller/LogoutController.java b/app/src/main/java/com/techcourse/controller/LogoutController.java index 777eb25afb..f9c39099be 100644 --- a/app/src/main/java/com/techcourse/controller/LogoutController.java +++ b/app/src/main/java/com/techcourse/controller/LogoutController.java @@ -1,25 +1,20 @@ 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.mvc.asis.Controller; 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) { - final var session = req.getSession(); + @RequestMapping(value = "/logout", method = RequestMethod.GET) + public ModelAndView logout(HttpServletRequest request, HttpServletResponse response) { + final var session = request.getSession(); session.removeAttribute(UserSession.SESSION_KEY); - return "redirect:/"; - } - - @RequestMapping(value = "/logout", method = RequestMethod.POST) - public ModelAndView logout(HttpServletRequest req, HttpServletResponse res) { - String path = execute(req, res); - return new ModelAndView(new JspView(path)); + return new ModelAndView(new JspView("redirect:/")); } } diff --git a/app/src/main/java/com/techcourse/controller/RegisterController.java b/app/src/main/java/com/techcourse/controller/RegisterController.java index 8dffb57bf4..0cad0a933e 100644 --- a/app/src/main/java/com/techcourse/controller/RegisterController.java +++ b/app/src/main/java/com/techcourse/controller/RegisterController.java @@ -2,31 +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 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.mvc.asis.Controller; import webmvc.org.springframework.web.servlet.view.JspView; -public class RegisterController implements Controller { - - @Override - public String 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"; - } +@Controller +public class RegisterController { @RequestMapping(value = "/register", method = RequestMethod.POST) - public ModelAndView save(HttpServletRequest request, HttpServletResponse response) throws Exception { - String path = execute(request, response); - return new ModelAndView(new JspView(path)); + public ModelAndView save(final HttpServletRequest request, + final HttpServletResponse response) { + final User user = new User(2, + request.getParameter("account"), + request.getParameter("password"), + request.getParameter("email")); + InMemoryUserRepository.save(user); + final JspView view = new JspView("redirect:/index.jsp"); + return new ModelAndView(view); } - } diff --git a/app/src/main/java/com/techcourse/controller/RegisterViewController.java b/app/src/main/java/com/techcourse/controller/RegisterViewController.java index 5daa6d5e9f..aedb60ff41 100644 --- a/app/src/main/java/com/techcourse/controller/RegisterViewController.java +++ b/app/src/main/java/com/techcourse/controller/RegisterViewController.java @@ -1,23 +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.mvc.asis.Controller; import webmvc.org.springframework.web.servlet.view.JspView; -public class RegisterViewController implements Controller { - - @Override - public String execute(final HttpServletRequest req, final HttpServletResponse res) { - return "/register.jsp"; - } +@Controller +public class RegisterViewController { @RequestMapping(value = "/register/view", method = RequestMethod.GET) - public ModelAndView loginView(HttpServletRequest req, HttpServletResponse res) { - String path = execute(req, res); - return new ModelAndView(new JspView(path)); + public ModelAndView loginView(final HttpServletRequest req, + final HttpServletResponse res) { + return new ModelAndView(new JspView("/register.jsp")); } } diff --git a/app/src/main/java/com/techcourse/controller/UserController.java b/app/src/main/java/com/techcourse/controller/UserController.java new file mode 100644 index 0000000000..cd6cf39689 --- /dev/null +++ b/app/src/main/java/com/techcourse/controller/UserController.java @@ -0,0 +1,33 @@ +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(final HttpServletRequest request, + final HttpServletResponse response) { + final String account = request.getParameter("account"); + log.debug("user id : {}", account); + + final ModelAndView modelAndView = new ModelAndView(new JsonView()); + final User user = InMemoryUserRepository.findByAccount(account) + .orElseThrow(); + + modelAndView.addObject("user", user); + return modelAndView; + } +} diff --git a/app/src/main/java/com/techcourse/support/web/filter/ResourceFilter.java b/app/src/main/java/com/techcourse/support/web/filter/ResourceFilter.java index ddbddd671c..47b3c6097c 100644 --- a/app/src/main/java/com/techcourse/support/web/filter/ResourceFilter.java +++ b/app/src/main/java/com/techcourse/support/web/filter/ResourceFilter.java @@ -39,8 +39,8 @@ public void init(final FilterConfig filterConfig) throws ServletException { @Override public void doFilter(final ServletRequest request, final ServletResponse response, final FilterChain chain) throws IOException, ServletException { - final var req = (HttpServletRequest) request; - final var path = req.getRequestURI().substring(req.getContextPath().length()); + final var httpServletRequest = (HttpServletRequest) request; + final var path = httpServletRequest.getRequestURI().substring(httpServletRequest.getContextPath().length()); if (isResourceUrl(path)) { log.debug("path : {}", path); 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 ff8e24553f..a38f24b195 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 @@ -14,7 +14,8 @@ public ModelAndView(final View view) { this.model = new HashMap<>(); } - public ModelAndView addObject(final String attributeName, final Object attributeValue) { + public ModelAndView addObject(final String attributeName, + final Object attributeValue) { model.put(attributeName, attributeValue); return this; } diff --git a/app/src/main/java/com/techcourse/DispatcherServlet.java b/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/DispatcherServlet.java similarity index 85% rename from app/src/main/java/com/techcourse/DispatcherServlet.java rename to mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/DispatcherServlet.java index 6d296881e0..568dbf505f 100644 --- a/app/src/main/java/com/techcourse/DispatcherServlet.java +++ b/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/DispatcherServlet.java @@ -1,4 +1,4 @@ -package com.techcourse; +package webmvc.org.springframework.web.servlet.mvc; import jakarta.servlet.ServletException; import jakarta.servlet.http.HttpServlet; @@ -8,8 +8,6 @@ import org.slf4j.LoggerFactory; import webmvc.org.springframework.web.servlet.ModelAndView; import webmvc.org.springframework.web.servlet.View; -import webmvc.org.springframework.web.servlet.mvc.HandlerAdapter; -import webmvc.org.springframework.web.servlet.mvc.HandlerMapping; public class DispatcherServlet extends HttpServlet { @@ -41,9 +39,9 @@ protected void service(final HttpServletRequest request, final HttpServletRespon } } - private void render(ModelAndView modelAndView, HttpServletRequest req, HttpServletResponse res) throws Exception { - View view = modelAndView.getView(); - view.render(modelAndView.getModel(), req, res); + private void render(ModelAndView modelAndView, HttpServletRequest request, HttpServletResponse response) throws Exception { + final View view = modelAndView.getView(); + view.render(modelAndView.getModel(), request, response); } public void addHandlerMapping(HandlerMapping handlerMapping) { diff --git a/app/src/main/java/com/techcourse/HandlerAdapters.java b/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/HandlerAdapters.java similarity index 81% rename from app/src/main/java/com/techcourse/HandlerAdapters.java rename to mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/HandlerAdapters.java index f4bb1bb4f9..3f35034ba4 100644 --- a/app/src/main/java/com/techcourse/HandlerAdapters.java +++ b/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/HandlerAdapters.java @@ -1,7 +1,4 @@ -package com.techcourse; - -import jakarta.annotation.Nonnull; -import webmvc.org.springframework.web.servlet.mvc.HandlerAdapter; +package webmvc.org.springframework.web.servlet.mvc; import java.util.ArrayList; import java.util.List; @@ -14,11 +11,11 @@ public void addHandlerAdapter(HandlerAdapter handlerAdapter) { handlerAdapters.add(handlerAdapter); } - @Nonnull public HandlerAdapter getHandlerAdapter(Object handler) { return handlerAdapters.stream() .filter(adapter -> adapter.supports(handler)) .findAny() .orElseThrow(() -> new IllegalArgumentException("핸들러 어뎁터를 찾지 못했습니다.: " + handler)); + } } diff --git a/app/src/main/java/com/techcourse/HandlerMappings.java b/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/HandlerMappings.java similarity index 86% rename from app/src/main/java/com/techcourse/HandlerMappings.java rename to mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/HandlerMappings.java index 32591b4929..8773063799 100644 --- a/app/src/main/java/com/techcourse/HandlerMappings.java +++ b/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/HandlerMappings.java @@ -1,8 +1,6 @@ -package com.techcourse; +package webmvc.org.springframework.web.servlet.mvc; -import jakarta.annotation.Nonnull; import jakarta.servlet.http.HttpServletRequest; -import webmvc.org.springframework.web.servlet.mvc.HandlerMapping; import java.util.ArrayList; import java.util.List; @@ -16,7 +14,6 @@ public void initializeEach() { handlerMappings.forEach(HandlerMapping::initialize); } - @Nonnull public Object getHandler(HttpServletRequest request) { return handlerMappings.stream() .map(handlerMapping -> handlerMapping.getHandler(request)) 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 bdd1fde780..0000000000 --- a/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/asis/Controller.java +++ /dev/null @@ -1,8 +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/ControllerHandlerAdapter.java b/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/asis/ControllerHandlerAdapter.java deleted file mode 100644 index cf586549fb..0000000000 --- a/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/asis/ControllerHandlerAdapter.java +++ /dev/null @@ -1,24 +0,0 @@ -package webmvc.org.springframework.web.servlet.mvc.asis; - -import jakarta.servlet.http.HttpServletRequest; -import jakarta.servlet.http.HttpServletResponse; -import webmvc.org.springframework.web.servlet.ModelAndView; -import webmvc.org.springframework.web.servlet.mvc.HandlerAdapter; -import webmvc.org.springframework.web.servlet.view.JspView; - -public class ControllerHandlerAdapter implements HandlerAdapter { - - @Override - public boolean supports(final Object handler) { - return handler instanceof Controller; - } - - @Override - public ModelAndView handle(final HttpServletRequest request, - final HttpServletResponse response, - final Object handler) throws Exception { - final Controller controller = (Controller) handler; - return new ModelAndView(new JspView(controller.execute(request, response))); - } - -} 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/AnnotationHandlerMapping.java b/mvc/src/main/java/webmvc/org/springframework/web/servlet/mvc/tobe/AnnotationHandlerMapping.java index c300a83a75..ba6dcf1cc3 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 @@ -40,7 +40,8 @@ public void initialize() { } } - private void createHandlerExecution(final Method[] methods, final Object controllerInstance) { + private void createHandlerExecution(final Method[] methods, + final Object controllerInstance) { for (Method method : methods) { if (method.isAnnotationPresent(RequestMapping.class)) { final RequestMapping requestMapping = method.getAnnotation(RequestMapping.class); @@ -62,7 +63,7 @@ private void addHandlerExecutions(final HandlerExecution handlerExecution, } } - private Object getInstance(Class controller) { + private Object getInstance(final Class controller) { try { return controller.getConstructor().newInstance(); } catch (Exception e) { 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..479c0848e4 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,47 @@ package webmvc.org.springframework.web.servlet.view; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; +import web.org.springframework.http.MediaType; import webmvc.org.springframework.web.servlet.View; +import java.io.PrintWriter; import java.util.Map; +import java.util.NoSuchElementException; public class JsonView implements View { + private static final ObjectMapper objectMapper = new ObjectMapper(); + @Override - public void render(final Map model, final HttpServletRequest request, HttpServletResponse response) throws Exception { + public void render(final Map model, + final HttpServletRequest request, + final HttpServletResponse response) throws Exception { + final String jsonBody = getJsonString(model); + response.setContentType(MediaType.APPLICATION_JSON_UTF8_VALUE); + final PrintWriter writer = response.getWriter(); + writer.write(jsonBody); + writer.flush(); + } + + private String getJsonString(final Map model) { + if (model.size() == 1) { + return model.values().stream() + .map(this::writeValueAsString) + .findAny() + .orElseThrow(NoSuchElementException::new); + } + return writeValueAsString(model); } + + private String writeValueAsString(final Object object) { + try { + return objectMapper.writeValueAsString(object); + } catch (JsonProcessingException e) { + throw new IllegalArgumentException("JsonProcessingException: ", e); + } + } + } 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 1275a38089..6ac9de5ba7 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 @@ -16,12 +16,14 @@ public class JspView implements View { private final String viewName; - public JspView(String viewName) { + public JspView(final String viewName) { this.viewName = viewName; } @Override - public void render(final Map model, final HttpServletRequest request, final HttpServletResponse response) throws Exception { + public void render(final Map model, + final HttpServletRequest request, + final HttpServletResponse response) throws Exception { model.keySet().forEach(key -> { log.debug("attribute name : {}, value : {}", key, model.get(key)); request.setAttribute(key, model.get(key));