mvn archetype:generate -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=1.4 -DgroupId=com.example -DartifactId=my-osgi-project -Dversion=1.0-SNAPSHOT
<dependency>
<groupId>jakarta.enterprise</groupId>
<artifactId>jakarta.enterprise.cdi-api</artifactId>
<version>3.0.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>jakarta.ws.rs</groupId>
<artifactId>jakarta.ws.rs-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.core</artifactId>
<version>7.0.0</version>
<scope>provided</scope>
</dependency>
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
@Path("/myresource")
public class MyResource {
@GET
@Produces("text/plain")
public String getHello() {
return "Hello from RESTful service!";
}
}
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceRegistration;
import jakarta.ws.rs.core.Application;
import jakarta.ws.rs.core.UriBuilder;
import jakarta.ws.rs.ext.RuntimeDelegate;
public class Activator implements BundleActivator {
private ServiceRegistration<?> service;
public void start(BundleContext context) throws Exception {
Application application = new Application();
application.addClasses(MyResource.class);
RuntimeDelegate delegate = RuntimeDelegate.getInstance();
UriBuilder uriBuilder = delegate.createUriBuilder();
uriBuilder.path("/myapp");
service = context.registerService(Application.class.getName(), application, null);
}
public void stop(BundleContext context) throws Exception {
service.unregister();
}
}
mvn clean install