2011-08-04 25 views
0

我有一个用户模型,我提供了一个RESTful API,以便客户端可以调用以更新给定用户的必要数据。以下是我用来更新用户数据的代码和curl命令,但它无论如何都不起作用。我的代码/命令有什么问题吗?使用PUT更新模型数据

和我有以下路径设置

PUT  /user/{<\d+>id}/?      Users.update 

// Updates a single user. 
public static void update(Long id) { 
    // Fetch from user from DB 
    User user = safeFindById(id); 

    // Set new values 
    user.edit("user", params.all()); 

    // Persist user 
    user.validateAndSave(); 

    // return the user rendered based on template 
    get(id); 
} 

使用curl - 添加新用户

$ curl d '{"email":"[email protected]","password":"secret","firstname":"who","lastname":"is"}' -X POST http://localhost:9001/user/add 

要更新的用户,这两个命令下面没有工作

$ curl -H "Accept: application/json" -X PUT -d "firstname=test" http://localhost:9001/user/1 

$ curl -H "Accept: application/json" -H "X-HTTP-Method-Override: PUT" -X POST -d "firstname=test" http://localhost:9001/user/1 
+0

你看到的错误是什么?什么不工作? –

+0

我刚刚创建了一个简单的项目(没有数据模型),并且curl PUT工作正常 - 或者至少它用params调用了正确的控制器方法。你能粘贴你得到的错误/异常吗? –

+0

我没有看到任何返回的错误,但名字的值没有从“who”改为“test”。所以我的卷曲命令看起来不错? – tom

回答

0

没有完整的路线和异常很难分析问题。此外,我不知道如果你的用户对象真的是firstname而不是firstName。我会建议改变方法:

public static void update(Long id, String firstname) { 
    Logger.error(firstname); 
    .... 
} 

所以你可以检查错误是否在请求中。