仿牌外貿(mào)網(wǎng)站百度搜索高級搜索
文章目錄
- 響應(yīng)頭添加版本號
- 獲取版本號
- 添加響應(yīng)處理器
- 請求結(jié)果
- 打包項目后綴添加版本號和時間
- 實現(xiàn)
- 打包結(jié)果
響應(yīng)頭添加版本號
獲取版本號
在 pom.xml
中,在 project.version
下定義版本號
在 application.yml
獲取 pom.xml
中 project.version
中的信息
添加響應(yīng)處理器
完整代碼如下:
通過 @Value("${project.version}")
獲取 application.yml
中的 project.version
,并寫入響應(yīng)頭
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;@Overridepublic boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType) {return true;}@Overridepublic 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;}
}
請求結(jié)果
打包項目后綴添加版本號和時間
實現(xiàn)
在 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>