This is a small apache karaf demo application that demonstrates how to use Jersey in Apache Karaf, and how to inject OSGi services into Jersey resources.
The application is a counter where you can click a button and see a value be incremented. The click of the button goes through a REST call and the returned value is displayed.
To avoid introducing confusing extra dependencies the demo is extremely primitive.
The focus is on using jersey in the HTTP web whiteboard.
That means there is no database stuff, just a counter value kept in memory (I have a “backend” to illustrate how an OSGi service can be injected into a jersey resource serving an HTTP path).
The “frontend” is just two javascript functions called from static HTML read from a classpath resource in the webgui servlet (again: to illustrate how a frontend calls a backend without introducing confusing details).
The sampleapp application implements a similar counter application, but with database, react frontend, users and login, and i18n support.
The demo consists of the following maven artifacts:
- jerseyinkaraf The top project which in addition to containing common configuration, and the list of modules, also creates a karaf feature repository containing the features of all bundles created in the modules, and attaches the karaf feature repository to the maven artifact
- jerseyinkaraf.servicedef which is an OSGi bundle containing interfaces and beans defining the available OSGi services
- jerseinkaraf.services which is an OSGi bundle containing implementations of the services
- jerseyinkaraf.webapi which is an OSGi bundle that defines a REST API that plugs into the Web Whiteboard Extender and exposes the OSGi services
- jerseyinkaraf.webgui which is an OSGi bundle that exposes a HTML and JavaScript application that plugs into the Web Whiteboard Extender
This is how to build and install the servlet:
- First download and install apache karaf
- Clone and build jersey-demo into the local maven repository (aka. the “maven local cache”):
git clone https://github.com/steinarb/jersey-demo.git cd jersey-demo mvn clean install
- In the apache karaf command line, install the features that pulls in the servlets with their dependencies
feature:repo-add mvn:no.priv.bang.demos.jerseyinkaraf/jerseyinkaraf/LATEST/xml/features feature:install jerseyinkaraf.webapi feature:install jerseyinkaraf.webgui
- After this, the servlet will be listening for POST requests, at the local path “/jerseyinkaraf/api/testservice” on the karaf web server, e.g. http://localhost:8181/jerseyinkaraf for a karaf server started locally, with default settings
This is how to uninstall the servlet from karaf:
- At the karaf console prompt, give the following command:
feature:uninstall webapi
This will cause repeated clicks on the “Increment” button to result in 405 responses, because the webgui servlet now intercepts the requests, but that servlet doesn’t support the POST requests used to increment the counter.
Reinstalling the webapi feature will cause the count to start again from 0.
This is a demo project and therefore have no releases.
To preserve a version of the demo project still running on karaf 4.2.x and using OSGi 6, the branch release/1.1.0 has been created for the karaf 4.2.x version.
branch | snapshot version | karaf version supported |
---|---|---|
release/1.1.0 | 1.1.0-SNAPSHOT | >=4.2.11 |
master | 2.0.0-SNAPSHOT | >=4.3.2 |
This software project is licensed under Apache License v. 2.
See the LICENSE file for details.