当前位置: 首页 > news >正文

贵州安顺做公司网站建站行业有哪些

贵州安顺做公司网站,建站行业有哪些,公司域名备案网站名称,网站开发西安springBoot--web--http缓存机制测试 前言1、多端内容适配基于请求头内容协商(默认开启)基于请求参数内容协商(需要开启) 2、默认返回json数据3、设置返回xml数据导入jackson-dataformat-xml包在类文件中添加注解 JacksonXmlRootEl…

springBoot--web--http缓存机制测试

  • 前言
  • 1、多端内容适配
    • 基于请求头内容协商(默认开启)
    • 基于请求参数内容协商(需要开启)
  • 2、默认返回json数据
  • 3、设置返回xml数据
    • 导入jackson-dataformat-xml包
    • 在类文件中添加注解 @JacksonXmlRootElement
  • 4、基于请求参数内容协商功能,默认参数是format
  • 5、内容协商的原理--httpMessageConverter
    • @ResponseBody 由HttpMessageConverter处理
  • 6、增加yaml内容协商
    • 导入yaml文件
    • 把测试对象写成yaml
    • 在配置中增加yaml类型
    • 在config下配置一个把对象转为yaml的类
    • 定义类
    • 效果

在这里插入图片描述

前言

一套系统适配多端数据返回

1、多端内容适配

基于请求头内容协商(默认开启)

客户想服务器发送请求,携带http标准的Accept请求头
Accept:application/json text/xml text/yaml
服务端根据客户端请求头期望的数据类型进行动态返回

基于请求参数内容协商(需要开启)

发送请求 Get/projects/spring-boot?format=json
匹配到@GetMapping(*/projects/spring-boot*)
根据参数协商,优先返回json类型数据(需要开启参数匹配设置)
发送请求Get/projects/spring-boot?format=xml 优先返回xml类型数据

2、默认返回json数据

在这里插入图片描述

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

 @GetMapping("/person")public Person person(){Person person = new Person();person.setId(1l);person.setUserName("张三");person.setEmail("2aaa@qq.com");person.setAge(18);return person;}

3、设置返回xml数据

导入jackson-dataformat-xml包

在这里插入图片描述

<dependency><groupId>com.fasterxml.jackson.dataformat</groupId><artifactId>jackson-dataformat-xml</artifactId><version>2.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("2aaa@qq.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.yaml=text/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(List<HttpMessageConverter<?>> 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 AbstractHttpMessageConverter<Object> {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();}}
}

效果

在这里插入图片描述

http://www.yayakq.cn/news/983022/

相关文章:

  • 网站建设专家哪家强网站关键词优化步骤
  • 高端网站设计开发广州网站建设易企
  • 沈阳 建设工程 招标中心网站android 移动网站开发
  • 中国建设银行北京天竺支行网站wordpress缺少样式表
  • 精仿手表网站上海搬家公司电话价格表
  • 做网站需要交管理费吗网站建设如何实现检索功能
  • 千万pv网站开发成本什么是网络营网络销售好做吗谁做过
  • 织梦模板网站源码甘肃省建设厅建筑业信息网
  • 高端模板建站报价关键词优化的建议
  • 临河做网站商城网站怎样做
  • 郑州网站优化排名推广网页版qq登录入口官网手机
  • 建网站赚钱 知乎织梦本地安装网站
  • 渭南免费做网站php网站建设培训班
  • 制作网站需要什么知识济宁网架公司
  • 铜陵app网站做营销招聘信息展厅设计费
  • 中山建设企业网站公司网站建设费如何出账
  • 给公司做网站数据分析哪家卖的wordpress主题好
  • 哪个网站建设好网站服务费一年多少钱
  • h5做的网站有哪些wordpress建m域名网站
  • 网站建设的扩展性分析江苏建设厅官方网站人工费
  • 高校网站建设存在问题手机软件开发专业
  • 免费网站建设必择山东绘政科技wordpress总是404
  • 安顺市住房和城乡建设局网站天台县建设局官方网站
  • 上海移动云网站建设自己做网站翻译服务器 - 添加网站
  • 长春专业做网站代运营公司收费
  • 山东城乡住房建设厅网站有哪些可以推广的平台
  • 如何用个门户网站做销售济南百度做网站
  • 如何构建一个电子商务网站高端建站选哪家
  • 网站被域名重定向昆明外贸网站设计服务商
  • 邢台网站制作哪家好最好的销售管理系统