网站制作平台公司,镇江高端网站定制,湖北工程信息网,网上开店教程springBoot--web--http缓存机制测试 前言1、多端内容适配基于请求头内容协商#xff08;默认开启#xff09;基于请求参数内容协商#xff08;需要开启#xff09; 2、默认返回json数据3、设置返回xml数据导入jackson-dataformat-xml包在类文件中添加注解 JacksonXmlRootEl… springBoot--web--http缓存机制测试 前言1、多端内容适配基于请求头内容协商默认开启基于请求参数内容协商需要开启 2、默认返回json数据3、设置返回xml数据导入jackson-dataformat-xml包在类文件中添加注解 JacksonXmlRootElement 4、基于请求参数内容协商功能默认参数是format5、内容协商的原理--httpMessageConverterResponseBody 由HttpMessageConverter处理 6、增加yaml内容协商导入yaml文件把测试对象写成yaml在配置中增加yaml类型在config下配置一个把对象转为yaml的类定义类效果 前言
一套系统适配多端数据返回
1、多端内容适配
基于请求头内容协商默认开启
客户想服务器发送请求携带http标准的Accept请求头 Accept:application/json text/xml text/yaml 服务端根据客户端请求头期望的数据类型进行动态返回
基于请求参数内容协商需要开启
发送请求 Get/projects/spring-boot?formatjson
匹配到GetMapping(*/projects/spring-boot*)
根据参数协商优先返回json类型数据需要开启参数匹配设置
发送请求Get/projects/spring-boot?formatxml 优先返回xml类型数据2、默认返回json数据 GetMapping(/person)public Person person(){Person person new Person();person.setId(1l);person.setUserName(张三);person.setEmail(2aaaqq.com);person.setAge(18);return person;}3、设置返回xml数据
导入jackson-dataformat-xml包 dependencygroupIdcom.fasterxml.jackson.dataformat/groupIdartifactIdjackson-dataformat-xml/artifactIdversion2.15.3/version/dependency在类文件中添加注解 JacksonXmlRootElement 4、基于请求参数内容协商功能默认参数是format
在配置文件中配置
5、内容协商的原理–httpMessageConverter
ResponseBody 由HttpMessageConverter处理 6、增加yaml内容协商
导入yaml文件 把测试对象写成yaml
public static void main(String[] args) throws JsonProcessingException {Person person new Person();person.setId(1l);person.setUserName(张三);person.setEmail(2aaaqq.com);person.setAge(18);YAMLFactory factory new YAMLFactory().disable(YAMLGenerator.Feature.WRITE_DOC_START_MARKER);ObjectMapper mapper new ObjectMapper(factory);String s mapper.writeValueAsString(person);System.out.println(s);}在配置中增加yaml类型
#增加一种新的内容类型
spring.mvc.contentnegotiation.media-types.yamltext/yaml在config下配置一个把对象转为yaml的类 package com.atguigu.boot304demo.config;import com.atguigu.boot304demo.component.MyYamlHttpMessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.CacheControl;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;import java.util.List;
import java.util.concurrent.TimeUnit;/*** author jitwxs* date 2023年10月20日 10:18*/
//EnableWebMvc //禁用boot的默认设置
Configuration //这是一个配置类,给容器中放一个WevMvcConfigurer组件,就能自定义底层
public class MyConfig {Beanpublic WebMvcConfigurer webMvcConfigurer(){return new WebMvcConfigurer() {Overridepublic void addResourceHandlers(ResourceHandlerRegistry registry) {registry.addResourceHandler(/static/**).addResourceLocations(classPath:/a/, classpath:/b/).setCacheControl(CacheControl.maxAge(1180, TimeUnit.SECONDS));}Overridepublic void extendMessageConverters(ListHttpMessageConverter? converters) {converters.add(new MyYamlHttpMessageConverter());}};}
}
定义类 package com.atguigu.boot304demo.component;import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import com.fasterxml.jackson.dataformat.yaml.YAMLGenerator;
import lombok.Data;
import org.springframework.http.HttpInputMessage;
import org.springframework.http.HttpOutputMessage;
import org.springframework.http.MediaType;
import org.springframework.http.converter.AbstractHttpMessageConverter;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.http.converter.HttpMessageNotWritableException;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.Charset;/*** author jitwxs* date 2023年10月21日 12:50*/
public class MyYamlHttpMessageConverter extends AbstractHttpMessageConverterObject {private ObjectMapper objectMapper null;public MyYamlHttpMessageConverter(){
// 支持哪种媒体类型super(new MediaType(text,yaml, Charset.forName(UTF-8)));YAMLFactory factory new YAMLFactory().disable(YAMLGenerator.Feature.WRITE_DOC_START_MARKER);this.objectMapper new ObjectMapper(factory);}Overrideprotected boolean supports(Class? clazz) {
// 只要对象类型不要基本类型return true;}Override //RequestBodyprotected Object readInternal(Class? clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException {return null;}Override //ResponseBody //把对象怎样写出来protected void writeInternal(Object o, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException {
// try-with写法,自动关流
// try(OutputStream os outputMessage.getBody()){
// this.objectMapper.writeValue(os, o);
// }OutputStream body outputMessage.getBody();try {this.objectMapper.writeValue(body,o);}finally {body.close();}}
}
效果