kiran.kumar M
kiran.kumar M

Reputation: 801

How to aggregate JAX-RS responses from multiple services

We have a user dashboard which is displayed after login. The dashboard is composed of multiple widgets. Each widget pulls content from separate restful service. for eg : /news /mails /questions /alerts . Each widget calls the service after it is loaded on the page. This way there are multiple webservice calls.

Is there a way to reduce multiple calls.

The way it should work is when the page is loaded first time the service should return aggregated data for all the widgets in single call.

Each service should also be available independently so that it can be used for refreshing a single widget, and for other system integration.

Note :A widget is this case is any javascript portlet which can consume json data from restful service. All services are within a single web-application.

How can we aggregate the responses from multiple services and combine into a single JSON?

Upvotes: 1

Views: 1267

Answers (2)

Friso
Friso

Reputation: 1110

Remember that json services are just methods, so to elaborate on @Vengard's answer (please accept that one as it was earlier) you cou(ld create a aggregate method which would look something like this:

@Path ("15218462")
class Test {
  @Path ("aggregate")
  public Map<String, Object> aggregate(@QueryParam("service") List<String> services) {
    Map<String, Object> result = new HashMap<>();       
      for (String serviceName : services) {
        if(serviceName.equals("mails") {
          result.put("mails", mails());
        }
        // ... etc 
      }
  }
  @Path
  public List<String> mails() {
    // .... processing code ...
  }
}

If the services are not under your control and you don't mind being dependent on 3rd parties you might want to check out something like Yahoo pipes (http://pipes.yahoo.com/pipes/)

Upvotes: 0

Vegard
Vegard

Reputation: 5072

If you can add your own JAX-RS service simply add a new service that calls each of the others and creates the aggregated response.

for instance:

GET http://myservice.com/rest/aggregate?service=news&service=mails&service=questions

The url prefix for the serivce could be supplied as a separate encoded parameter, or provided within the aggregation service.

If all of these run within the same server just use the existing API for the other services to create the aggregated response instead.

Upvotes: 1

Related Questions