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>

打包结果

相关推荐
oak隔壁找我16 小时前
JVM常用调优参数
java·后端
蝎子莱莱爱打怪20 小时前
OpenClaw 从零配置指南:接入飞书 + 常用命令 + 原理图解
java·后端·ai编程
狼爷1 天前
Go 没有 override?别硬套继承!用接口+嵌入,写更清爽的“覆盖”逻辑
java·go
小兔崽子去哪了1 天前
Java 自动化部署
java·后端
ma_king1 天前
入门 java 和 数据库
java·数据库·后端
后端AI实验室1 天前
我用Cursor开发了3个月,整理出这套提效4倍的工作流
java·ai
码路飞1 天前
GPT-5.3 Instant 终于学会好好说话了,顺手对比了下同天发布的 Gemini 3.1 Flash-Lite
java·javascript
SimonKing1 天前
OpenCode AI编程助手如何添加Skills,优化项目!
java·后端·程序员
Seven971 天前
剑指offer-80、⼆叉树中和为某⼀值的路径(二)
java
怒放吧德德2 天前
Netty 4.2 入门指南:从概念到第一个程序
java·后端·netty