Skip to content

Commit

Permalink
refactor: ObjectMapper 와 JsonView 통합
Browse files Browse the repository at this point in the history
  • Loading branch information
Cyma-s committed Sep 26, 2023
1 parent 9731d76 commit d36fd8a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -1,17 +1,29 @@
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;
import java.util.Map;
import web.org.springframework.http.MediaType;

public class JsonView implements View {

private static final ObjectMapper objectMapper = new ObjectMapper();

@Override
public void render(final Map<String, ?> model, final HttpServletRequest request, HttpServletResponse response)
throws Exception {
final String body = JsonWriterObjectMapper.writeJson(model);
final String body = writeJson(model);
response.setContentType(MediaType.APPLICATION_JSON_UTF8_VALUE);
response.getWriter().write(body);
}

private String writeJson(final Map<String, ?> objects) {
try {
return objectMapper.writeValueAsString(objects);
} catch (JsonProcessingException e) {
throw new RuntimeException("Json 객체 변환 과정에서 예외가 발생했습니다.");
}
}
}

This file was deleted.

0 comments on commit d36fd8a

Please sign in to comment.