[英]Why are my two different angularjs services resolving to the same restful Java EE web service?
I have two angularjs services and they are supposed to call different restful services (the first one retrieves a single user and the second one returns an array of users). 我有两个angularjs服务,它们应该调用不同的Restful服务(第一个服务检索一个用户,第二个服务返回一个用户数组)。 Why are they both calling the service to get array of users?
他们俩为什么都调用该服务来获取用户?
Here are the two angularjs services: 这是两个angularjs服务:
angular.module('clearsoftDemoApp').factory('UserDetail', function ($resource) {
return $resource('http://localhost:8080/ClearsoftDemoBackend/webresources/clearsoft.demo.users/:id', {}, {
find: {method: 'GET', params: {id: '@id'}},
update: { method: 'PUT', params: {id: '@id'} },
delete: { method: 'DELETE', params: {id: '@id'} }
});
}); });
angular.module('clearsoftDemoApp').factory('Users', function ($resource) {
return $resource('http://localhost:8080/ClearsoftDemoBackend/webresources/clearsoft.demo.users', {}, {
findAll: {method: 'GET', isArray: true}
});
}); });
And here is the relevant code from the Java RESTful service: 以下是Java RESTful服务中的相关代码:
@Stateless
@Path("clearsoft.demo.users")
public class UsersFacadeREST extends AbstractFacade<Users> {
@PersistenceContext(unitName = "ClearsoftDemoBackendPU")
private EntityManager em;
public UsersFacadeREST() {
super(Users.class);
}
@GET
@Path("{id}")
@Produces({"application/xml", "application/json"})
public Users find(@PathParam("id") Integer id) {
return super.find(id);
}
@GET
@Override
@Produces({"application/xml", "application/json"})
public List<Users> findAll() {
return super.findAll();
}
The problem is that when i run this, both angularjs services seem to be calling the findAll() web service which is not my intent. 问题是,当我运行此命令时,两个angularjs服务似乎都在调用findAll()Web服务,这不是我的意图。
$resource
is meant to retrieve data from an endpoint, manipulate it and send it back and it uses a set of default parameters that can be explicitely overriden on calls. $resource
用于从端点检索数据,对其进行处理并将其发送回去,它使用一组默认参数,这些默认参数可以在调用时被显式覆盖。 Just the use the internal resource matching process of AngularJS instead defining to resouce functions, ie when defining only the first UserDetails
resource and calling http://localhost:8080/ClearsoftDemoBackend/webresources/clearsoft.demo.users
, then you will get all users loaded. 只需使用AngularJS的内部资源匹配过程来代替定义功能,即仅定义第一个
UserDetails
资源并调用http://localhost:8080/ClearsoftDemoBackend/webresources/clearsoft.demo.users
,那么您将获得所有用户加载。
For that you need to define the :id
as an optional paramter. 为此,您需要将
:id
定义为可选参数。
One side note is you may need to add the [ngResource]
module so don't forget to include it. 值得注意的是,您可能需要添加
[ngResource]
模块,因此请不要忘记添加它。
var service = angular.module("clearsoftDemoApp", ["ngResource"]);
// UserDetail Resource
service.factory('UserDetail', function ($resource) {
return $resource(
'/ClearsoftDemoBackend/webresources/clearsoft.demo.users/:id',
{id: "@id" },
{
find: {method: 'GET'},
update: { method: 'PUT'},
delete: { method: 'DELETE'}
});
});
The below snippet defines a UserDetail
module which let you fire below calls: 以下代码段定义了一个
UserDetail
模块,可让您在调用下方触发:
// Get all users
var users = UserDetail.query(); // Calls: GET /ClearsoftDemoBackend/webresources/clearsoft.demo.users
// Get user with iD 1
var user = UserDetail.get({},{'id': 1}); // Calls: GET /ClearsoftDemoBackend/webresources/clearsoft.demo.users/1
// Find user with iD 1
var user = UserDetail.find({},{'id': 1}); // Calls: GET /ClearsoftDemoBackend/webresources/clearsoft.demo.users/1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.