【Springboot2.x】通过参数选择返回xml或json数据或自定义返回值类型自定义消息转换器

一、背景

此处用到了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返回不同类型的值

在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值