Springboot 之 Filter 实现超大响应 JSON 数据压缩( 二 )

注册 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 之 Filter 实现超大响应 JSON 数据压缩

文章插图

推荐阅读