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>

打包结果

相关推荐
吴声子夜歌10 小时前
Java——动态代理
java·开发语言·代理模式
AI人工智能+电脑小能手10 小时前
【大白话说Java面试题 第59题】【JVM篇】第19题:并发标记过程中会出现什么问题?
java·开发语言·jvm
平行侠10 小时前
40希尔排序 - 以递减间距进行插入排序
java·算法·排序算法
摇滚侠10 小时前
Mybatis 面试题 真正的 offer 偏方 Java 基础 Java 高级
java·开发语言·mybatis
淘矿人10 小时前
Claude助力前端开发
java·数据库·git·python·sql·spring·database
砍材农夫10 小时前
物联网 基于netty心跳和ack机制
java·物联网·netty
happymaker062610 小时前
Spring学习日记——DAY07(SpringMVC)
java·学习·spring
Devin~Y10 小时前
大厂Java面试实录:Spring Boot/Cloud + Redis + Kafka + JVM + RAG(Spring AI)三轮追问(小Y翻车版)
java·jvm·spring boot·redis·spring cloud·kafka·mybatis
JAVA面经实录91710 小时前
Java 并发工具类
java·大数据·开发语言
驭渊的小故事10 小时前
Java数据结构集合框架(顺序表(ArrayList)的详细解析)(两千字详细解析)
java·开发语言