POST angularjs 球衣 $resource

POST angularjs jersey with $resource

我正在尝试对我的 Jersey 应用程序执行 post 一个字符串(稍后它可能是一个数组或映射),但我无法让它工作。

球衣方法:

@GET
@javax.ws.rs.Path("/angu/{param}")
@Consumes(MediaType.APPLICATION_JSON)
public void testAngu(@PathParam("param") String param){

}

Angular 服务:

services.factory('testFactory', function ($resource) {
return $resource('/app/api/folders/angu/:param', {}, {
    save: {
        method: 'POST',
        params: {param : '@param'}
    }
})

});

Angular 控制器:

app.controller('scanController', ['$scope', 'firstScanFactory', 'testFactory', function ($scope, firstScanFactory, testFactory) {
firstScanFactory.get({}, function (firstScanFactory) {
    $scope.shows = firstScanFactory.listShows;
})

$scope.callJersey = function() {
    testFactory.save("toto");
}

}]);

最后是通话按钮:

<a class="ls-sc-button default" ng-click="callJersey()">Valider</a>

我做错了什么?

  1. 您的 Jersey 方法使用 @GET 注释,但是您的 Angular 代码使用 method: 'POST'.

    因此,泽西岛永远不会 select testAngu() 作为匹配路线。

  2. 如果使用@,则需要将参数作为对象的属性传递。 @param 字符串将告诉资源将 :param 替换为您作为参数传递给它的对象上的 属性 参数:

    testFactory.save({"param": "toto"});

API documentation 说:

If the parameter value is prefixed with @ then the value for that parameter will be extracted from the corresponding property on the data object (provided when calling an action method). For example, if the defaultParam object is {someParam: '@someProp'} then the value of someParam will be data.someProp.