Skip to content
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 9 commits into from
Sep 26, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import webmvc.org.springframework.web.servlet.adapter.AnnotationHandlerAdapter;
import webmvc.org.springframework.web.servlet.handlermapping.AnnotationHandlerMapping;
import com.techcourse.ManualHandlerMapping;
import webmvc.org.springframework.web.servlet.view.JspView;
import webmvc.org.springframework.web.servlet.view.View;

public class DispatcherServlet extends HttpServlet {
Expand Down
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();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

render 메서드가 호출될 때마다 새로운 ObjectMapper 객체가 생성될 것 같은데 정적 변수로 두는 것은 어떨까요?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

현재 구조에서는 JsonView가 싱글턴이 아니기 때문에 하나의 JsonView마다 render() 메소드가 한 번만 호출됩니다. 그래서 정적 변수로 두는 것이 크게 메리트있다고 생각하진 않아요ㅠㅠ

그래서! 나중에 스프링 DI를 구현한다는 가정하에 일반 필드로 두었어요! 요건 어떠신가요?

public class JsonView implements View {

    private final ObjectMapper objectMapper = new ObjectMapper();

    @Override
    public void render(final Map<String, ?> model, final HttpServletRequest request, HttpServletResponse response) throws Exception {
        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;
    }
}

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

결국 JsonView 객체를 무한히 생성할 수 있기 때문에 static 이 의미있다고 생각하지 않았습니다!

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;
}
}