-
Notifications
You must be signed in to change notification settings - Fork 305
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
[MVC 구현하기 3단계] 디투(박정훈) 미션 제출합니다. #625
Merged
Merged
Changes from 1 commit
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
5fe6ab3
refactor: 패키징 변경
shb03323 43e0a2e
refactor: view 역할 분리
shb03323 b5f2211
feat: JsonView 구현
shb03323 31b393b
feat: UserController 추가
shb03323 1450f4c
feat: 레거시 controller 제거
shb03323 e829dcd
refactor: dispatcherServlet 패키지 이동
shb03323 f384e25
refactor: User에 getter 추가
shb03323 92a75c5
refactor: 로그아웃 오류 수정
shb03323 2cc9a45
refactor: ObjectMapper를 JsonView 필드로 두기
shb03323 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
This file contains 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
14 changes: 14 additions & 0 deletions
14
mvc/src/main/java/webmvc/org/springframework/web/servlet/view/JsonView.java
This file contains 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,13 +1,27 @@ | ||
package webmvc.org.springframework.web.servlet.view; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import web.org.springframework.http.MediaType; | ||
|
||
import java.util.Map; | ||
|
||
public class JsonView implements View { | ||
|
||
@Override | ||
public void render(final Map<String, ?> model, final HttpServletRequest request, HttpServletResponse response) throws Exception { | ||
final ObjectMapper objectMapper = new ObjectMapper(); | ||
final Object modelValue = getModelValue(model); | ||
|
||
response.setContentType(MediaType.APPLICATION_JSON_UTF8_VALUE); | ||
response.getWriter().write(objectMapper.writeValueAsString(modelValue)); | ||
} | ||
|
||
private Object getModelValue(final Map<String, ?> model) { | ||
if (model.size() == 1) { | ||
return model.values().iterator().next(); | ||
} | ||
return model; | ||
} | ||
} |
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.
render
메서드가 호출될 때마다 새로운ObjectMapper
객체가 생성될 것 같은데 정적 변수로 두는 것은 어떨까요?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.
현재 구조에서는
JsonView
가 싱글턴이 아니기 때문에 하나의JsonView
마다render()
메소드가 한 번만 호출됩니다. 그래서 정적 변수로 두는 것이 크게 메리트있다고 생각하진 않아요ㅠㅠ그래서! 나중에 스프링 DI를 구현한다는 가정하에 일반 필드로 두었어요! 요건 어떠신가요?
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.
결국
JsonView
객체를 무한히 생성할 수 있기 때문에 static 이 의미있다고 생각하지 않았습니다!