@Override protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) throws Exception { OutputStream stream = (this.updateContentLength ? createTemporaryOutputStream() : response.getOutputStream()); Object value = filterModel(model); writeContent(stream, value, this.jsonPrefix); if (this.updateContentLength) { writeToResponse(response, (ByteArrayOutputStream) stream); } }
/** * Construct a new {@code MappingJacksonJsonView}, setting the content type to {@code application/json}. */ public MappingJacksonJsonView() { setContentType(DEFAULT_CONTENT_TYPE); setExposePathVariables(false); }
/** * Whether to use the default pretty printer when writing JSON. * This is a shortcut for setting up an {@code ObjectMapper} as follows: * <pre class="code"> * ObjectMapper mapper = new ObjectMapper(); * mapper.configure(SerializationConfig.Feature.INDENT_OUTPUT, true); * </pre> * <p>The default value is {@code false}. */ public void setPrettyPrint(boolean prettyPrint) { this.prettyPrint = prettyPrint; configurePrettyPrint(); }
@Bean(name = "jsonView") public MappingJacksonJsonView jsonView() { final MappingJacksonJsonView mappingJacksonJsonView = new MappingJacksonJsonView(); mappingJacksonJsonView.setContentType("application/json"); mappingJacksonJsonView.setObjectMapper(new CustomObjectMapper()); return mappingJacksonJsonView; }
/** * @return Returns {@link ModelAndView} created from {@link MappingJacksonJsonView} holding the * information about the exception. */ public ModelAndView asModelAndView() { MappingJacksonJsonView jsonView = new MappingJacksonJsonView(); Map<String, String> map = new HashMap<>(); map.put("error", exception.getMessage()); map.put("exceptionType", exception.getClass().getName()); return new ModelAndView(jsonView, map); } }
MappingJacksonJsonView view = new MappingJacksonJsonView(); view.setContentType("text/html"); Map<String, Object> responseMap = new HashMap<String, Object>(); responseMap.put("success", true); ModelAndView mav = new ModelAndView(view, responseMap);
/** * This class will make sure that if there is a single object to * transform to JSON, it won't be rendered inside a map. */ @SuppressWarnings("unchecked") @Override protected Object filterModel(Map<String, Object> model) { Object result = super.filterModel(model); if (!(result instanceof Map)) { return result; } Map map = (Map) result; if (map.size() == 1) { return map.values().toArray()[0]; } return map; } }
@Override protected void prepareResponse(HttpServletRequest request, HttpServletResponse response) { setResponseContentType(request, response); response.setCharacterEncoding(this.encoding.getJavaName()); if (this.disableCaching) { response.addHeader("Pragma", "no-cache"); response.addHeader("Cache-Control", "no-cache, no-store, max-age=0"); response.addDateHeader("Expires", 1L); } }
@ExceptionHandler(AccessDeniedException.class) @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) public ModelAndView handleAllExceptions(Exception exception) { MappingJacksonJsonView jsonView = new MappingJacksonJsonView(); ModelAndView mav = new ModelAndView(jsonView); mav.addObject(ERROR_MESSAGE_PARAMETER, exception.getMessage()); return mav; } }
/** * Catches all exceptions threw by any method in this controller and returns HTTP status 500 with error message in * response body instead. * * @param exception exception that was thrown * @return {@link ModelAndView} object with JSON view and error message as model */ @ExceptionHandler(Exception.class) @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) public ModelAndView handleAllExceptions(Exception exception) { MappingJacksonJsonView jsonView = new MappingJacksonJsonView(); ModelAndView mav = new ModelAndView(jsonView); mav.addObject(ERROR_MESSAGE_PARAMETER, exception.getMessage()); return mav; }
/** * Set the {@code ObjectMapper} for this view. * If not set, a default {@link ObjectMapper#ObjectMapper() ObjectMapper} will be used. * <p>Setting a custom-configured {@code ObjectMapper} is one way to take further control of * the JSON serialization process. The other option is to use Jackson's provided annotations * on the types to be serialized, in which case a custom-configured ObjectMapper is unnecessary. */ public void setObjectMapper(ObjectMapper objectMapper) { Assert.notNull(objectMapper, "'objectMapper' must not be null"); this.objectMapper = objectMapper; configurePrettyPrint(); }