在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";
}
但我无法像ExtendedResourceContext那样实例化一些Jersey对象。
我发现了这个话题,但它似乎适用于泽西岛的第1版: How to access multiple resources in a single request : Jersey Rest
感谢您的回答,抱歉我的英语不好。
答案 0 :(得分:-1)
require 'open-uri'
require 'nokogiri'
require 'net/http'
#Store URL to be scraped
uri = URI.parse('https://secure.thameswater.co.uk/dynamic/cps/rde/xchg/corp/hs.xsl/Thames_Water_Supply.xml')
# other code here
通过使用上面的示例,您可以解决问题。这将有用