为什么我不能用AngularJs发出POST请求

时间:2014-11-23 18:14:58

标签: json angularjs jax-rs

我遇到AngularJS问题并发出POST请求。我的json创建很好,但是当我在我的REST服务上发布它时,我遇到了问题:

WARNING: No operation matching request path "/ws/resources/Users/Add" is found, Relative Path: /Add, HTTP Method: POST, ContentType: application/json, Accept: application/json,text/plain,*/*,. Please enable FINE/TRACE log level for more details.

我真的不知道为什么,因为我的GET请求正常。

以下是代码示例

休息服务:

@Path("/Users")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public class UserRESTResource {

    @GET
    @Path("/All")
    public List<UserDto> getClients() {
        //that works fine
    }

    @POST
    @Path("/Add/{user}")
    public void add(@PathParam("user") UserDto userDto) {
        //to sth
    }
}

这就是我如何使GET请求有效:

$http.get('resources/Users/All').success(function(data) {
        $scope.clients = data;
    });

这就是我制作json并发出POST请求的方式:

$scope.Add = function() {
        $http({
              method: "POST",
              url: "resources/Users/Add",
              data: {
                  "user" : {
                      "firstName" : $scope.firstNameA,
                      "lastName" : $scope.lastNameA,
                  }
              },
              headers: {'Content-Type':'application/json'}
            });
    }

UserDto:

@XmlRootElement(name = "user")
@XmlAccessorType(XmlAccessType.FIELD)
public class UserDto {
public long id;
public String firstName;
public String lastName;
}

我做错了什么?

1 个答案:

答案 0 :(得分:0)

我猜想当你发帖时,用户不在网址中,所以它不符合路线定义。

相关问题