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;
                    }
                });
            }
        };
    }

猜你喜欢

转载自blog.csdn.net/qq_47431361/article/details/123787520