Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.6k views
in Technique[技术] by (71.8m points)

java - How do you configure spring boot 2 to return xml by default?

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Your WebMvc configuration is working as well as you configured it.

The default ContentType is used if no Accept header is present.

To reach your scope, you have to go on with the Content Negotiation Strategy, and disable the Accept header. Your configureContentNegotiation method should look like:

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

You can take a look atthis article on Spring blog and atthis article on Baeldung.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...