SpringBoot 応答ヘッダーにバージョン番号を追加し、パッケージ化されたプロジェクトのサフィックスにバージョン番号と時刻を追加します。


応答ヘッダーにバージョン番号を追加する

バージョン番号を取得する

pom.xmlproject.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;

    @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以下のコードを記述します

<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>

パッケージ結果

ここに画像の説明を挿入

おすすめ

転載: blog.csdn.net/xiaohuihui1400/article/details/132334644
おすすめ