From 63223b3efe3b6c96f7554673a44fe00526dedc9f Mon Sep 17 00:00:00 2001 From: jaeyeon kim Date: Tue, 26 Sep 2023 12:14:46 +0900 Subject: [PATCH] =?UTF-8?q?refactor=20:=20JsonView=20=EC=97=90=EC=84=9C=20?= =?UTF-8?q?=EC=9D=91=EB=8B=B5=EC=9D=84=20=EB=B0=98=ED=99=98=ED=95=A0=20?= =?UTF-8?q?=EB=95=8C=20PrintWriter=20=EB=A5=BC=20=EC=82=AC=EC=9A=A9?= =?UTF-8?q?=ED=95=98=EC=97=AC=20=EB=B0=98=ED=99=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../springframework/web/servlet/view/JsonView.java | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) 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 4bc1509ac7..ae9acbb12f 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,26 +1,27 @@ package webmvc.org.springframework.web.servlet.view; import com.fasterxml.jackson.databind.ObjectMapper; -import jakarta.servlet.ServletOutputStream; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; +import java.io.PrintWriter; import java.util.Map; import web.org.springframework.http.MediaType; import webmvc.org.springframework.web.servlet.View; public class JsonView implements View { + private final ObjectMapper objectMapper = new ObjectMapper(); + @Override public void render( Map model, HttpServletRequest request, HttpServletResponse response ) throws Exception { - ObjectMapper objectMapper = new ObjectMapper(); - ServletOutputStream outputStream = response.getOutputStream(); - String responseBody = objectMapper.writeValueAsString(model); response.setContentType(MediaType.APPLICATION_JSON_UTF8_VALUE); - outputStream.write(responseBody.getBytes()); + String responseBody = objectMapper.writeValueAsString(model); + PrintWriter printWriter = new PrintWriter(response.getOutputStream()); + printWriter.write(responseBody); } }