@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 ViewResolver contentNegotiatingViewResolver(ContentNegotiationManager manager) { List<ViewResolver> resolvers = new ArrayList<ViewResolver>(); resolvers.add(getJspViewResolver()); resolvers.add(getJsonViewResolver()); ContentNegotiatingViewResolver resolver = new ContentNegotiatingViewResolver(); resolver.setContentNegotiationManager(manager); resolver.setViewResolvers(resolvers); return resolver; }
/* * @Bean public ThymeleafViewResolver thymeleafViewResolver() { * ThymeleafViewResolver thymeleafViewResolver = new * ThymeleafViewResolver(); thymeleafViewResolver .setViewNames(new String[] * { "WEEB-IBF/views/thymeleaf/*" }); thymeleafViewResolver.setOrder(2); * thymeleafViewResolver.setTemplateEngine(springTemplateEngine()); return * thymeleafViewResolver; } */ @Bean public ContentNegotiatingViewResolver contentNegotiatingViewResolver( final ContentNegotiationManager contentNegotiationManager) { ContentNegotiatingViewResolver contentNegotiatingViewResolver = new ContentNegotiatingViewResolver(); contentNegotiatingViewResolver.setContentNegotiationManager(contentNegotiationManager); List<ViewResolver> viewResolvers = new ArrayList<ViewResolver>(); viewResolvers.add(beanNameViewResolver()); viewResolvers.add(internalResourceViewResolver()); contentNegotiatingViewResolver.setViewResolvers(viewResolvers); return contentNegotiatingViewResolver; }
@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; }
@Bean public ViewResolver cnViewResolver(ContentNegotiationManager cnm) { ContentNegotiatingViewResolver cnvr = new ContentNegotiatingViewResolver(); cnvr.setContentNegotiationManager(cnm); return cnvr; }