How to apply JSON on Spring MVC 4.0 app by using JavaConfig? -
I want to use JSON (Jackson Library) with my existing application by using JavaConfig (Spring MVC 4.0) I have my configuration: I want to implement JSON configuration but I do not get any solution I think I have to expand the webmc config adapter. And in other detail, I use Spring Security. Can you help me? Thanks Try this one ViewResolver & gt; Reserves = New Arrestist & lt; ViewResolver & gt; (); InternalResourceViewResolver r1 = New InternalResourceViewResolver (); R1.setPrefix ("/ web-INF / pages /"); R1.setSuffix ("jsp."); R1.setViewClass (JstlView.class); Resolvers.add (r1); JsonViewResolver r2 = new JsonViewResolver (); Resolvers.add (r2); ContentNegotiatingViewResolver Resolver = New ContentNotivingWeaverSolar (); Resolver.setViewResolvers (resolver); Resolver.setContentNegotiationManager (manager); Return resolver; } and should be used to add Jackson support MappingJacksonJsonView or MappingJackson2JsonView This is the config for just illustrating your example in application.xml Then maybe need to add MediaTypes to ContentNegotiatingViewResolver in the @Bean method. Check for more about ContentNegotiation
@Configuration @EnableWebMvc @ComponentScan ({"my.package.controller"}) Public class WebConfiguration extends WebMvcConfigurerAdapter {@Bean (name = "viewResolver ") Public Internal Resourcevolverroller Viewroller () {Internal Resourcevivalroll Viewer = New Internal ResourceWizardSolar (); ViewResolver.setPrefix ("/ web-INF / pages /"); ViewResolver.setSuffix ("jsp."); Return visitorolver; }}
@Bean Public ViewResolver contentNegotiatingViewResolver {ContentNegotiationManager Manager} {list From & lt ;: = "text">
Public Segment JsonViewResolver applies ViewResolver {public view resolutionViewName (string VIEWNAME, locale locale ) Exceptions thrown {MappingJacksonJoscience View = New MappingJacksonJohnview (); View.setPrettyPrint (true); Return view; }}
& lt; Beam square = "org.springframework.web.servlet.view .contentNegotiatingViewResolver" & gt; & Lt; Property Name = "Order" Value = "1" /> & Lt; Property Name = "Media Type" & gt; & Lt; Map & gt; & Lt; Entry key = "JSON" value = "App / JSON" /> & Lt; Entry key = "xml" value = "app / XML" /> & Lt; / Map & gt; & Lt; / Property & gt;
Comments
Post a Comment