springboot 十二 基于请求参数的内容协商

基于请求参数的内容协商

在yaml中添加配置

spring:
  mvc:
    contentnegotiation:
      favor-parameter: true 

如果不开启的话他会以默认的方式处理请求数据
在这里插入图片描述
权重不同 处理的优先级就不同

controller 控制器

在这里插入图片描述

请求参数中携带format请求=返回方式

http://localhost:8080/test/person?format=json //返回JSON
http://localhost:8080/test/person?format=xml //返回XML

结果

在这里插入图片描述
在这里插入图片描述

自定义请求头And参数内容协商

public class MyConverter implements HttpMessageConverter<Person> {

    //是否支持读
    @Override
    public boolean canRead(Class<?> clazz, MediaType mediaType) {
        return false;
    }

    //是否支持写
    @Override
    public boolean canWrite(Class<?> clazz, MediaType mediaType) {
        return clazz.isAssignableFrom(Person.class);
    }

    //返回支持的格式名
    @Override
    public List<MediaType> getSupportedMediaTypes() {
        return MediaType.parseMediaTypes("application/x-tong");
    }


    @Override
    public Person read(Class<? extends Person> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException {
        return null;
    }

    //发送客户端数据形式
    @Override
    public void write(Person person, MediaType contentType, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException {
        String date = person.getName()+";"+person.getAge();
        OutputStream body = outputMessage.getBody();
        body.write(date.getBytes());
    }
}
 public WebMvcConfigurer webMvcConfigurer(){
        return new WebMvcConfigurer() {
            //覆盖底层的MessageMyConverter
            @Override
            public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {

                Map<String, MediaType> mediaType = new HashMap<>();
                mediaType.put("json", MediaType.APPLICATION_JSON);                       //json方式
                mediaType.put("xml", MediaType.APPLICATION_XML);                         //xml方式
                mediaType.put("aaa", MediaType.parseMediaType("application/x-tong"));        //将自己的装入底层
                ParameterContentNegotiationStrategy parameterContentNegotiationStrategy = new ParameterContentNegotiationStrategy(mediaType);       //参数形式
                HeaderContentNegotiationStrategy headerContentNegotiationStrategy = new HeaderContentNegotiationStrategy();     //请求头方式

                configurer.strategies(Arrays.asList(parameterContentNegotiationStrategy, headerContentNegotiationStrategy));
            }

            @Override
            public void addFormatters(FormatterRegistry registry) {
                registry.addConverter(new Converter<String, Pet>() {

                    @Override
                    public Pet convert(String source) {
                        if(!StringUtils.isEmpty(source)){
                            Pet pat = new Pet();
                            String [] split = source.split(",");
                            pat.setName(split[0]);
                            pat.setAge(String.valueOf(Integer.valueOf(split[1])));
                            return pat;
                        }
                        return null;
                    }
                });
            }
        };
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值