How to create a Restful service and deploy an OSGi container?

My goal is to create a Maven project with recovery service with Eclipse. Then pack it as a kit and deploy it into the Fuse ESB karaf OSGi container. So far, I know how to use the JAX-RS API annotations, @Path @GET:

package com.restfultest; import javax.ws.rs.GET; import javax.ws.rs.Path; @Path("/example") public class ExampleService { @GET public String sayHello() { return "Hello Restful service"; } } 

My question is: 1. What maven archetype should I use? maven-archetype-webapp or quickstart?

2. How to implement the Activator? Like this?

 public class Activator implements BundleActivator { private ServiceRegistration<?> registration; public void start(BundleContext context) throws Exception { // TODO Auto-generated method stub ExampleService exampleService = new ExampleService(); registration = context.registerService( ExampleService.class.getName(), exampleService, null ); } public void stop(BundleContext context) throws Exception { // TODO Auto-generated method stub registration.unregister(); } } 

3. How to register and publish the service (for example, how to configure the address and port of the endpoint)?

I am new to osgi. Can anyone provide me some resources or a detailed tutorial?

+6
source share
3 answers
+2
source

In general, I recommend that you get the OSGI specification , it reads well and explains a lot.

0
source

Here is my 5 cents:

1. What Maven archetype should you use?

  • I used the OSGI HTTP service (quick start), check this sample, I find it was more natural.

2. How to implement an activator?

3. How to register and publish the service?

  • I recommend that you download the latest jersey-ALL-bundle and install it on your OSGI.
  • And then, in my specific case, I used the Maven Bundle Plugin to handle import in Runtime and packaging, so my pom.xml looks something like this (note the dependencies):

     ... <packaging>bundle</packaging> <build> <plugins> <!--+ + OSGi Bundle-Manifiest Generator. + --> <plugin> <groupId>org.apache.felix</groupId> <artifactId>maven-bundle-plugin</artifactId> <version>2.3.7</version> <extensions>true</extensions> <configuration> <instructions> <Import-Package>javax.servlet.*;version="[2.4,4.0)",*</Import-Package> <Bundle-Activator>com.sample.api.Activator</Bundle-Activator> <Implementation-Title>jersey-osgi-http-service-bundle</Implementation-Title> <Implementation-Version>${project.version}</Implementation-Version> </instructions> <unpackBundle>true</unpackBundle> </configuration> </plugin> </plugins> </build> <dependencies> <!--+======================+--> <!--+ REST Dependencies +--> <!--+======================+--> <dependency> <groupId>org.glassfish.jersey.containers</groupId> <artifactId>jersey-container-servlet-core</artifactId> <version>2.22.1</version> </dependency> <dependency> <groupId>org.apache.felix</groupId> <artifactId>org.apache.felix.http.bundle</artifactId> <version>2.2.0</version> </dependency> <!--+=========================================+--> <!--+ Apache Felix Framework (OSGi framework) +--> <!--+=========================================+--> <dependency> <groupId>org.apache.felix</groupId> <artifactId>org.apache.felix.framework</artifactId> <version>4.6.0</version> </dependency> </dependencies> 
  • In my specific case, I also have WebConsole installed, then my packages look like this:

      0|Active | 0|System Bundle (4.6.0) 1|Active | 1|Apache Felix Bundle Repository (2.0.2) 2|Active | 1|Apache Felix Gogo Command (0.14.0) 3|Active | 1|Apache Felix Gogo Runtime (0.12.1) 4|Active | 1|Apache Felix Gogo Shell (0.10.0) 5|Active | 1|com.sample-api (1.3.0.SNAPSHOT) 6|Active | 1|jersey-all (2.22.1) 7|Active | 1|Apache Felix Log Service (1.0.0) 8|Active | 1|Apache Felix Configuration Admin Service (1.2.4) 9|Active | 1|Apache Felix Shell Service (1.4.2) 10|Active | 1|Apache Felix Http Bundle (2.0.4) 11|Active | 1|HTTP Service (1.0.0) 12|Active | 1|Apache Felix Web Management Console (3.1.2) 
  • For relaxation, it is important to have packages 6, 10 and 11.

  • And the port is configured in OSGI "config.properties" (I used Felix), just by doing this:

     org.osgi.service.http.port=28370 
  • Then, to access the service, simply use the following path:

     http://localhost:28370/jersey-http-service/status 
0
source

All Articles