如何为所有控制器设置基本网址
@Controller("/api/hello")
class HelloController{
@Get("/greet")
fun greet(){
}
}
除了在每个控制器上编写/ api之外,还有一种方法可以将其作为所有其余控制器端点的配置中的基本url写入
答案 0 :(得分:2)
您可以一次配置一次RouteBuilder.UriNamingStrategy(默认实现HyphenatedUriNamingStrategy)
application.yml
:micronaut:
context-path: /someApiPath
ConfigurableUriNamingStrategy
并展开HyphenatedUriNamingStrategy
:@Singleton
@Replaces(HyphenatedUriNamingStrategy::class)
class ConfigurableUriNamingStrategy : HyphenatedUriNamingStrategy() {
@Value("\${micronaut.context-path}")
var contextPath: String? = null
override fun resolveUri(type: Class<*>?): String {
return contextPath ?: "" + super.resolveUri(type)
}
override fun resolveUri(beanDefinition: BeanDefinition<*>?): String {
return contextPath ?: "" + super.resolveUri(beanDefinition)
}
override fun resolveUri(property: String?): String {
return contextPath ?: "" + super.resolveUri(property)
}
override fun resolveUri(type: Class<*>?, id: PropertyConvention?): String {
return contextPath ?: "" + super.resolveUri(type, id)
}
}
此配置将应用于所有控制器,
为您的HelloController
URI路径将为/someApiPath/greet
,如果缺少属性micronaut.context-path
,则/greet
:
@Controller
class HelloController {
@Get("/greet")
fun greet(){
}
}
答案 1 :(得分:0)
目前没有可用的此类功能 必须在application.yml中指定自定义属性,然后从控制器中引用它们
例如:
@Controller(“${my.config:/api}/foo”))