Grails 动态 Rest 端点映射
Grails Dynamic Rest Endpoint Mapping
在我的 UrlMappings 中,我定义了这个映射:
"/$controller/$action?/$id?(.$format)?"{}
现在我想添加一组版本 2 服务。
例如:
URI 的新服务:/api/myaction
我希望能够定义一个新端点 /api/v2/myaction
,其中 myaction 将映射到一个名为 myactionV2[=23= 的新操作]
不应该那样做,我建议的方法是分成两个控制器
/api1/myaction
/api2/myaction
或正在行动
/api/myaction1
/api/myaction2
有多种方法可以做到这一点,最佳解决方案取决于您未包含在问题中的一些因素。这是最接近问题的解决方案和 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 header,但由于上面的一些措辞,我认为这不会完全符合您的要求。
希望对您有所帮助。
在我的 UrlMappings 中,我定义了这个映射:
"/$controller/$action?/$id?(.$format)?"{}
现在我想添加一组版本 2 服务。
例如:
URI 的新服务:/api/myaction
我希望能够定义一个新端点 /api/v2/myaction
,其中 myaction 将映射到一个名为 myactionV2[=23= 的新操作]
不应该那样做,我建议的方法是分成两个控制器
/api1/myaction
/api2/myaction
或正在行动
/api/myaction1
/api/myaction2
有多种方法可以做到这一点,最佳解决方案取决于您未包含在问题中的一些因素。这是最接近问题的解决方案和 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 header,但由于上面的一些措辞,我认为这不会完全符合您的要求。
希望对您有所帮助。