To start with I've read this:
Spring boot - setting default Content-type header if it's not present in request
The older version of this worked on spring boot 1. However when receiving request with the following accept header Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2"
The response is in json.
I've put in a class
@Configuration
public class MyWebMvcConfigurer implements WebMvcConfigurer {
@Override
public void configureContentNegotiation(
ContentNegotiationConfigurer configurer) {
configurer.defaultContentType(MediaType.APPLICATION_XML);
}
}
And I can see that the defaultContentType stratedgy is being set. However it its being overwritten by the the AcceptHeaderConfig stratedgy.
It looks like the defaultContentType is only used as a fallback.
Note that the same code in spring boot 1 worked and defaulted to XML.
Complete Example
package com.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import javax.servlet.http.HttpServletRequest;
import javax.xml.bind.annotation.XmlRootElement;
@SpringBootApplication
@RestController
public class CnApp {
@RequestMapping("/")
public Person person(HttpServletRequest request, ModelMap model){
return new Person();
}
public static void main(String[] args) throws Exception {
SpringApplication.run(CnApp.class, args);
}
@XmlRootElement
public static class Person {
public String firstName = "Jon";
public String lastName = "Doe";
}
@Configuration
public static class ServerConfig implements WebMvcConfigurer {
@Override
public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
configurer.defaultContentType(MediaType.APPLICATION_XML);
}
}
}
as you can see by running
curl localhost:8080 -H"Accept: text/html, image/gif, image/jpg;q=0.2, */*;q=0.2"
It defaults to json even though XML is default
From comment I posted below
The issue is with the old version of spring we can send with an accept header and get requests for that it defaults to XML. However JSON is still supported.
So when an accept header comes in that supports both JSON and XML at same specificity we need to return XML.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…