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

文章目录


响应头添加版本号

获取版本号

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

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

添加响应处理器

完整代码如下:

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

java 复制代码
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;

    @Override
    public boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType) {
        return true;
    }

    @Override
    public 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 标签,写入以下代码

xml 复制代码
<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>

打包结果

相关推荐
铲子Zzz25 分钟前
Java使用接口AES进行加密+微信小程序接收解密
java·开发语言·微信小程序
霖檬ing31 分钟前
K8s——配置管理(1)
java·贪心算法·kubernetes
Vic101011 小时前
Java 开发笔记:多线程查询逻辑的抽象与优化
java·服务器·笔记
Biaobiaone1 小时前
Java中的生产消费模型解析
java·开发语言
特立独行的猫a2 小时前
11款常用C++在线编译与运行平台推荐与对比
java·开发语言·c++
louisgeek2 小时前
Java 位运算
java
hweiyu003 小时前
Maven 私库
java·maven
Super Rookie3 小时前
Spring Boot 企业项目技术选型
java·spring boot·后端
写不出来就跑路3 小时前
Spring Security架构与实战全解析
java·spring·架构
ZeroNews内网穿透3 小时前
服装零售企业跨区域运营难题破解方案
java·大数据·运维·服务器·数据库·tcp/ip·零售