@Bean public ContentNegotiatingViewResolver viewResolver() { ContentNegotiatingViewResolver resolver = new ContentNegotiatingViewResolver(); // Used strategy only for JSON FixedContentNegotiationStrategy fixedStrategy = new FixedContentNegotiationStrategy(MediaType.APPLICATION_JSON); Collection<ContentNegotiationStrategy> strategies = new ArrayList<ContentNegotiationStrategy>(); strategies.add(fixedStrategy); resolver.setContentNegotiationManager(new ContentNegotiationManager(fixedStrategy)); // Handle formatting of dates and JSON root elements ObjectMapper mapper = new ObjectMapper(); mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, false); mapper.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, false); mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false); mapper.registerModule(new JodaModule()); ArrayList<View> defaultViews = new ArrayList<View>(); MappingJackson2JsonView jsonView = new MappingJackson2JsonView(); jsonView.setObjectMapper(mapper); jsonView.setExtractValueFromSingleKeyModel(true); defaultViews.add(jsonView); resolver.setDefaultViews(defaultViews); resolver.setOrder(2); return resolver; }
@Bean public ContentNegotiatingViewResolver contentViewResolver() throws Exception { ContentNegotiationManagerFactoryBean contentNegotiationManager = new ContentNegotiationManagerFactoryBean(); contentNegotiationManager.addMediaType("json", MediaType.APPLICATION_JSON); InternalResourceViewResolver viewResolver = new InternalResourceViewResolver(); viewResolver.setPrefix("/WEB-INF/views/"); viewResolver.setSuffix(".jsp"); MappingJackson2JsonView defaultView = new MappingJackson2JsonView(); defaultView.setExtractValueFromSingleKeyModel(true); ContentNegotiatingViewResolver contentViewResolver = new ContentNegotiatingViewResolver(); contentViewResolver.setContentNegotiationManager(contentNegotiationManager.getObject()); contentViewResolver.setViewResolvers(Arrays.<ViewResolver>asList(viewResolver)); contentViewResolver.setDefaultViews(Arrays.<View>asList(defaultView)); return contentViewResolver; }
@Override public void setPrefixJson(boolean prefixJson) { super.setPrefixJson(prefixJson); this.jsonPrefix = "{} && "; }
@Override public void setJsonPrefix(String jsonPrefix) { super.setJsonPrefix(jsonPrefix); this.jsonPrefix = jsonPrefix; }
@Override public void setUpdateContentLength(boolean updateContentLength) { super.setUpdateContentLength(updateContentLength); this.updateContentLength = updateContentLength; }
@Bean public View jsonTemplate() { MappingJackson2JsonView view = new MappingJackson2JsonView(); view.setPrettyPrint(true); return view; }
@Override public View resolveViewName(String viewName, Locale locale) throws Exception { MappingJackson2JsonView jsonView = new MappingJackson2JsonView(); jsonView.setPrettyPrint(true); return jsonView; }