Angularjs 如何使用angular.js从Java REST服务获取数据

Angularjs 如何使用angular.js从Java REST服务获取数据,angularjs,jakarta-ee,Angularjs,Jakarta Ee,我在Netbeans上使用默认的JEE7 REST应用程序。我尝试调用的其余方法是: @GET @Path("{id}") @Produces({"application/xml", "application/json"}) public Customer find(@PathParam("id") Integer id) { return super.find(id); } 我可以成功地获得所有客户的列表。但是,当我使用angular在客户端获取客户ID时,它会生成以下URL: ht

我在Netbeans上使用默认的JEE7 REST应用程序。我尝试调用的其余方法是:

@GET
@Path("{id}")
@Produces({"application/xml", "application/json"})
public Customer find(@PathParam("id") Integer id) {
    return super.find(id);
}
我可以成功地获得所有客户的列表。但是,当我使用angular在客户端获取客户ID时,它会生成以下URL:

http://localhost:8080/mavenproject2/customers?0=1 
当我通过1的ID时 增加?加上0=的索引使调用失败

http://localhost:8080/mavenproject2/customers/1 works
我的服务如下所示:

   customerServices.factory('customerById', function ($resource) {
    return $resource('http://localhost:8080/mavenproject2/customers/:id', {id: "@id"}, {
        query: {method: 'GET', isArray: true}
    });
})
.controller('customerDetailsController', function ($scope, $routeParams, customerById) {
    $scope.customer = customerById.query($routeParams.customerId);

});
我的控制器如下所示:

   customerServices.factory('customerById', function ($resource) {
    return $resource('http://localhost:8080/mavenproject2/customers/:id', {id: "@id"}, {
        query: {method: 'GET', isArray: true}
    });
})
.controller('customerDetailsController', function ($scope, $routeParams, customerById) {
    $scope.customer = customerById.query($routeParams.customerId);

});

非常感谢您的帮助。

您可以在控制器中尝试以下操作:

$scope.fetchData = function() {
    $http({
        method : 'GET',
        url : 'http://localhost:8080/mavenproject2/customers/',
        params : {id : theIdFromYourCode}
    }).success(function(data) {
        // do something
    }).error(function(data, status) {
        // do something if error
    });
}; 

请注意,您必须包括$http模块。

您可以在控制器中尝试以下操作:

$scope.fetchData = function() {
    $http({
        method : 'GET',
        url : 'http://localhost:8080/mavenproject2/customers/',
        params : {id : theIdFromYourCode}
    }).success(function(data) {
        // do something
    }).error(function(data, status) {
        // do something if error
    });
}; 

请注意,您必须包含$http模块。

您应该将参数作为对象传递,并使用@指定字段。对于您的情况:

$scope.customer = customerById.query({ id: $routeParams.customerId });

您应该将参数作为对象传递,并使用@指定字段。对于您的情况:

$scope.customer = customerById.query({ id: $routeParams.customerId });

谢谢你的回复。我试图远离$http,因为我知道angular将能够与Java对话,AngularAPI必须工作;谢谢你的回复。我试图远离$http,因为我知道angular将能够与Java对话,AngularAPI必须工作;