-
Notifications
You must be signed in to change notification settings - Fork 304
[MVC 구현하기 - 3단계] 에버(손채영) 미션 제출합니다. #865
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
c36eff8
feat: JsonView 구현
helenason 17d9af6
test: JsonView 및 UserController
helenason 3643262
refactor: Legacy MVC 제거
helenason f5d5126
test: 전체적인 테스트 보완
helenason aac5307
test: 학습테스트 IoC/DI
helenason 3dac7a8
refactor: 컨트롤러 메서드 시그니처 통일
helenason 31859ab
refactor: 스캔할 패키지 경로 외부에서 주입
helenason a3850dd
refactor: tobe 패키지 제거
helenason fb0c1bd
refactor: ControllerScanner 클래스 추출
helenason 211fbec
test: ObjectMapper 파싱 방식 테스트
helenason 95a7556
refactor: UserService 기본생성자 접근제어자 닫기
helenason 761923d
refactor: 어노테이션 필터링 로직 외부 추출
helenason e6e71ea
refactor: canAssign 메서드 로직 수정
helenason d1d98ce
refactor: 컨트롤러 패키지 상수 app 모듈로 추출
helenason 6dfc3da
refactor: 예외 처리 함수형 인터페이스 적용
helenason File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
10 changes: 6 additions & 4 deletions
10
app/src/main/java/com/techcourse/DispatcherServletInitializer.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 0 additions & 26 deletions
26
app/src/main/java/com/techcourse/ManualHandlerAdapter.java
This file was deleted.
Oops, something went wrong.
40 changes: 0 additions & 40 deletions
40
app/src/main/java/com/techcourse/ManualHandlerMapping.java
This file was deleted.
Oops, something went wrong.
18 changes: 18 additions & 0 deletions
18
app/src/main/java/com/techcourse/controller/ForwardController.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.techcourse.controller; | ||
|
||
import com.interface21.context.stereotype.Controller; | ||
import com.interface21.web.bind.annotation.RequestMapping; | ||
import com.interface21.web.bind.annotation.RequestMethod; | ||
import com.interface21.webmvc.servlet.ModelAndView; | ||
import com.interface21.webmvc.servlet.view.JspView; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
|
||
@Controller | ||
public class ForwardController { | ||
|
||
@RequestMapping(value = "/", method = RequestMethod.GET) | ||
public ModelAndView index(final HttpServletRequest req, final HttpServletResponse res) throws Exception { | ||
return new ModelAndView(JspView.from("/index.jsp")); | ||
} | ||
} |
38 changes: 26 additions & 12 deletions
38
app/src/main/java/com/techcourse/controller/LoginController.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,51 @@ | ||
package com.techcourse.controller; | ||
|
||
import com.interface21.context.stereotype.Controller; | ||
import com.interface21.web.bind.annotation.RequestMapping; | ||
import com.interface21.web.bind.annotation.RequestMethod; | ||
import com.interface21.webmvc.servlet.ModelAndView; | ||
import com.interface21.webmvc.servlet.view.JspView; | ||
import com.techcourse.domain.User; | ||
import com.techcourse.repository.InMemoryUserRepository; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import com.interface21.webmvc.servlet.mvc.asis.Controller; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
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(JspView.from("/index.jsp")); | ||
} | ||
|
||
return InMemoryUserRepository.findByAccount(req.getParameter("account")) | ||
.map(user -> { | ||
log.info("User : {}", user); | ||
return login(req, user); | ||
}) | ||
.orElse("redirect:/401.jsp"); | ||
.orElse(new ModelAndView(JspView.from("/401.jsp"))); | ||
} | ||
|
||
private String login(final HttpServletRequest request, final User user) { | ||
if (user.checkPassword(request.getParameter("password"))) { | ||
final var session = request.getSession(); | ||
private ModelAndView login(final HttpServletRequest req, final User user) { | ||
if (user.checkPassword(req.getParameter("password"))) { | ||
final var session = req.getSession(); | ||
session.setAttribute(UserSession.SESSION_KEY, user); | ||
return "redirect:/index.jsp"; | ||
return new ModelAndView(JspView.from("/index.jsp")); | ||
} | ||
return "redirect:/401.jsp"; | ||
return new ModelAndView(JspView.from("/401.jsp")); | ||
} | ||
|
||
@RequestMapping(value = "/login", method = RequestMethod.GET) | ||
public ModelAndView getView(final HttpServletRequest req, final HttpServletResponse res) throws Exception { | ||
return UserSession.getUserFrom(req.getSession()) | ||
.map(user -> { | ||
log.info("logged in {}", user.getAccount()); | ||
return new ModelAndView(JspView.from("/index.jsp")); | ||
}) | ||
.orElse(new ModelAndView(JspView.from("/login.jsp"))); | ||
} | ||
} |
22 changes: 0 additions & 22 deletions
22
app/src/main/java/com/techcourse/controller/LoginViewController.java
This file was deleted.
Oops, something went wrong.
15 changes: 10 additions & 5 deletions
15
app/src/main/java/com/techcourse/controller/LogoutController.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,20 @@ | ||
package com.techcourse.controller; | ||
|
||
import com.interface21.context.stereotype.Controller; | ||
import com.interface21.web.bind.annotation.RequestMapping; | ||
import com.interface21.web.bind.annotation.RequestMethod; | ||
import com.interface21.webmvc.servlet.ModelAndView; | ||
import com.interface21.webmvc.servlet.view.JspView; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import com.interface21.webmvc.servlet.mvc.asis.Controller; | ||
|
||
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(JspView.from("/")); | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
app/src/main/java/com/techcourse/controller/UserController.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package com.techcourse.controller; | ||
|
||
import com.interface21.context.stereotype.Controller; | ||
import com.interface21.web.bind.annotation.RequestMapping; | ||
import com.interface21.web.bind.annotation.RequestMethod; | ||
import com.interface21.webmvc.servlet.ModelAndView; | ||
import com.interface21.webmvc.servlet.view.JsonView; | ||
import com.techcourse.domain.User; | ||
import com.techcourse.repository.InMemoryUserRepository; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
@Controller | ||
public class UserController { | ||
|
||
private static final Logger log = LoggerFactory.getLogger(UserController.class); | ||
|
||
@RequestMapping(value = "/api/user", method = RequestMethod.GET) | ||
public ModelAndView show(HttpServletRequest req, HttpServletResponse res) { | ||
final String account = req.getParameter("account"); | ||
log.debug("user id : {}", account); | ||
|
||
final ModelAndView modelAndView = new ModelAndView(JsonView.from()); | ||
final User user = InMemoryUserRepository.findByAccount(account) | ||
.orElseThrow(); | ||
|
||
modelAndView.addObject("user", user); | ||
return modelAndView; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,6 @@ | ||
package com.techcourse.repository; | ||
|
||
import com.techcourse.domain.User; | ||
|
||
import java.util.Map; | ||
import java.util.Optional; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
|
@@ -11,7 +10,7 @@ public class InMemoryUserRepository { | |
private static final Map<String, User> database = new ConcurrentHashMap<>(); | ||
|
||
static { | ||
final var user = new User(1, "gugu", "password", "[email protected]"); | ||
final var user = new User(1, "ever", "password", "[email protected]"); | ||
database.put(user.getAccount(), user); | ||
} | ||
|
||
|
@@ -23,5 +22,6 @@ public static Optional<User> findByAccount(String account) { | |
return Optional.ofNullable(database.get(account)); | ||
} | ||
|
||
private InMemoryUserRepository() {} | ||
private InMemoryUserRepository() { | ||
} | ||
} |
49 changes: 0 additions & 49 deletions
49
app/src/test/java/com/techcourse/HandlerMappingRegistryTest.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
getter는 사용하는 곳이 없는 것 같아요:)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
위 getter는 jackson의 ObjectMapper가 User 객체를 Json 문자열로 파싱하기 위한 용도입니다! Getter를 선언하지 않은 필드의 경우는 Json 문자열로 파싱하지 못하더라구요. 혹시 제가 모르는 또 다른 방법이 있는 걸까요? 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
말씀하신 부분이 맞는 것 같아요! 그런데 id, password, email은 직렬화하는 곳이 없는 것 같아서 여쭤봤어요!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ObjectMapper는 기본적으로 public으로 선언되어 있지 않은 필드에 대해 직렬화하지 못합니다! ObjectMapper가 private 필드를 직렬화하도록 하기 위해서는 해당 필드에 대한 getter가 필요해요. 즉, 제가 선언한 getter는 저의 비즈니스 코드에서는 사용되지 않지만, ObjectMapper에서 사용된다고 보시면 될 것 같아요 😊
model에 데이터가 1개면 값을 그대로 반환하고 2개 이상이면 Map 형태 그대로 JSON으로 변환해서 반환한다.
라는 요구사항이 있었는데요. User 클래스에getAccount()
만 존재할 경우 아래와 같이 account 필드에 대해서만 직렬화돼요.이를 아래와 같이 출력하기 위해 나머지 세 필드에 대해서도 getter를 추가했다고 보시면 됩니다!
관련하여 저도 더 자세히 학습할겸,
ObjectMapperTest
를 추가해두었습니다! 참고 자료도 첨부할게요 :)P.S. 저는 id, account, password, email 모든 필드에 대해 직렬화하여 반환하는 것을 이번 미션의 요구사항 중 하나라고 생각해서 위와 같이 구현하였습니다!