1
votes

I've created an Adobe aem bundle. I want to expose it as a restful web-service.How can I do it. I see there are explanation for consuming a third-party rest service but not enough information as to how to expose your own AEM bundle as a rest service.

3
Why would you expose a bundle as a REST service? What are you trying to achieve that cannot be done via a Sling servlet?Imran Saeed

3 Answers

2
votes

Exposing a proper REST API is quite a challenge of its own. I'm not going to dwell on it here because there's a lot of excellent material on the web. Just google for REST and HATEOAS. The book RESTful Web APIs by Leonard Richardson and Michael Amundsen also describes the subject very nicely.

That said, I'm not sure what you mean about exposing an AEM bundle as a REST service but it's probably going to take quite a lot of design work.

An OSGi bundle (I assume that's what you meant by AEM bundle (sic!) ) can potentially expose a number of OSGi services, each with a separate set of available methods.

While you could technically draw a parallel between an OSGi service (with its own API that's basically a set of Java methods to be called by other components in the OSGi environment) and a RESTful web service (with its hypermedia-driven API available over HTTP), the design constraints for both types of services are completely different. You can't just expose an OSGi service using a RESTful web service.

What you need to do is to design a RESTful Web API and back it up with the OSGi bundle that you have.

One way this could be done is by creating a number of Sling Servlets. These servlets are themselves OSGi components and, therefore, can ingest OSGi services that your bundle already exposes.

I have no way of knowing what your API is supposed to do but if it's about storing data in the Content Repository, you should keep in mind that Sling itself is built around the principles of REST. What you want to achieve may well be doable using the OOTB servlets and appropriately composed forms (hypermedia controls).

1
votes

As AEM is built on top of REST architectural concept it exposes Restful endpoints via Servlets. You can use the default SlingServlets like GET/POST methods or write your own Servlets by extending the SlingAllMethodsServlet (i.e. Sling Servlet that accepts GETs or POSTs)

Other clients that are able to perform REST requests can sent REST requests to your AEM (calling your AEM Servlet) by performing GET or POSTs.

For an example JSON representation of the OOTB content by using the default sling GET Servlet can be seen by below urls which renders you the content in JSON format with child levels(depth content) based on the selector

http://localhost:4502/cf#/content/geometrixx-outdoors/en/men/coats.json
http://localhost:4502/content/geometrixx-outdoors/en/men/coats.1.json

from the client end there are multiple ways of calling these Servlets like by AJAX, JAVA SWING applications, HTTP FORM Post, etc,.

Some examples are shown in below articles. AJAX CALL TO SERVLET, USING .net call to AEM, Using AEM POST CALL

~Hope it helps

0
votes

if you create a class in you core project under servlets package, you can define a restful service using annotation:

@SlingServlet(paths = "/bin/pagesutils/importservlet", methods = "POST")

declaring also the method and the path. Remember to allow the path of the servlet in your dispatcher configuration.