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>

打包结果

相关推荐
H_oRIZoN_8 分钟前
Linux入门DAY27(文件IO(系统调用)详解|open/read/write/lseek)
java·linux·服务器
延凡科技11 分钟前
从 “人管机器“ 到 “数据管人“:智慧矿山综合管控落地实践
java·后端·struts
卓怡学长43 分钟前
w180springboot基于Java的悠扬乐器管理
java·spring boot·mysql·spring·maven·intellij-idea
古法安卓1 小时前
Android-高版本 LMKD 源码解析:基于 PSI 的内存压力监控与查杀全流程
android·java·android studio
古法安卓1 小时前
Android-PSI 详解:libpsi 源码解析——116 行代码架起 lmkd 与内核之间的桥
android·java·android studio
用户36533043470941 小时前
深入理解 Java Unsafe:从底层魔法到被淘汰的命运
java
宸津-代码粉碎机2 小时前
FastUtil+AI多Agent实战:Java AI项目性能终极加速方案
java·服务器·开发语言·python·安全·php
choumou_M2 小时前
SpringBoot_5:商品秒杀场景的并发实践(初步)
java·spring boot·后端
suaizai_2 小时前
AI Agent如何懂你:四层能力拆解
java·前端·人工智能
javachen__2 小时前
Docker一键清理 + 全局限制,告别日志报满
java·docker·容器