【问题标题】:Jersey 2 - How to access multiple resource in a single requestJersey 2 - 如何在单个请求中访问多个资源
【发布时间】:2015-08-03 12:30:10
【问题描述】:

在 Jersey 2 中,我正在尝试开发一种方法,该方法允许我传递一对(服务、方法)的 JSON 列表,该列表表示 REST 请求中资源的访问路径,并将结果聚合到单个响应中.因此,JSON 列表可能是这样的:

[
    {
        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) {
        ...
    }   
}

因此,我可以对 REST 进行一次 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";
}

但我无法实例化一些 Jersey 对象,例如 ExtendedResourceContext。

我找到了这个主题,但它似乎适用于泽西岛的第 1 版: How to access multiple resources in a single request : Jersey Rest

谢谢你的回答,对不起我的英语不好。

【问题讨论】:

  • 为什么不创建一个新资源呢?

标签: java rest jersey jersey-2.0


【解决方案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);

通过使用上面的示例,您可以解决您的问题。希望这会有所帮助

【讨论】:

    猜你喜欢
    • 2019-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多