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