我在Grails 2.3.5应用程序中设置了Restful Controller。通过PUT将对象发送到我的端点时,对象将不会更新。我的println没有显示更新的数据,但显示原始数据。我不明白为什么。
Restful controller :
import grails.rest.RestfulController
class PluginSettingController extends RestfulController {
static responseFormats = ['json', 'xml']
PluginSettingController() {
super(PluginSetting)
}
@Override
def update(PluginSetting setting){
println(setting.name)
// This prints out the old name: "Old name"
}
域类:
class PluginSetting {
String name
String value
static constraints = {
name(nullable:false, blank:false, unique: true)
value(nullable:false, blank:false)
}
}
卷曲请求:
curl -i -X PUT -d "{'name':'NEW','value':'my value', 'id':1}" http://localhost:8080/app/pluginSettings/1
旧对象(这是我将GET发送到网址“http://localhost:8080/app/pluginSettings/1”时获得的内容)
{
"class": "configuration.PluginSetting",
"id": 1,
"name": "Old name",
"value": "my value"
}
答案 0 :(得分:0)
这最终对Grails 2.5.4起作用了 - 当使用第二个控制器时,常规方法无法正常工作,这很令人惊讶。
@Transactional(readOnly=false) // not needed
class EngineController extends RestfulController<PluginSettings> {
static responseFormats = ['json', 'xml']
EngineController() {
super(PluginSettings)
}
}
class UrlMappings {
static mappings = {
"/$controller/$action?/$id?(.$format)?"{
constraints {
// apply constraints here
}
}
"/"(view:"/index")
"500"(view:'/error')
}
}
#Show
curl -i -X GET -H "Accept: application/json" localhost:8080/app/engine/index
curl -i -X GET -H "Accept: application/json" localhost:8080/app/engine/show/1
#Create
curl -i -X POST -H "Content-Type: application/json" -d '{"name":"Along Came A Spider"}' localhost:8080/app/engine/save
#Update
curl -i -X PUT -H "Content-Type: application/json" -d '{"name":"Along Came A Spider"}' localhost:8080/app/engine/update/1
#Delete
curl -i -X DELETE http://localhost:8080/app/engine/delete/1
但是当我修改urlMappings以包含
时"/api/engines"(resources:"Engine")
我必须使用
curl -i -X GET -H "Accept: application/json" localhost:8080/app/api/engines/
curl -i -X GET -H "Accept: application/json" localhost:8080/app/api/engines/1
curl -i -X POST -H "Content-Type: application/json" -d '{"name":"Along Came A Spider"}' localhost:8080/app/api/engines/
curl -i -X PUT -H "Content-Type: application/json" -d '{"name":"Along Came A Spider"}' localhost:8080/app/api/engines/1
curl -i -X DELETE http://localhost:8080/app/api/engines/1