JAX-RS(JSR311) is the JCP driven specification for creating REST
styled webservices in java. There are use cases where content/service needs to be exposed to third parties, though AEM itself is rest driven there is no support for creating RESTful services.
Jersey and OSGI can be brought
together to provide restful service on AEM platform. jax-rs-connector
is one such project that does this job and allows publishing Resources
as OSGI service, the good thing is you can use all existing OSGI service
via DI annotation @References.
The connector has a publisher bundle which does this job via ServiceTracker mechanism i.e. it tracks services having JAX-RS annotation(@Path, @Provider ) and hooks them into jersey i.e publishes as restful service.
AEM Implementation
To use it in AEM one needs to do following-:
Now add some Resource as OSGI Service, as below :
The connector has a publisher bundle which does this job via ServiceTracker mechanism i.e. it tracks services having JAX-RS annotation(@Path, @Provider ) and hooks them into jersey i.e publishes as restful service.
Clone the github project jax-rs-connector and build the project or download jars from http://search.maven.org/#search|ga|1|g%3A%22com.eclipsesource.jaxrs%22 .
- Install jersey-all jar via osgi console.
- Install publisher jar and it's dependency (JAX-RS Security Provider jar)
- Install (JAX-RS Gson Provider, Gson jar) for JSON response. (Optional)
Now add some Resource as OSGI Service, as below :
1: @Service
2: @Component(metatype = false)
3: @Path("/helloservice")
4: public class SampleResource {
5:
6: //You can Refrence existing osgi service
7: //@Reference
8: //private MyService service;
9:
10: @GET
11: @Path("/sayhi")
12: public String helloWorld() {
13: return "SampleResource says hi";
14: }
15:
16: @GET
17: @Path("/getoperation")
18: @Produces({MediaType.TEXT_PLAIN})
19: public String getServiceOperation() {
20: String status = false;
21: //service.dosomething return
22: return status;
23: }
24: }
Services are registered at /services path (configurable via osgi console) so the helloservice will be available at http://localhost:4502/services/helloservice/sayhi
Note: Tested on cq 6.0
References:
This comment has been removed by the author.
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteHi Apurv,
ReplyDeleteCould you please provide complete code for this, like imports and pom changes ?
Regards
Vikas