Skip to content

Commit

Permalink
refactor: 메서드 분리
Browse files Browse the repository at this point in the history
  • Loading branch information
miseongk committed Sep 26, 2023
1 parent a3fc8c2 commit 206f941
Showing 1 changed file with 8 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package webmvc.org.springframework.web.servlet.view;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
Expand All @@ -16,12 +17,15 @@ public class JsonView implements View {
public void render(final Map<String, ?> model, final HttpServletRequest request, final HttpServletResponse response)
throws Exception {
response.setContentType(MediaType.APPLICATION_JSON_UTF8_VALUE);

final PrintWriter writer = response.getWriter();
final String jsonResponse = jsonToString(model);
writer.write(jsonResponse);
}

private String jsonToString(final Map<String, ?> model) throws JsonProcessingException {
if (model.size() == 1) {
writer.write(mapper.writeValueAsString(model.values().toArray()[0]));
return;
return mapper.writeValueAsString(model.values().toArray()[0]);
}
writer.write(mapper.writeValueAsString(model));
return mapper.writeValueAsString(model);
}
}

0 comments on commit 206f941

Please sign in to comment.