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

SpringBoot 响应头添加版本号、打包项目后缀添加版本号和时间

文章目录

  • 响应头添加版本号
    • 获取版本号
    • 添加响应处理器
    • 请求结果
  • 打包项目后缀添加版本号和时间
    • 实现
    • 打包结果


响应头添加版本号

获取版本号

pom.xml 中,在 project.version 下定义版本号

在这里插入图片描述

application.yml 获取 pom.xmlproject.version 中的信息

在这里插入图片描述

添加响应处理器

完整代码如下:

通过 @Value("${project.version}") 获取 application.yml 中的 project.version,并写入响应头

import jakarta.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.MethodParameter;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.http.server.ServletServerHttpResponse;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;@ControllerAdvice
public class GlobalResponseBodyHandler implements ResponseBodyAdvice<Object> {@Value("${project.version}")private String version;@Overridepublic boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType) {return true;}@Overridepublic Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType, Class<? extends HttpMessageConverter<?>> selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) {ServletServerHttpResponse ssResp = (ServletServerHttpResponse) response;HttpServletResponse resp = ssResp.getServletResponse();resp.setHeader("version", StringUtils.isNotEmpty(version) ? version : "unknown");return body;}
}

请求结果

在这里插入图片描述

打包项目后缀添加版本号和时间

实现

pom.xml 中的 build 标签,写入以下代码

<build><!--打包后生成文件名--><finalName>${project.artifactId}-${project.version}_${current.time}</finalName><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><configuration><mainClass>com.chh.api.ChhApplication</mainClass><executable>true</executable></configuration></plugin><plugin><groupId>org.codehaus.mojo</groupId><artifactId>build-helper-maven-plugin</artifactId><version>3.0.0</version><executions><execution><id>timestamp-property</id><goals><goal>timestamp-property</goal></goals></execution></executions><configuration><name>current.time</name><pattern>yyyyMMdd-HHmmss</pattern><timeZone>GMT+8</timeZone></configuration></plugin><!-- 打包跳过测试--><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-surefire-plugin</artifactId><configuration><skip>true</skip></configuration></plugin></plugins>
</build>

打包结果

在这里插入图片描述

http://www.lryc.cn/news/136664.html

相关文章:

  • 优化指南:带宽限制的可行策略
  • 计算机提示mfc120u.dll缺失(找不到)怎么解决
  • Java基于SpringBoot+Vue实现酒店客房管理系统(2.0 版本)
  • 微服务架构2.0--云原生时代
  • C++day2作业(2023.8.22)
  • 在 Spring Boot 中使用 OpenAI ChatGPT API
  • 【leetcode】225.用队列实现栈
  • 机器学习中XGBoost算法调参技巧
  • 第1章:计算机网络体系结构
  • 【Java 动态数据统计图】动态数据统计思路Demo(动态,排序,containsKey)三(115)
  • 【游戏评测】河洛群侠传一周目玩后感
  • java新特性之Lambda表达式
  • 【考研数学】线形代数第三章——向量 | 2)向量组相关性与线性表示的性质,向量组的等价、极大线性无关组与秩
  • Java中调用Linux脚本
  • Nexus 如何配置 Python 的私有仓库
  • Maven 配置文件修改及导入第三方jar包
  • jmeter CSV 数据文件设置
  • 【SA8295P 源码分析】20 - GVM Android Kernel NFS Support 配置
  • c++都补了c语言哪些坑?
  • 【C语言】C语言用数组算平均数,并输出大于平均数的数
  • 「UG/NX」Block UI 体收集器BodyCollector
  • 金九银十面试题之《JVM》
  • wireshark | 过滤筛选总结
  • list使用
  • 【图解】多层感知器(MLP)
  • React(8)
  • ssm社区管理与服务系统源码和论文
  • Git多版本并行开发实践
  • 修复hive重命名分区后新分区为0的问题
  • Gin+微服务实现抖音视频上传到七牛云