在我的 UrlMappings 中,我定义了这个映射:
"/$controller/$action?/$id?(.$format)?"{}
现在我想添加一组版本 2 服务。
例如: URI 处的新服务: /api/myaction
我希望能够定义一个新的端点/api/v2/myaction
,其中myaction将映射到一个名为myactionV2的新操作
有多种方法可以做到这一点,最佳解决方案取决于您未包含在问题中的一些因素。这是一个最接近问题的解决方案以及 OP 在上面添加的评论。
请参阅https://github.com/jeffbrown/javaheadendpoints 上的项目。
package demo.v1
class ApiController {
static namespace = 'v1'
def myaction() {
render 'This request was handled by version 1 of the api'
}
}
package demo.v2
class ApiController {
static namespace = 'v2'
def myaction() {
render 'This request was handled by version 2 of the api'
}
}
package demo.v3
class ApiController {
static namespace = 'v3'
def myaction() {
render 'This request was handled by version 3 of the api'
}
}
package javaheadendpoints
class UrlMappings {
static mappings = {
"/$controller/$action?/$id?(.$format)?"{
constraints {
// apply constraints here
}
}
"/$controller/$namespace/$action/$id?(.$format)?" {
// ...
}
"/"(view:"/index")
"500"(view:'/error')
"404"(view:'/notFound')
}
}
发送请求会产生我认为所请求的行为:
$ curl http://localhost:8080/api/v1/myaction
This request was handled by version 1 of the api
$ curl http://localhost:8080/api/v2/myaction
This request was handled by version 2 of the api
$ curl http://localhost:8080/api/v3/myaction
This request was handled by version 3 of the api
其他选项包括使用Version
http 标头,但由于上面的一些措辞,我认为这不是您想要的。
我希望这有帮助。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句