You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@karaf.apache.org by jb...@apache.org on 2020/10/05 03:35:22 UTC

[karaf] branch master updated: Add REST example client using Jersey

This is an automated email from the ASF dual-hosted git repository.

jbonofre pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/karaf.git


The following commit(s) were added to refs/heads/master by this push:
     new 0e978a0  Add REST example client using Jersey
     new 7e96ac7  Merge pull request #1209 from jbonofre/REST_JERSEY_CLIENT_EXAMPLE
0e978a0 is described below

commit 0e978a0cd6175355d562239bc7944dd3f4fc4fa1
Author: jbonofre <jb...@apache.org>
AuthorDate: Sat Oct 3 06:51:40 2020 +0200

    Add REST example client using Jersey
---
 examples/karaf-rest-example/README.md              |  9 +++
 .../karaf-rest-example-client-jersey/pom.xml       | 81 ++++++++++++++++++++++
 .../rest/client/jersey/AddBookingCommand.java      | 63 +++++++++++++++++
 .../rest/client/jersey/ListBookingCommand.java     | 55 +++++++++++++++
 .../karaf-rest-example-client/pom.xml              |  1 +
 .../src/main/feature/feature.xml                   | 32 +++++++++
 .../karaf/itests/examples/RestExampleTest.java     | 11 +++
 7 files changed, 252 insertions(+)

diff --git a/examples/karaf-rest-example/README.md b/examples/karaf-rest-example/README.md
index 93ba30d..d78f18d 100644
--- a/examples/karaf-rest-example/README.md
+++ b/examples/karaf-rest-example/README.md
@@ -36,6 +36,7 @@ The "client" bundle uses the `BookingService` with a REST client stub.
 * **karaf-rest-example-client** is a regular Blueprint bundle using the `BookingService`.
 * **karaf-rest-example-client-http** is a regular Blueprint REST client bundle using Java Http.
 * **karaf-rest-example-client-cxf** is a regular Blueprint REST client bundle using Apache CXF.
+* **karaf-rest-example-client-jersey** is a regular Blueprint REST client bundle using Jakarta Jersey.
 * **karaf-rest-example-whiteboard** is another way to deploy REST services using whiteboard pattern.
 * **karaf-rest-example-features** provides a Karaf features repository used for the deployment.
 
@@ -81,6 +82,14 @@ karaf@root()> feature:install karaf-rest-example-client-cxf
 
 The `karaf-rest-example-client-cxf` feature provides `booking:*` commands you can use to call the REST service.
 
+And the service client feature using Jakarta Jersey:
+
+```
+karaf@root()> feature:install karaf-rest-example-client-jersey
+```
+
+The `karaf-rest-example-client-jersey` feature provides `booking:*`
+
 ## Usage
 
 Once you have install a client feature, you can use `booking:add` and `booking:list` commands to interact with the REST
diff --git a/examples/karaf-rest-example/karaf-rest-example-client/karaf-rest-example-client-jersey/pom.xml b/examples/karaf-rest-example/karaf-rest-example-client/karaf-rest-example-client-jersey/pom.xml
new file mode 100644
index 0000000..bf97286
--- /dev/null
+++ b/examples/karaf-rest-example/karaf-rest-example-client/karaf-rest-example-client-jersey/pom.xml
@@ -0,0 +1,81 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+
+    <!--
+
+        Licensed to the Apache Software Foundation (ASF) under one or more
+        contributor license agreements.  See the NOTICE file distributed with
+        this work for additional information regarding copyright ownership.
+        The ASF licenses this file to You under the Apache License, Version 2.0
+        (the "License"); you may not use this file except in compliance with
+        the License.  You may obtain a copy of the License at
+
+           http://www.apache.org/licenses/LICENSE-2.0
+
+        Unless required by applicable law or agreed to in writing, software
+        distributed under the License is distributed on an "AS IS" BASIS,
+        WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+        See the License for the specific language governing permissions and
+        limitations under the License.
+    -->
+
+    <modelVersion>4.0.0</modelVersion>
+
+    <parent>
+        <groupId>org.apache.karaf.examples</groupId>
+        <artifactId>karaf-rest-example-client</artifactId>
+        <version>4.3.0-SNAPSHOT</version>
+        <relativePath>../pom.xml</relativePath>
+    </parent>
+
+    <artifactId>karaf-rest-example-client-jersey</artifactId>
+    <name>Apache Karaf :: Examples :: REST :: Client :: Jersey</name>
+    <packaging>bundle</packaging>
+
+    <dependencies>
+        <dependency>
+            <groupId>javax.ws.rs</groupId>
+            <artifactId>javax.ws.rs-api</artifactId>
+            <version>2.1</version>
+        </dependency>
+        <dependency>
+            <groupId>com.fasterxml.jackson.jaxrs</groupId>
+            <artifactId>jackson-jaxrs-json-provider</artifactId>
+            <version>${jackson.version}</version>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.karaf.examples</groupId>
+            <artifactId>karaf-rest-example-api</artifactId>
+            <version>${project.version}</version>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.karaf.shell</groupId>
+            <artifactId>org.apache.karaf.shell.core</artifactId>
+        </dependency>
+    </dependencies>
+
+    <build>
+        <plugins>
+            <plugin>
+                <groupId>org.apache.karaf.tooling</groupId>
+                <artifactId>karaf-services-maven-plugin</artifactId>
+            </plugin>
+            <plugin>
+                <groupId>org.apache.felix</groupId>
+                <artifactId>maven-bundle-plugin</artifactId>
+                <configuration>
+                    <instructions>
+                        <Private-Package>
+                            org.apache.karaf.examples.rest.client.jersey
+                        </Private-Package>
+                        <Import-Package>
+                            org.apache.karaf.shell*;version="[4,5)",
+                            *
+                        </Import-Package>
+                    </instructions>
+                </configuration>
+            </plugin>
+        </plugins>
+    </build>
+
+</project>
\ No newline at end of file
diff --git a/examples/karaf-rest-example/karaf-rest-example-client/karaf-rest-example-client-jersey/src/main/java/org/apache/karaf/examples/rest/client/jersey/AddBookingCommand.java b/examples/karaf-rest-example/karaf-rest-example-client/karaf-rest-example-client-jersey/src/main/java/org/apache/karaf/examples/rest/client/jersey/AddBookingCommand.java
new file mode 100644
index 0000000..dad17bb
--- /dev/null
+++ b/examples/karaf-rest-example/karaf-rest-example-client/karaf-rest-example-client-jersey/src/main/java/org/apache/karaf/examples/rest/client/jersey/AddBookingCommand.java
@@ -0,0 +1,63 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one or more
+ *  contributor license agreements.  See the NOTICE file distributed with
+ *  this work for additional information regarding copyright ownership.
+ *  The ASF licenses this file to You under the Apache License, Version 2.0
+ *  (the "License"); you may not use this file except in compliance with
+ *  the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+package org.apache.karaf.examples.rest.client.jersey;
+
+import com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider;
+import org.apache.karaf.examples.rest.api.Booking;
+import org.apache.karaf.shell.api.action.Action;
+import org.apache.karaf.shell.api.action.Argument;
+import org.apache.karaf.shell.api.action.Command;
+import org.apache.karaf.shell.api.action.Option;
+import org.apache.karaf.shell.api.action.lifecycle.Service;
+
+import javax.ws.rs.client.Client;
+import javax.ws.rs.client.ClientBuilder;
+import javax.ws.rs.client.Entity;
+import javax.ws.rs.client.WebTarget;
+
+@Service
+@Command(scope = "booking", name = "add", description = "Add booking")
+public class AddBookingCommand implements Action {
+
+    @Argument(index = 0, name = "id", description = "Booking ID", required = true, multiValued = false)
+    long id;
+
+    @Argument(index = 1, name = "customer", description = "Customer name", required = true, multiValued = false)
+    String customer;
+
+    @Argument(index = 2, name = "flight", description = "Flight number", required = true, multiValued = false)
+    String flight;
+
+    @Option(name = "--url", description = "Location of the REST service", required = false, multiValued = false)
+    String restLocation = "http://localhost:8181/cxf/booking/";
+
+    @Override
+    public Object execute() throws Exception {
+        Booking booking = new Booking();
+        booking.setId(id);
+        booking.setFlight(flight);
+        booking.setCustomer(customer);
+
+        Client client = ClientBuilder.newClient();
+        client.register(new JacksonJsonProvider());
+        WebTarget target = client.target(restLocation);
+        target.request().post(Entity.json(booking));
+
+        return null;
+    }
+
+}
diff --git a/examples/karaf-rest-example/karaf-rest-example-client/karaf-rest-example-client-jersey/src/main/java/org/apache/karaf/examples/rest/client/jersey/ListBookingCommand.java b/examples/karaf-rest-example/karaf-rest-example-client/karaf-rest-example-client-jersey/src/main/java/org/apache/karaf/examples/rest/client/jersey/ListBookingCommand.java
new file mode 100644
index 0000000..8b6898c0
--- /dev/null
+++ b/examples/karaf-rest-example/karaf-rest-example-client/karaf-rest-example-client-jersey/src/main/java/org/apache/karaf/examples/rest/client/jersey/ListBookingCommand.java
@@ -0,0 +1,55 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one or more
+ *  contributor license agreements.  See the NOTICE file distributed with
+ *  this work for additional information regarding copyright ownership.
+ *  The ASF licenses this file to You under the Apache License, Version 2.0
+ *  (the "License"); you may not use this file except in compliance with
+ *  the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+package org.apache.karaf.examples.rest.client.jersey;
+
+import com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider;
+import org.apache.karaf.examples.rest.api.Booking;
+import org.apache.karaf.shell.api.action.Action;
+import org.apache.karaf.shell.api.action.Command;
+import org.apache.karaf.shell.api.action.Option;
+import org.apache.karaf.shell.api.action.lifecycle.Service;
+
+import javax.ws.rs.client.Client;
+import javax.ws.rs.client.ClientBuilder;
+import javax.ws.rs.client.WebTarget;
+import javax.ws.rs.core.GenericType;
+import javax.ws.rs.core.MediaType;
+
+import java.util.List;
+
+@Service
+@Command(scope = "booking", name = "list", description = "List booking")
+public class ListBookingCommand implements Action {
+
+    @Option(name = "--url", description = "Location of the REST service", required = false, multiValued = false)
+    String restLocation = "http://localhost:8181/cxf/booking/";
+
+    @Override
+    public Object execute() throws Exception {
+        Client client = ClientBuilder.newClient();
+        client.register(new JacksonJsonProvider());
+        WebTarget target = client.target(restLocation);
+
+        List<Booking> bookings = (List<Booking>) target.request(MediaType.APPLICATION_JSON).get(new GenericType<List<Booking>>() {});
+        for (Booking booking : bookings) {
+            System.out.println(booking.getId() + " " + booking.getCustomer() + " " + booking.getFlight());
+        }
+
+        return null;
+    }
+
+}
\ No newline at end of file
diff --git a/examples/karaf-rest-example/karaf-rest-example-client/pom.xml b/examples/karaf-rest-example/karaf-rest-example-client/pom.xml
index 311a9a8..3334702 100644
--- a/examples/karaf-rest-example/karaf-rest-example-client/pom.xml
+++ b/examples/karaf-rest-example/karaf-rest-example-client/pom.xml
@@ -35,6 +35,7 @@
     <modules>
         <module>karaf-rest-example-client-cxf</module>
         <module>karaf-rest-example-client-http</module>
+        <module>karaf-rest-example-client-jersey</module>
     </modules>
 
 </project>
diff --git a/examples/karaf-rest-example/karaf-rest-example-features/src/main/feature/feature.xml b/examples/karaf-rest-example/karaf-rest-example-features/src/main/feature/feature.xml
index 2f6eef8..fc6a6ac 100644
--- a/examples/karaf-rest-example/karaf-rest-example-features/src/main/feature/feature.xml
+++ b/examples/karaf-rest-example/karaf-rest-example-features/src/main/feature/feature.xml
@@ -47,6 +47,11 @@
         <bundle>mvn:org.apache.karaf.examples/karaf-rest-example-client-cxf/${project.version}</bundle>
     </feature>
 
+    <feature name="karaf-rest-example-client-jersey" version="${project.version}">
+        <feature>jersey</feature>
+        <bundle>mvn:org.apache.karaf.examples/karaf-rest-example-client-jersey/${project.version}</bundle>
+    </feature>
+
     <feature name="karaf-rest-example-whiteboard" version="${project.version}">
         <feature version="${project.version}">karaf-rest-example-common</feature>
         <feature>http-whiteboard</feature>
@@ -60,15 +65,42 @@
     <feature name="karaf-rest-example-scr" version="${project.version}">
         <feature version="${project.version}">karaf-rest-example-common</feature>
         <feature dependency="true">http</feature>
+        <feature dependency="true">pax-http-whiteboard</feature>
         <requirement>osgi.service;effective:=active;filter:=(objectClass=org.osgi.service.http.HttpService)</requirement>
         <feature dependency="true">scr</feature>
         <feature version="${cxf.version}" dependency="true">cxf-jaxrs</feature>
         <bundle dependency="true">mvn:com.fasterxml.jackson.core/jackson-core/${jackson.version}</bundle>
         <bundle dependency="true">mvn:com.fasterxml.jackson.core/jackson-annotations/${jackson.version}</bundle>
         <bundle dependency="true">mvn:com.fasterxml.jackson.core/jackson-databind/${jackson.version}</bundle>
+        <bundle dependency="true">mvn:com.fasterxml.jackson.module/jackson-module-jaxb-annotations/${jackson.version}</bundle>
         <bundle dependency="true">mvn:com.fasterxml.jackson.jaxrs/jackson-jaxrs-base/${jackson.version}</bundle>
         <bundle dependency="true">mvn:com.fasterxml.jackson.jaxrs/jackson-jaxrs-json-provider/${jackson.version}</bundle>
         <bundle>mvn:org.apache.karaf.examples/karaf-rest-example-scr/${project.version}</bundle>
     </feature>
 
+    <feature name="hk2" description="HK2 support" version="2.6.1">
+        <bundle dependency="true">mvn:org.javassist/javassist/3.25.0-GA</bundle>
+        <bundle>mvn:org.glassfish.hk2.external/jakarta.inject/2.6.1</bundle>
+        <bundle>mvn:org.glassfish.hk2/osgi-resource-locator/1.0.3</bundle>
+        <bundle>mvn:org.glassfish.hk2/hk2-locator/2.6.1</bundle>
+        <bundle>mvn:org.glassfish.hk2.external/aopalliance-repackaged/2.6.1</bundle>
+        <bundle>mvn:org.glassfish.hk2/hk2-api/2.6.1</bundle>
+        <bundle>mvn:org.glassfish.hk2/hk2-utils/2.6.1</bundle>
+    </feature>
+    <feature name="jersey-hk2" version="2.30.1">
+        <feature>hk2</feature>
+        <bundle>mvn:org.glassfish.jersey.core/jersey-common/2.30.1</bundle>
+        <bundle>mvn:org.glassfish.jersey.inject/jersey-hk2/2.30.1</bundle>
+    </feature>
+    <feature name="jersey" description="Jersey support" version="2.30.1">
+        <feature>jersey-hk2</feature>
+        <bundle>mvn:org.glassfish.jersey.containers/jersey-container-servlet/2.30.1</bundle>
+        <bundle>mvn:org.glassfish.jersey.containers/jersey-container-servlet-core/2.30.1</bundle>
+        <bundle>mvn:org.glassfish.jersey.core/jersey-server/2.30.1</bundle>
+        <bundle>mvn:org.glassfish.jersey.core/jersey-client/2.30.1</bundle>
+        <bundle>mvn:org.glassfish.jersey.media/jersey-media-jaxb/2.30.1</bundle>
+        <bundle>mvn:org.glassfish.jersey.media/jersey-media-json-jackson/2.30.1</bundle>
+        <bundle>mvn:org.glassfish.jersey.ext/jersey-entity-filtering/2.30.1</bundle>
+    </feature>
+
 </features>
diff --git a/itests/test/src/test/java/org/apache/karaf/itests/examples/RestExampleTest.java b/itests/test/src/test/java/org/apache/karaf/itests/examples/RestExampleTest.java
index 1c93415..d48142e 100644
--- a/itests/test/src/test/java/org/apache/karaf/itests/examples/RestExampleTest.java
+++ b/itests/test/src/test/java/org/apache/karaf/itests/examples/RestExampleTest.java
@@ -92,6 +92,17 @@ public class RestExampleTest extends BaseTest {
     }
 
     @Test
+    public void testScrWithJerseyClient() throws Exception {
+        setup();
+
+        installAndAssertFeature("karaf-rest-example-scr");
+
+        installAndAssertFeature("karaf-rest-example-client-jersey");
+
+        verify();
+    }
+
+    @Test
     public void testWhiteboard() throws Exception {
         setup();