注册 GzipFilter 拦截器package com.olive.config;import com.olive.filter.GzipFilter;import org.springframework.boot.web.servlet.FilterRegistrationBean;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;/** * 注册filter */@Configurationpublic class FilterRegistration {@Beanpublic FilterRegistrationBean<GzipFilter> gzipFilterRegistrationBean() {FilterRegistrationBean<GzipFilter> registration = new FilterRegistrationBean<>();//Filter可以new,也可以使用依赖注入Beanregistration.setFilter(new GzipFilter());//过滤器名称registration.setName("gzipFilter");//拦截路径registration.addUrlPatterns("/*");//设置顺序registration.setOrder(1);return registration;}}
定义 Controller【Springboot 之 Filter 实现超大响应 JSON 数据压缩】该 Controller 非常简单,主要读取一个大文本文件,作为输出的内容 。
package com.olive.controller;import java.io.File;import java.util.HashMap;import java.util.Map;import com.olive.vo.ArticleRequestVO;import org.apache.commons.io.FileUtils;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@RestControllerpublic class TestController { @RequestMapping("/getArticle") public Map<String, Object> getArticle(){Map<String, Object> result = new HashMap<>();result.put("code", 200);result.put("msg", "success");byte[] bytes = null;try {bytes = FileUtils.readFileToByteArray(new File("C:\\Users\\2230\\Desktop\\凯平项目资料\\改装车项目\\CXSSBOOT_DB_DDL-1.0.9.sql"));}catch (Exception e){}String content = new String(bytes);ArticleRequestVO vo = new ArticleRequestVO();vo.setId(1L);vo.setTitle("BUG弄潮儿");vo.setContent(content);result.put("body", vo);return result; }}
Controller 返回数据的 VO
package com.olive.vo;import lombok.Data;import java.io.Serializable;@Datapublic class ArticleRequestVO implements Serializable {private Long id;private String title;private String content;}
定义 Springboot 引导类package com.olive;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublic class Application {public static void main(String[] args) {SpringApplication.run(Application.class);}}
测试测试的curl
curl -X POST http://127.0.0.1:8080/getArticle
文章插图
推荐阅读
- SpringBoot+Vue3 AgileBoot - 手把手一步一步带你Run起全栈项目
- 咸鱼之王龙鱼坚盾怎么搭配
- 宝石研物语:伊恩之石好感度角色是什么
- 宝石研物语:伊恩之石迈向绚烂之时活动在哪
- 宝石研物语:伊恩之石迈向绚烂之时最后一关怎么打
- 宝石研物语:伊恩之石怎么培养莉莉的好感度
- 原神熏风之境怎么通关
- 宝石研物语:伊恩之石都有哪些称号
- SpringBoot 自定义注解 实现多数据源
- 分布式存储系统之Ceph集群启用Dashboard及使用Prometheus监控Ceph