springboot 返回数据格式问题
先说原由:原来我的框架是 springMVC,项目提供了一个热加载配置文件的接口,直接在浏览器中使用get方式就能热更新配置文件到内存中,然鹅,我最近改了框架为springboot方式的,然后就出现问了。
先贴出配置代码
@Override
public void configureContentNegotiation(
ContentNegotiationConfigurer configurer) {
configurer.mediaType("ext", MediaType.APPLICATION_JSON);// 设置ext放回类型为json
configurer.mediaType("in", MediaType.APPLICATION_JSON);// 设置ext放回类型为json
configurer.mediaType("do", MediaType.APPLICATION_JSON);// 设置ext放回类型为json
configurer.favorPathExtension(false);
}
返回的是XML格式数据
然后找资料。看到了这个配置的地方还是有很多讲究的
关键在于配置的地方,各个含义
@Override
public void configureContentNegotiation(
ContentNegotiationConfigurer configurer) {
// TODO Auto-generated method stub
// super.configureContentNegotiation(configurer);
//设置是否忽略请求头的content-type,设置为TRUE后,按照请求后缀来返回对应的数据格式
// configurer.ignoreAcceptHeader(true);
//自定义后缀返回的数据格式
configurer.mediaType("ext", MediaType.APPLICATION_JSON);// 设置ext放回类型为json
configurer.mediaType("in", MediaType.APPLICATION_JSON);// 设置ext放回类型为json
configurer.mediaType("do", MediaType.APPLICATION_JSON);// 设置ext放回类型为json
//是否是否使用后缀 来返回数据格式,优先级比 请求头要高
configurer.favorPathExtension(false);
favorParameter(true).
parameterName("mediaType").
useJaf(false).
//设置默认的返回数据格式
configurer.defaultContentType(MediaType.APPLICATION_JSON).
mediaType("xml", MediaType.APPLICATION_XML).
mediaType("json", MediaType.APPLICATION_JSON); */
}