【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 {
    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 getSupportedMediaTypes() {
        return  MediaType.parseMediaTypes(XGUIGU);
    }

    @Override
    public Person read(Class 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 添加依赖

        
            com.fasterxml.jackson.dataformat
            jackson-dataformat-xml
        
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 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参数返回不同类型的值

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

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

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

你可能感兴趣的:(json,spring,java)