Springboot-内容协商

Springboot-内容协商

内容协商
根据客户端接收能力不同,返回不同媒体类型的数据。

1、引入xml依赖

 <dependency>
    <groupId>com.fasterxml.jackson.dataformat</groupId>
    <artifactId>jackson-dataformat-xml</artifactId>
</dependency>

2、postman分别测试返回json和xml
只需要改变请求头中Accept字段。Http协议中规定的,告诉服务器本客户端可以接收的数据类型。
xml类型:application/xml
json类型:application/json

3、开启浏览器参数方式内容协商功能
为了方便内容协商,开启基于请求参数的内容协商功能。

spring:
  mvc:
    contentnegotiation:
      favor-parameter: true #开启请求参数内容协商模式 

发请求:json类型: http://localhost:8080/test/person?format=json
xml类型:http://localhost:8080/test/person?format=xml

确定客户端接收什么样的内容类型;
1、Parameter策略优先确定是要返回json数据(获取请求头中的format的值)
2、最终进行内容协商返回给客户端json即可。

4、内容协商原理
• 1、判断当前响应头中是否已经有确定的媒体类型。MediaType
• 2、获取客户端(PostMan、浏览器)支持接收的内容类型。(获取客户端Accept请求头字段)【application/xml】
• contentNegotiationManager 内容协商管理器 默认使用基于请求头的策略
• HeaderContentNegotiationStrategy 确定客户端可以接收的内容类型
• 3、遍历循环所有当前系统的 MessageConverter,看谁支持操作这个对象(Person)
• 4、找到支持操作Person的converter,把converter支持的媒体类型统计出来。
• 5、客户端需要【application/xml】。服务端能力【10种、json、xml】
• 6、进行内容协商的最佳匹配媒体类型
• 7、用 支持 将对象转为 最佳匹配媒体类型 的converter。调用它进行转化 。
导入了jackson处理xml的包,xml的converter就会自动进来

5、自定义 MessageConverter
实现多协议数据兼容。json、xml、x-guigu
0、@ResponseBody 响应数据出去 调用 RequestResponseBodyMethodProcessor 处理
1、Processor 处理方法返回值。通过 MessageConverter 处理
2、所有 MessageConverter 合起来可以支持各种媒体类型数据的操作(读、写)
3、内容协商找到最终的 messageConverter;
SpringMVC的什么功能。一个入口给容器中添加一个 WebMvcConfigurer

 @Bean
    public WebMvcConfigurer webMvcConfigurer(){
        return new WebMvcConfigurer() {
            @Override
            public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
            }
        }
    }

如:先创建自己需要返回的类型,converter

package com.zm.boot.converter;

import com.zm.boot.bean.Person;
import org.springframework.http.HttpInputMessage;
import org.springframework.http.HttpOutputMessage;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.http.converter.HttpMessageNotWritableException;

import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.util.List;

public class GuiguMessageConverter implements HttpMessageConverter<Person> {
    /**
     * 把aClass类型的数据读成mediaType格式类型
     */
    @Override
    public boolean canRead(Class<?> aClass, MediaType mediaType) {
        return false;
    }

    /**
     * 写,把符合aClass类型的数据写成mediaType格式n
     */
    @Override
    public boolean canWrite(Class<?> aClass, MediaType mediaType) {
        return aClass.isAssignableFrom(Person.class);//如果是person类型,就写成mediaType的格式
    }

    /**
     * 服务器要统计所有MessageConverter都能写出那些数据类型
     *
     * 如:application/x-guigu\
     */
    @Override
    public List<MediaType> getSupportedMediaTypes() {
        return MediaType.parseMediaTypes("application/x-guigu");
    }

    @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.getUserName()+";"+person.getAge()+";"+person.getBirth();

        //写出去
        OutputStream body=httpOutputMessage.getBody();
        body.write(data.getBytes());
    }
}

将创建好的converter加入到系统中,在webconfig中进行添加

package com.zm.boot.config;

import com.zm.boot.bean.Pet;
import com.zm.boot.converter.GuiguMessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.converter.Converter;
import org.springframework.format.FormatterRegistry;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.util.StringUtils;
import org.springframework.web.accept.HeaderContentNegotiationStrategy;
import org.springframework.web.accept.ParameterContentNegotiationStrategy;
import org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer;
import org.springframework.web.servlet.config.annotation.PathMatchConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.util.UrlPathHelper;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Configuration(proxyBeanMethods = false)
public class WebConfig {

    @Bean
    public WebMvcConfigurer webMvcConfigurer(){
        return new WebMvcConfigurer() {

            //自定义内容协商策略;将自定义的converter设置访问方式
            @Override
            public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
                //Map<String,MediaType> mediaTypes
                Map<String, MediaType> mediaTypeMap=new HashMap<>();
                mediaTypeMap.put("json",MediaType.APPLICATION_JSON);
                mediaTypeMap.put("xml",MediaType.APPLICATION_XML);
                mediaTypeMap.put("gg",MediaType.parseMediaType("application/x-guigu"));//自定义访问方式
                //指定支持解析哪些参数对应的那些媒体类型
                ParameterContentNegotiationStrategy parameterStrategy=new ParameterContentNegotiationStrategy(mediaTypeMap);//请求参数的方式
                //parameterStrategy.setParameterName("");//这是设置请求头的参数,如若没有设置,就是默认的format;如若设置了,就是设置的值,如设置成ff,则访问时就是ff=xml这样的方式
                HeaderContentNegotiationStrategy headStrategy=new HeaderContentNegotiationStrategy();//请求头的方式
                configurer.strategies(Arrays.asList(parameterStrategy,headStrategy));
            }

            //将自定义的converter添加进去
            @Override
            public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
                converters.add(new GuiguMessageConverter());
            }
        };
    }
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
SpringBoot中的内容协商是指根据请求的参数或者请求头中的信息来确定返回的内容类型。在SpringBoot中,默认开启了基于请求头的内容协商功能,即根据请求头中的Accept字段来确定返回的内容类型。同时,也可以通过配置开启基于请求参数的内容协商功能,即根据请求参数来确定返回的内容类型。\[1\]\[2\]内容协商SpringMVC中也有实现,但SpringMVC默认开启的是基于请求头的内容协商功能。\[1\]所以,如果你在SpringBoot中使用内容协商功能,可以直接使用默认的配置,而如果你在SpringMVC中使用内容协商功能,需要手动配置开启基于请求参数的内容协商功能。\[1\]\[2\]内容协商是一个非常重要的知识点,对于理解和掌握SpringBootSpringMVC的请求和响应处理机制非常有帮助。\[3\] #### 引用[.reference_title] - *1* *3* [SpringBoot中的内容协商,颠覆你的认知](https://blog.csdn.net/likun557/article/details/122994859)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [Springboot-内容协商](https://blog.csdn.net/qq_42250642/article/details/116055710)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值