HTTP服务REST - 如何使用多个PUT方法

时间:2015-08-06 03:45:26

标签: javascript angularjs scala rest

我正在搞乱我的路线文件,我希望能够调用另一种方法(删除而不是更新)。

代码段:

# User management API
GET     /users                      @controllers.Users.findUsers
POST    /user                       @controllers.Users.createUser
PUT     /user/:firstName/:lastName  @controllers.Users.updateUser ( firstName: String, lastName: String )
PUT     /user/:firstName/:lastName  @controllers.Users.deleteUser ( firstName: String, lastName: String )

    updateUser: (firstName, lastName, user) ->
      @$log.debug "updateUser  #{angular.toJson(user,true) }"
      deferred  = @$q.defer()

      @$http.put("/user/#{firstName}/#{lastName}",user)
        .success((data, status,headers) =>
            @$log.info("Successfully updated user - status #{status}")
            deferred.resolve(data)
        )

        .error((data,status,header) =>
            @$log.info("Faileed to update user - status #{status}")
            deferred.reject(data)
        )
        deferred.promise


    deleteUser: (firstName,lastName,user) ->
      @$log.debug "deleteUser  #{angular.toJson(user,true) }"
      deferred  = @$q.defer()

      @$http.put("/user/#{firstName}/#{lastName}",user)
        .success((data, status,headers) =>
            @$log.info("Successfully deleted user - status #{status}")
            deferred.resolve(data)
        )

      .error((data,status,header) =>
          @$log.info("Faileed to delete user - status #{status}")
          deferred.reject(data)
      )
      deferred.promise

servicesModule.service('UserService',['$log','$http','$q',UserService])

有谁知道如何区分两条PUT路线?或者更确切地说是调用特定的PUT路线?

1 个答案:

答案 0 :(得分:4)

您在删除操作中使用 put ,而您可以使用 DELETE http请求类型。所以它就像

GET users
POST user
PUT user/:firstName/:lastName
DELETE  user/:firstName/:lastName

为了区分两个相似的动作,最简单的方法是简单地改变路线,那样就像

PUT user/:firstName/:lastName
PUT user/update/:firstName/:lastName