mirror of
https://github.moeyy.xyz/https://github.com/GeWuYou/forgeboot
synced 2025-10-28 04:26:37 +08:00
Compare commits
2 Commits
b4c8b6036b
...
47691cd605
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
47691cd605 | ||
|
|
e6f70b0e74 |
@ -0,0 +1,30 @@
|
|||||||
|
package com.gewuyou.forgeboot.webmvc.version.config.entities;
|
||||||
|
|
||||||
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 版本属性
|
||||||
|
* <p>
|
||||||
|
* 该类用于配置和管理版本相关的属性,通过@ConfigurationProperties注解
|
||||||
|
* 将其与配置文件中以 version 为前缀的属性自动绑定
|
||||||
|
*
|
||||||
|
* @author gewuyou
|
||||||
|
* @since 2025-05-02 11:52:24
|
||||||
|
*/
|
||||||
|
@ConfigurationProperties(prefix = "version")
|
||||||
|
public class VersionProperties {
|
||||||
|
/**
|
||||||
|
* API前缀
|
||||||
|
* <p>
|
||||||
|
* 定义了API的路由前缀,用于在URL中区分不同的API版本
|
||||||
|
*/
|
||||||
|
private String apiPrefix = "/api";
|
||||||
|
|
||||||
|
public String getApiPrefix() {
|
||||||
|
return apiPrefix;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setApiPrefix(String apiPrefix) {
|
||||||
|
this.apiPrefix = apiPrefix;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,7 +1,9 @@
|
|||||||
package com.gewuyou.forgeboot.webmvc.version.config
|
package com.gewuyou.forgeboot.webmvc.version.config
|
||||||
|
|
||||||
import com.gewuyou.forgeboot.core.extension.log
|
import com.gewuyou.forgeboot.core.extension.log
|
||||||
|
import com.gewuyou.forgeboot.webmvc.version.config.entities.VersionProperties
|
||||||
import com.gewuyou.forgeboot.webmvc.version.mapping.ApiVersionRequestMappingHandlerMapping
|
import com.gewuyou.forgeboot.webmvc.version.mapping.ApiVersionRequestMappingHandlerMapping
|
||||||
|
import org.springframework.boot.context.properties.EnableConfigurationProperties
|
||||||
import org.springframework.context.annotation.Bean
|
import org.springframework.context.annotation.Bean
|
||||||
import org.springframework.context.annotation.Configuration
|
import org.springframework.context.annotation.Configuration
|
||||||
|
|
||||||
@ -12,6 +14,7 @@ import org.springframework.context.annotation.Configuration
|
|||||||
* @author gewuyou
|
* @author gewuyou
|
||||||
*/
|
*/
|
||||||
@Configuration
|
@Configuration
|
||||||
|
@EnableConfigurationProperties(VersionProperties::class)
|
||||||
open class VersionAutoConfiguration {
|
open class VersionAutoConfiguration {
|
||||||
/**
|
/**
|
||||||
* 创建并配置一个 ApiVersionRequestMappingHandlerMapping 实例
|
* 创建并配置一个 ApiVersionRequestMappingHandlerMapping 实例
|
||||||
@ -23,8 +26,8 @@ open class VersionAutoConfiguration {
|
|||||||
* @return ApiVersionRequestMappingHandlerMapping 实例,用于处理基于 API 版本的请求映射
|
* @return ApiVersionRequestMappingHandlerMapping 实例,用于处理基于 API 版本的请求映射
|
||||||
*/
|
*/
|
||||||
@Bean
|
@Bean
|
||||||
open fun apiVersionRequestMappingHandlerMapping(): ApiVersionRequestMappingHandlerMapping {
|
open fun apiVersionRequestMappingHandlerMapping(versionProperties: VersionProperties): ApiVersionRequestMappingHandlerMapping {
|
||||||
log.info("创建 API 版本请求映射处理程序映射")
|
log.info("创建 API 版本请求映射处理程序映射")
|
||||||
return ApiVersionRequestMappingHandlerMapping()
|
return ApiVersionRequestMappingHandlerMapping(versionProperties).also { it.order = Int.MIN_VALUE }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -2,6 +2,7 @@ package com.gewuyou.forgeboot.webmvc.version.mapping
|
|||||||
|
|
||||||
|
|
||||||
import com.gewuyou.forgeboot.webmvc.version.annotation.ApiVersion
|
import com.gewuyou.forgeboot.webmvc.version.annotation.ApiVersion
|
||||||
|
import com.gewuyou.forgeboot.webmvc.version.config.entities.VersionProperties
|
||||||
import org.springframework.core.annotation.AnnotatedElementUtils
|
import org.springframework.core.annotation.AnnotatedElementUtils
|
||||||
import org.springframework.web.bind.annotation.RestController
|
import org.springframework.web.bind.annotation.RestController
|
||||||
import org.springframework.web.servlet.mvc.method.RequestMappingInfo
|
import org.springframework.web.servlet.mvc.method.RequestMappingInfo
|
||||||
@ -14,7 +15,9 @@ import java.lang.reflect.Method
|
|||||||
* @since 2025-02-04 20:30:44
|
* @since 2025-02-04 20:30:44
|
||||||
* @author gewuyou
|
* @author gewuyou
|
||||||
*/
|
*/
|
||||||
class ApiVersionRequestMappingHandlerMapping : RequestMappingHandlerMapping() {
|
class ApiVersionRequestMappingHandlerMapping(
|
||||||
|
private val versionProperties: VersionProperties
|
||||||
|
) : RequestMappingHandlerMapping() {
|
||||||
/**
|
/**
|
||||||
* 判断是否处理特定类型的Bean
|
* 判断是否处理特定类型的Bean
|
||||||
* 仅处理标注了 @RestController 注解的类
|
* 仅处理标注了 @RestController 注解的类
|
||||||
@ -66,7 +69,7 @@ class ApiVersionRequestMappingHandlerMapping : RequestMappingHandlerMapping() {
|
|||||||
.map {
|
.map {
|
||||||
// 加上版本前缀
|
// 加上版本前缀
|
||||||
RequestMappingInfo
|
RequestMappingInfo
|
||||||
.paths("/api/$it")
|
.paths("${versionProperties.apiPrefix}/$it")
|
||||||
.build()
|
.build()
|
||||||
}.reduce {
|
}.reduce {
|
||||||
// 组合
|
// 组合
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user