I have gone through other similar asked questions but nothing worked for me.
All my API's return JSON as response by Default:
Because of some XML API, i had to add jackson-xml
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
</dependency>
Now by default "Without accept header" All responses are XML.
I would like to have JSON as default Response format .
As stated in the doc here:
https://spring.io/blog/2013/05/11/content-negotiation-using-spring-mvc
I implemented the following config :
@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 make the ignoreAcceptHeader(true)
then everything is JSON even the XML API returning JSON.
Case 2: when ignoreAcceptHeader(false)
is then default is XML.
I forget to mention my API's look like this:
@RequestMapping(value = "/getXml", method = RequestMethod.GET)
public ResponseEntity<String> getXml( HttpServletRequest request)
throws JAXBException {
return returnXml();
}
I am quite 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 response should be XML.
Any advice would be of great help.
Thanks.