How to apply JSON on Spring MVC 4.0 app by using JavaConfig? -


"itemprop =" text ">

I want to use JSON (Jackson Library) with my existing application by using JavaConfig (Spring MVC 4.0)

I have my configuration:

  @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; }}   

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

  @Bean Public ViewResolver contentNegotiatingViewResolver {ContentNegotiationManager Manager} {list From & lt ;: = "text"> 

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

  Public Segment JsonViewResolver applies ViewResolver {public view resolutionViewName (string VIEWNAME, locale locale ) Exceptions thrown {MappingJacksonJoscience View = New MappingJacksonJohnview (); View.setPrettyPrint (true); Return view; }}   

This is the config for just illustrating your example in application.xml

  & 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;   

Then maybe need to add MediaTypes to ContentNegotiatingViewResolver in the @Bean method. Check for more about

ContentNegotiation

Comments

Popular posts from this blog

c - Mpirun hangs when mpi send and recieve is put in a loop -

python - Apply coupon to a customer's subscription based on non-stripe related actions on the site -

java - Unable to get JDBC connection in Spring application to MySQL -