2014-11-23 110 views
0

我遇到了AngularJS問題併發出POST請求。我的JSON創造很好,但是當我將它張貼在我的REST服務,我已經得到了有問題的:爲什麼我無法使用AngularJS發送POST請求

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請求正常工作。

這裏是代碼的樣本

REST服務:

@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; 
} 

我在做什麼錯了?

回答

0

我猜你發佈時,用戶不在URL中,所以它不會匹配路由定義。

+0

仔細檢查POST方法是否需要添加。 – 2014-11-23 18:21:28

+0

這是一個答案或評論? – harishr 2014-11-23 18:24:13

+0

add方法用@POST註釋,所以用戶不必在url中。 – user3350171 2014-11-23 18:25:42