Spring mvc: changing default response format from xml to json

I went through other similar questions, but nothing worked for me.

All my APIs return JSON as the default response:

Due to some XML API I had to add jackson-xml

<dependency> <groupId>com.fasterxml.jackson.dataformat</groupId> <artifactId>jackson-dataformat-xml</artifactId> </dependency> 

Now the default is " No Acceptance Header" All XML responses .

I would like to have JSON as the default response format .

As stated in the document:

https://spring.io/blog/2013/05/11/content-negotiation-using-spring-mvc

I implemented the following configuration:

 @Override public void configureContentNegotiation(ContentNegotiationConfigurer configurer) { configurer.favorPathExtension(false).favorParameter(true).parameterName("mediaType").ignoreAcceptHeader(true) .useJaf(false).defaultContentType(MediaType.APPLICATION_JSON) .mediaType("xml", MediaType.APPLICATION_XML).mediaType("json", MediaType.APPLICATION_JSON); } 

Case 1: if I create ignoreAcceptHeader(true) , then all JSON even the XML API returns JSON.

Case 2: when ignoreAcceptHeader(false) , the default is XML.

I forgot to mention that my API looks like this:

 @RequestMapping(value = "/getXml", method = RequestMethod.GET) public ResponseEntity<String> getXml( HttpServletRequest request) throws JAXBException { return returnXml(); } 

I'm completely lost here. All I want is Default (Without AcceptHeader), should be JSON. (API returns XML as String)

And when Accept Header: "Application / xml" is defined, then the response should be XML.

Any advice would be very helpful.

Thanks.

+11
source share
3 answers

In general, if you want a json response, you need a jackson-databind module:

 <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>${json-jackson-version}</version> </dependency> 

and then you should define MappingJackson2HttpMessageConverter in your configuration:

 @Configuration @EnableWebMvc public class WebAppMainConfiguration extends WebMvcConfigurerAdapter { @Override public void configureMessageConverters(List<HttpMessageConverter<?>> converters) { converters.add(new MappingJackson2HttpMessageConverter()); [..] super.configureMessageConverters(converters); } [...] } 

In your case, you can implement your own AbstractGenericHttpMessageConverter so that you can enable this converter between different concrete converters depending on the type of media.

Check out AbstractGenericHttpMessageConverter#writeInternal(..) method

+4
source

Since you set ignoreAcceptHeader to true and favorPathExtension to false, Spring will rely on other alternatives to negotiate content. So the URL parameter that you configured XML and JSON will look like

since @stan indicated /getXml?mediaType=xml will return an XML response, otherwise it will default to json(defaultContentType(MediaType.APPLICATION_JSON))

+2
source

For me, adding

 @Configuration public class WebConfig extends WebMvcConfigurerAdapter { @Override public void configureContentNegotiation(ContentNegotiationConfigurer configurer) { configurer.defaultContentType(MediaType.APPLICATION_JSON_UTF8); } } 

solved a problem.

Now, by default, all RestController returns JSON if the request does not have an Accept header. Also, if the Accept: application/xml header is passed, the result is XML.

Also worth reading: https://spring.io/blog/2013/05/11/content-negotiation-using-spring-mvc

+1
source

Source: https://habr.com/ru/post/1260983/


All Articles