Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/apache-flex/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 您如何配置SpringBoot2在默认情况下返回xml?_Java_Spring Boot_Spring Mvc_Content Negotiation - Fatal编程技术网

Java 您如何配置SpringBoot2在默认情况下返回xml?

Java 您如何配置SpringBoot2在默认情况下返回xml?,java,spring-boot,spring-mvc,content-negotiation,Java,Spring Boot,Spring Mvc,Content Negotiation,首先,我读了以下内容: 这个旧版本在spring boot 1上运行。但是,当接收到带有以下accept标头的请求时,accept:text/html、image/gif、image/jpeg、*;q=.2,*/*;q=.2“ 响应是json格式的 我参加了一个班 @配置 公共类MyWebMVCConfiguer实现WebMVCConfiguer{ @凌驾 公共无效配置内容协商( ContentNegotiationConfigurer(配置器){ defaultContentType(Med

首先,我读了以下内容:

这个旧版本在spring boot 1上运行。但是,当接收到带有以下accept标头的请求时,
accept:text/html、image/gif、image/jpeg、*;q=.2,*/*;q=.2“

响应是json格式的

我参加了一个班

@配置
公共类MyWebMVCConfiguer实现WebMVCConfiguer{
@凌驾
公共无效配置内容协商(
ContentNegotiationConfigurer(配置器){
defaultContentType(MediaType.APPLICATION_XML);
}
}
我可以看到正在设置defaultContentType stratedgy,但是它被AcceptHeaderConfig stratedgy覆盖

看起来defaultContentType仅用作回退

请注意,spring boot 1中的相同代码工作正常,默认为XML


完整示例
package.com.example;
导入org.springframework.boot.SpringApplication;
导入org.springframework.boot.autoconfigure.springboot应用程序;
导入org.springframework.context.annotation.Configuration;
导入org.springframework.http.MediaType;
导入org.springframework.ui.ModelMap;
导入org.springframework.web.bind.annotation.RequestMapping;
导入org.springframework.web.bind.annotation.RestController;
导入org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer;
导入org.springframework.web.servlet.config.annotation.WebMVCConfiguer;
导入javax.servlet.http.HttpServletRequest;
导入javax.xml.bind.annotation.XmlRootElement;
@SpringBoot应用程序
@RestController
公共类CnApp{
@请求映射(“/”)
公共人物(HttpServletRequest请求,ModelMap模型){
返回新人();
}
公共静态void main(字符串[]args)引发异常{
SpringApplication.run(CnApp.class,args);
}
@XmlRootElement
公共静态类人员{
公共字符串firstName=“Jon”;
公共字符串lastName=“Doe”;
}
@配置
公共静态类ServerConfig实现WebMVCConfiguer{
@凌驾
public void配置contentnegotiation(ContentNegotiationConfigurer-configurer){
defaultContentType(MediaType.APPLICATION_XML);
}
}
}
正如你从跑步中看到的 接受:text/html、image/gif、image/jpg;q=0.2,*/*;q=0.2“它默认为json,即使XML是默认值


根据我在下面发表的评论

问题在于旧版本的spring可以发送一个accept头,并获取默认为XML的请求


因此,当一个接受头同时支持JSON和XML时,我们需要返回XML。

您的
WebMvc
配置与您配置的一样有效

如果不存在
Accept
标题,则使用默认的
ContentType

要达到您的范围,您必须继续使用内容协商策略,并禁用Accept标头。您的
configureContentNegotiation
方法应如下所示:

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

您可以查看Spring博客上的文章和Baeldung上的文章。

进一步研究这一点。 @thepaoloboi在回答中建议的是正确的,只有在没有其他形式的内容协商发生的情况下,defaultMessageConverter才会发生

为了解决这个问题,我已经详细介绍了
org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor
使用的代码,我发现它取决于已配置的转换器的顺序

因此,作为一个黑客,以下内容在Spring1和Spring2中都有效

package.com.example;
导入org.springframework.boot.SpringApplication;
导入org.springframework.boot.autoconfigure.springboot应用程序;
导入org.springframework.context.annotation.Configuration;
导入org.springframework.http.MediaType;
导入org.springframework.http.converter.HttpMessageConverter;
导入org.springframework.http.converter.xml.Jaxb2CollectionHttpMessageConverter;
导入org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter;
导入org.springframework.ui.ModelMap;
导入org.springframework.web.bind.annotation.RequestMapping;
导入org.springframework.web.bind.annotation.RestController;
导入org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer;
导入org.springframework.web.servlet.config.annotation.WebMVCConfigureAdapter;
导入javax.servlet.http.HttpServletRequest;
导入javax.xml.bind.annotation.XmlRootElement;
导入java.util.List;
@SpringBoot应用程序
@RestController
公共类CnApp{
@请求映射(“/”)
公共人物(HttpServletRequest请求,ModelMap模型){
返回新人();
}
公共静态void main(字符串[]args)引发异常{
SpringApplication.run(CnApp.class,args);
}
@XmlRootElement
公共静态类人员{
公共字符串firstName=“Jon”;
公共字符串lastName=“Doe”;
}
@配置
公共静态类ServerConfig扩展WebMVCConfigureAdapter{
@凌驾
public void配置contentnegotiation(ContentNegotiationConfigurer-configurer){
defaultContentType(MediaType.APPLICATION_XML);
}
@凌驾
public void configureMessageConverters(ListJonDoe%)
curl localhost:8080-H“接受:text/html,image/gif,image/jpg;q=0.2,application/json,*/*;q=0.2”
{“firstName”:“Jon”,“lastName”:“Doe”}%
请注意,如果在标题中的任何位置指定了application/json,则仍然首选此选项

这仍然感觉像是一种黑客行为,如果有一种方法可以对首选mime类型进行排序,而不必添加或重新排序转换器,那就太好了