diff --git a/mvc/src/main/java/nextstep/mvc/view/JsonView.java b/mvc/src/main/java/nextstep/mvc/view/JsonView.java index c4ebbcdf07..8077bd36b2 100644 --- a/mvc/src/main/java/nextstep/mvc/view/JsonView.java +++ b/mvc/src/main/java/nextstep/mvc/view/JsonView.java @@ -1,5 +1,6 @@ package nextstep.mvc.view; +import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; @@ -17,6 +18,14 @@ public class JsonView implements View { public void render(final Map model, final HttpServletRequest request, HttpServletResponse response) throws Exception { response.setContentType(APPLICATION_JSON_UTF8_VALUE); final PrintWriter writer = response.getWriter(); - writer.write(objectMapper.writeValueAsString(model)); + writer.write(generateJson(model)); + } + + private String generateJson(Map model) throws JsonProcessingException { + if (model.size() == 1) { + Object value = model.values().iterator().next(); + return objectMapper.writeValueAsString(value); + } + return objectMapper.writeValueAsString(model); } }