Reputation: 1073
How to set base url for all controllers
@Controller("/api/hello")
class HelloController{
@Get("/greet")
fun greet(){
}
}
Instead of writing /api on each controller is there a way to write it as base url in configuration for all rest controller endpoints
Upvotes: 1
Views: 2814
Reputation: 33
Now we can use micronaut.server.context-path
.
Example,
micronaut.server.context-path=/api/v1
In the application.propeties file or yml file. Then we can access the API by,
http://localhost:51711/api/v1/example
Upvotes: 1
Reputation: 3499
You can configure once RouteBuilder.UriNamingStrategy (default implementation HyphenatedUriNamingStrategy)
application.yml
:micronaut:
context-path: /someApiPath
ConfigurableUriNamingStrategy
and extend 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)
}
}
This configurations will be applied for all controllers,
for your HelloController
URI path will be /someApiPath/greet
, if the property micronaut.context-path
is missing then /greet
:
@Controller
class HelloController {
@Get("/greet")
fun greet(){
}
}
Upvotes: 4
Reputation: 1073
No such feature is available off the shelf at the moment have to specify custom properties in application.yml and refer them from controller
eg:
@Controller(“${my.config:/api}/foo”))
Upvotes: 0