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.