医疗器械网站建设,做推广的注册什么公司好,如何做推广网站,电商网站建设基础教案文章目录 响应头添加版本号获取版本号添加响应处理器请求结果 打包项目后缀添加版本号和时间实现打包结果 响应头添加版本号
获取版本号
在 pom.xml 中#xff0c;在 project.version 下定义版本号 在 application.yml 获取 pom.xml 中 project.version 中的信息 添加响应处… 文章目录 响应头添加版本号获取版本号添加响应处理器请求结果 打包项目后缀添加版本号和时间实现打包结果 响应头添加版本号
获取版本号
在 pom.xml 中在 project.version 下定义版本号 在 application.yml 获取 pom.xml 中 project.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 ResponseBodyAdviceObject {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;}
}
请求结果 打包项目后缀添加版本号和时间
实现
在 pom.xml 中的 build 标签写入以下代码
build!--打包后生成文件名--finalName${project.artifactId}-${project.version}_${current.time}/finalNamepluginsplugingroupIdorg.springframework.boot/groupIdartifactIdspring-boot-maven-plugin/artifactIdconfigurationmainClasscom.chh.api.ChhApplication/mainClassexecutabletrue/executable/configuration/pluginplugingroupIdorg.codehaus.mojo/groupIdartifactIdbuild-helper-maven-plugin/artifactIdversion3.0.0/versionexecutionsexecutionidtimestamp-property/idgoalsgoaltimestamp-property/goal/goals/execution/executionsconfigurationnamecurrent.time/namepatternyyyyMMdd-HHmmss/patterntimeZoneGMT8/timeZone/configuration/plugin!-- 打包跳过测试--plugingroupIdorg.apache.maven.plugins/groupIdartifactIdmaven-surefire-plugin/artifactIdconfigurationskiptrue/skip/configuration/plugin/plugins
/build打包结果