【问题标题】:Grails 2.5 REST PUT not getting calledGrails 2.5 REST PUT没有被调用
【发布时间】:2014-07-03 02:23:07
【问题描述】:

Grails 2.4 RESTful 控制器。

我有一个基本问题。我有一个带有简单域类的 RESTful 控制器,我的 GET、POST 工作正常。 如何发送 PUT JSON 请求? 我正在使用默认的 RESTful 生成的控制器

url -i -X POST -H "Content-Type: application/json" -d '{"roleId":1,"username":"testuser5"}' http://localhost:8090/testapp/User
HTTP/1.1 201 Created
Server: Apache-Coyote/1.1
Content-Type: application/json;charset=UTF-8
Transfer-Encoding: chunked
Date: Thu, 03 Jul 2014 02:07:13 GMT

{"id":null,"userId":79,"username":"testuser5"}

然后我尝试使用与上面相同的 JSON 响应进行 PUT(删除 id:null 并更改了用户名):

curl -i -X PUT -H "Content-Type: application/json" -d '{"userId":79,"username":"testuser6"}' http://localhost:8090/testapp/User

请求进入索引,我得到用户列表。我做错了什么?如何调用“更新”方法?如果我添加自己的方法并执行 PUT,我自己的方法会被调用。

领域类:

class User {
    Integer userId
    String username

    static mapping = {
        table 'user'
        version false
        id name:'userId', column: 'user_id'
    }

    static constraints = {
        username blank:false, nullable:false
    }
}

RESTful 控制器:

class UserController extends RestfulController {
    static responseFormats = ['json', 'xml']
    static allowedMethods = [save: "POST", update: "PUT", delete: "DELETE"]

    def index(Integer max) {
        params.max = Math.min(max ?: 10, 100)
        respond User.list(params), model:[userInstanceCount: User.count()]
    }
    def show(User userInstance) {
        respond userInstance
    }
    def create() {
        respond new User(params)
    }

     @Transactional
    def update(User userInstance) {
        println "*** in update "
        if (userInstance == null) {
            notFound()
            return
        }

        if (userInstance.hasErrors()) {
            respond userInstance.errors, view:'edit'
            return
        }

        userInstance.save flush:true

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.updated.message', args: [message(code: 'User.label', default: 'User'), userInstance.id])
                redirect userInstance
            }
            '*'{ respond userInstance, [status: OK] }
        }
    }

    protected void notFound() {
        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.not.found.message', args: [message(code: 'user.label', default: 'User'), params.id])
                redirect action: "index", method: "GET"
            }
            '*'{ render status: NOT_FOUND }
        }
    }
}

【问题讨论】:

    标签: json grails


    【解决方案1】:

    【讨论】:

    • 我在'update'方法的第一行添加了prinln,当我用curl调用PUT时我没有看到它。我应该如何调用更新?在控制器开始时,我有: static allowedMethods = [save: "POST", update: "PUT", delete: "DELETE"] 我还添加了您提到的验证,但没有运气。
    • 您传递给 PUT 请求的 URL 缺少 id
    • 如果没有ID怎么办,比如ID由服务器生成,PUT方法返回?
    猜你喜欢
    • 2016-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-06
    • 1970-01-01
    • 2011-07-19
    相关资源
    最近更新 更多