This is a karaf application that demonstrates using Jersey to provide a REST API in karaf, and demonstrates how OSGi services can be injected into Jersey resources created by the Jersey framework. https://steinar.bang.priv.no/2018/08/03/use-jersey-to-provide-rest-apis-from-karaf-applications/

Steinar Bang dbb81c1d7a Replace java 11 with java 17 in github actions CI build vor 5 Monaten
.github dbb81c1d7a Replace java 11 with java 17 in github actions CI build vor 5 Monaten
jacoco-coverage-report 917efb7e04 Bump snapshot version for the karaf 4.3.x version of the demo to 2.0.0-SNAPSHOT vor 3 Jahren
jerseyinkaraf.servicedef 37898b6dbf Change Count from bean to record vor 5 Monaten
jerseyinkaraf.services 37898b6dbf Change Count from bean to record vor 5 Monaten
jerseyinkaraf.webapi ee613ea0df Add a servlet name to the webgui servlet to fix 404 Not found on the app vor 5 Monaten
jerseyinkaraf.webcontext 38f6202243 Use var for local variables in tests vor 5 Monaten
jerseyinkaraf.webgui 6037dea37a Add xml:lang attribute for English to the static index.xhtml file vor 5 Monaten
src 09845c36b5 Create the Jersey in Karaf demo vor 6 Jahren
.editorconfig bfe18d049d Initial commit. vor 6 Jahren
.gitignore bfe18d049d Initial commit. vor 6 Jahren
LICENSE bfe18d049d Initial commit. vor 6 Jahren
README.org e5a389a83e Remove sonarqube complaint about not using constructor dependency injection, since HK2 in jersey can't do constructor dependency injection vor 5 Monaten
pom.xml e5a389a83e Remove sonarqube complaint about not using constructor dependency injection, since HK2 in jersey can't do constructor dependency injection vor 5 Monaten

README.org

Using Jersey in Apache Karaf

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.

Status

file:https://github.com/steinarb/jersey-demo/actions/workflows/jersey-demo-maven-ci-build.yml/badge.svg file:https://coveralls.io/repos/github/steinarb/jersey-demo/badge.svg file:https://sonarcloud.io/api/project_badges/measure?project=steinarb_jersey-demo&metric=alert_status#.svg file:https://maven-badges.herokuapp.com/maven-central/no.priv.bang.demos.jerseyinkaraf/jersey-demo/badge.svg

file:https://sonarcloud.io/images/project_badges/sonarcloud-white.svg

The structure of the Demo

file:https://sonarcloud.io/api/project_badges/measure?project=steinarb_jersey-demo&metric=sqale_index#.svg file:https://sonarcloud.io/api/project_badges/measure?project=steinarb_jersey-demo&metric=coverage#.svg file:https://sonarcloud.io/api/project_badges/measure?project=steinarb_jersey-demo&metric=ncloc#.svg file:https://sonarcloud.io/api/project_badges/measure?project=steinarb_jersey-demo&metric=code_smells#.svg file:https://sonarcloud.io/api/project_badges/measure?project=steinarb_jersey-demo&metric=sqale_rating#.svg file:https://sonarcloud.io/api/project_badges/measure?project=steinarb_jersey-demo&metric=security_rating#.svg file:https://sonarcloud.io/api/project_badges/measure?project=steinarb_jersey-demo&metric=bugs#.svg file:https://sonarcloud.io/api/project_badges/measure?project=steinarb_jersey-demo&metric=vulnerabilities#.svg file:https://sonarcloud.io/api/project_badges/measure?project=steinarb_jersey-demo&metric=duplicated_lines_density#.svg file:https://sonarcloud.io/api/project_badges/measure?project=steinarb_jersey-demo&metric=reliability_rating#.svg

    The demo consists of the following maven artifacts:
  1. 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
  2. jerseyinkaraf.servicedef which is an OSGi bundle containing interfaces and beans defining the available OSGi services
  3. jerseinkaraf.services which is an OSGi bundle containing implementations of the services
  4. jerseyinkaraf.webapi which is an OSGi bundle that defines a REST API that plugs into the Web Whiteboard Extender and exposes the OSGi services
  5. jerseyinkaraf.webgui which is an OSGi bundle that exposes a HTML and JavaScript application that plugs into the Web Whiteboard Extender

How to install

    This is how to build and install the servlet:
  1. First download and install apache karaf
  2. Clone and build jersey-demo into the local maven repository (aka. the "maven local cache"):
  3. #+BEGIN_EXAMPLE git clone https://github.com/steinarb/jersey-demo.git cd jersey-demo mvn clean install #+END_EXAMPLE
  4. In the apache karaf command line, install the features that pulls in the servlets with their dependencies
  5. #+BEGIN_EXAMPLE feature:repo-add mvn:no.priv.bang.demos.jerseyinkaraf/jerseyinkaraf/LATEST/xml/features feature:install jerseyinkaraf.webapi feature:install jerseyinkaraf.webgui #+END_EXAMPLE
  6. 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

To uninstall the counter servlet from karaf

    This is how to uninstall the servlet from karaf:
  1. At the karaf console prompt, give the following command:
  2. #+BEGIN_EXAMPLE feature:uninstall webapi #+END_EXAMPLE

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.

Release branches

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

License

This software project is licensed under Apache License v. 2.

See the LICENSE file for details.