【Springboot2.x】通过参数选择返回xml或json数据或自定义返回值类型自定义消息转换器
一、背景此处用到了spring的内容协商功能二、配置过程2.1 添加依赖<!--用于解析xml--><dependency><groupId>com.fasterxml.jackson.dataformat</groupId><artifactId>jackson-dataformat-xml</artifactId><
·
一、背景
此处用到了spring的内容协商功能
二、配置过程
2.1 自定义一个消息转化器用于解析返回值
/**
* @version V1.0
* @ClassName AtGuiGuMessageConverter
* @Description 自定义得messageconverter,专门用于写person类型得数据
* @Author maruis
* @Date 2022/5/30 8:06
*/
public class AtGuiGuMessageConverter implements HttpMessageConverter<Person> {
public static String XGUIGU = "application/x-atguigu";
@Override
public boolean canRead(Class<?> aClass, MediaType mediaType) {
return false;
}
@Override
public boolean canWrite(Class<?> aClass, MediaType mediaType) {
// 是否可以写,如果为true才能转换输出给浏览器,专门用于写person类型得数据
return aClass.isAssignableFrom(Person.class);
}
@Override
public List<MediaType> getSupportedMediaTypes() {
return MediaType.parseMediaTypes(XGUIGU);
}
@Override
public Person read(Class<? extends Person> aClass, HttpInputMessage httpInputMessage) throws IOException, HttpMessageNotReadableException {
return null;
}
@Override
public void write(Person person, MediaType mediaType, HttpOutputMessage httpOutputMessage) throws IOException, HttpMessageNotWritableException {
// 自定义数据,返回格式为把所有得数据按照中间加 ; 得形式返回
String data = person.getName()+";"+person.getAge()+";"+person.getsClientIp()+";"+person.getCat().toString();
OutputStream body = httpOutputMessage.getBody();
body.write(data.getBytes());
}
}
2.2 添加配置
2.2.1 添加依赖
<!-- 用于解析xml-->
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
</dependency>
2.2.2 xml配置
## 内容协商策略 开启默认参数 默认参数是format
spring.mvc.contentnegotiation.favor-parameter=true
2.2.3 webMvcConfigurer 中配置
@Bean
WebMvcConfigurer webMvcConfigurer() {
return new WebMvcConfigurer() {
/***
* 设置内容协商策略,会覆盖默认的策略,默认只有HeaderContentNegotiationStrategy(通过accept请求头的)
* 通过参数的内容协商需要配置默认参数format:spring.mvc.contentnegotiation.favor-parameter=true
* 这两可以通过两种方式来完成一个接口返回不同的内容类型:
* 1.返回xml:header中添加:Accept=application/xml 或者参数form=xml
* 2.返回json:header中添加:Accept=application/json 或者参数form=json
* 3.返回自定义的:header中添加:Accept=application/x-atguigu 或者参数form=gg
*/
@Override
public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
Map<String, MediaType> paraMediatys = new Hashtable<>();
paraMediatys.put("json",MediaType.APPLICATION_JSON);
paraMediatys.put("xml",MediaType.APPLICATION_XML);
// 自定义的
paraMediatys.put("gg",MediaType.parseMediaType(AtGuiGuMessageConverter.XGUIGU));
// 基于参数的类容协商策略
ParameterContentNegotiationStrategy parameterContentNegotiationStrategy = new ParameterContentNegotiationStrategy(paraMediatys);
// 基于请求头header的内容协商策略。
HeaderContentNegotiationStrategy headerContentNegotiationStrategy = new HeaderContentNegotiationStrategy();
// 把这两种策略都添加进去
configurer.strategies(Arrays.asList(parameterContentNegotiationStrategy,headerContentNegotiationStrategy));
WebMvcConfigurer.super.configureContentNegotiation(configurer);
}
};
}
2.3 测试
2.3.1 通过format参数返回不同类型的值

2.3.2 通过Header中Accept返回不同类型的值

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐


所有评论(0)