Java Jersey 2-如何在一个请求中访问多个资源
在Jersey 2中,我试图开发一种方法,允许我传递一个JSON耦合服务列表,该方法表示REST请求中资源的访问路径,并将结果聚合到单个响应中。因此,JSON列表可以如下所示:Java Jersey 2-如何在一个请求中访问多个资源,java,rest,jersey,jersey-2.0,Java,Rest,Jersey,Jersey 2.0,在Jersey 2中,我试图开发一种方法,允许我传递一个JSON耦合服务列表,该方法表示REST请求中资源的访问路径,并将结果聚合到单个响应中。因此,JSON列表可以如下所示: [ { service : "customerService", method : "getCustomer", params : { id:57 } }, { service : "custo
[
{
service : "customerService",
method : "getCustomer",
params : {
id:57
}
},
{
service : "customerService",
method : "getContacts",
params : {
idContact : 75
}
}
]
相应的命令bean可以如下所示:
public class Command {
private String method;
private String service;
public Command() {
}
public Command(final String service, final String method) {
this.service = service;
this.method = method;
}
public String getMethod() {
return method;
}
public String getService() {
return service;
}
public void setMethod(final String method) {
this.method = method;
}
public void setService(final String service) {
this.service = service;
}
}
@Path("/customerService")
public class CustomerService {
@GET
@Path("/getCustomer/{id}")
@Produces(MediaType.APPLICATION_JSON)
public Customer getCustomer(@PathParam("id") final int id) {
...
}
@GET
@Path("/getContacts/{idCustomer}")
@Produces(MediaType.APPLICATION_JSON)
public List<Contact> getContacts(@PathParam("idCustomer") final int idCustomer) {
...
}
}
客户服务类可以是这样的:
public class Command {
private String method;
private String service;
public Command() {
}
public Command(final String service, final String method) {
this.service = service;
this.method = method;
}
public String getMethod() {
return method;
}
public String getService() {
return service;
}
public void setMethod(final String method) {
this.method = method;
}
public void setService(final String service) {
this.service = service;
}
}
@Path("/customerService")
public class CustomerService {
@GET
@Path("/getCustomer/{id}")
@Produces(MediaType.APPLICATION_JSON)
public Customer getCustomer(@PathParam("id") final int id) {
...
}
@GET
@Path("/getContacts/{idCustomer}")
@Produces(MediaType.APPLICATION_JSON)
public List<Contact> getContacts(@PathParam("idCustomer") final int idCustomer) {
...
}
}
因此,我可以对其他人进行一次Ajax调用,获取联系人列表和客户数据,并获得一次Ajax调用
我的问题是如何调度命令以执行服务的方法。我试着这样做:
@Context
ExtendedResourceContext context;
@POST
@Path("/exec")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.TEXT_PLAIN)
public String exec(List<Command> commands) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
final List<Resource> resources = context.getResourceModel().getRootResources();
for (final Command command : commands) {
for (final Resource serviceResource : resources) {
if (serviceResource.getPath().equals("/" + command.getService())) {
System.out.println("Service found " + serviceResource.getPath());
for (final Resource methodResource : serviceResource.getChildResources()) {
if (methodResource.getPath().equals("/" + command.getMethod())) {
for (ResourceModelComponent component : methodResource.getComponents()) {
if (component instanceof ResourceMethod) {
final ResourceMethod m = (ResourceMethod) component;
if (m.getHttpMethod().equals("GET") || m.getHttpMethod().equals("POST")) {
final Invocable invocable = m.getInvocable();
Method method = invocable.getHandlingMethod();
method.invoke(this);
}
}
}
}
}
}
}
}
return "ok";
}
但是我不能实例化像ExtendedResourceContext这样的Jersey对象
我发现了这个主题,但它似乎适用于Jersey的版本1:
谢谢你的回答,很抱歉我的英语不好
JSONObject jo=new JSONObject();
JSONObject jo1=new JSONObject();
JSONArray jarr=new JSONArray();
jo.put("service","customerService");
jo.put("method","getCustomer");
jo1.put("id","57");
jo.put("params",jo1);
jarr.put(jo);
通过使用上述示例,您可以解决您的问题。希望这会有所帮助为什么不创建一个新资源?。