Repository: cxf-dosgi Updated Branches: refs/heads/master e9dc21950 -> ce82b08e8
[DOSGI-261] Adding Sample for Jackson provider Project: http://git-wip-us.apache.org/repos/asf/cxf-dosgi/repo Commit: http://git-wip-us.apache.org/repos/asf/cxf-dosgi/commit/ce82b08e Tree: http://git-wip-us.apache.org/repos/asf/cxf-dosgi/tree/ce82b08e Diff: http://git-wip-us.apache.org/repos/asf/cxf-dosgi/diff/ce82b08e Branch: refs/heads/master Commit: ce82b08e88e590739c5eaf56c6a6a01cdb52a9ec Parents: e9dc219 Author: Christian Schneider <[email protected]> Authored: Sat Mar 25 20:37:00 2017 +0100 Committer: Christian Schneider <[email protected]> Committed: Sat Mar 25 20:37:00 2017 +0100 ---------------------------------------------------------------------- samples/rest/impl-jackson/README.md | 12 +++ samples/rest/impl-jackson/pom.xml | 58 +++++++++++++++ .../dosgi/samples/rest/impl/JacksonIntent.java | 39 ++++++++++ .../samples/rest/impl/TaskResourceImpl.java | 78 ++++++++++++++++++++ samples/rest/pom.xml | 1 + 5 files changed, 188 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cxf-dosgi/blob/ce82b08e/samples/rest/impl-jackson/README.md ---------------------------------------------------------------------- diff --git a/samples/rest/impl-jackson/README.md b/samples/rest/impl-jackson/README.md new file mode 100644 index 0000000..6eeb0b4 --- /dev/null +++ b/samples/rest/impl-jackson/README.md @@ -0,0 +1,12 @@ +# CXF DOSGi example REST jackson + +This is a variant of the REST example and shows how to switch to jackson. + +In plain CXF we would need to do two things + +- Set the bus property skip.default.json.provider.registration=true +- Add com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider as a JAXRS provider + +In CXF-DOSGi we can set the bus property by using a special service property on out TaskResourceImpl. + +We can also set the JacksonJaxbJsonProvider by creating a separate class that offers the provider as an Remote Service Admin intent. As a last step we must add the intent to the TaskResourceImpl using another service property. http://git-wip-us.apache.org/repos/asf/cxf-dosgi/blob/ce82b08e/samples/rest/impl-jackson/pom.xml ---------------------------------------------------------------------- diff --git a/samples/rest/impl-jackson/pom.xml b/samples/rest/impl-jackson/pom.xml new file mode 100644 index 0000000..18375de --- /dev/null +++ b/samples/rest/impl-jackson/pom.xml @@ -0,0 +1,58 @@ +<?xml version='1.0' encoding='UTF-8' ?> +<!-- 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. --> +<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/maven-v4_0_0.xsd"> + + <modelVersion>4.0.0</modelVersion> + <artifactId>cxf-dosgi-samples-rest-impl-jackson</artifactId> + <packaging>bundle</packaging> + <name>CXF DOSGi Samples REST Impl</name> + + <parent> + <groupId>org.apache.cxf.dosgi.samples</groupId> + <artifactId>cxf-dosgi-samples-rest-parent</artifactId> + <version>2.2.0-SNAPSHOT</version> + <relativePath>../pom.xml</relativePath> + </parent> + + <properties> + <topDirectoryLocation>..</topDirectoryLocation> + </properties> + + <dependencies> + <dependency> + <groupId>org.apache.cxf.dosgi.samples</groupId> + <artifactId>cxf-dosgi-samples-rest-api</artifactId> + <version>${project.version}</version> + </dependency> + <dependency> + <groupId>org.apache.aries.rsa</groupId> + <artifactId>org.apache.aries.rsa.spi</artifactId> + <version>1.9.0</version> + </dependency> + <dependency> + <groupId>com.fasterxml.jackson.jaxrs</groupId> + <artifactId>jackson-jaxrs-json-provider</artifactId> + <version>2.6.6</version> + </dependency> + <dependency> + <groupId>org.apache.cxf</groupId> + <artifactId>cxf-core</artifactId> + <version>3.1.9</version> + </dependency> + <dependency> + <groupId>org.apache.cxf</groupId> + <artifactId>cxf-rt-features-logging</artifactId> + <version>3.1.10</version> + </dependency> + </dependencies> + +</project> http://git-wip-us.apache.org/repos/asf/cxf-dosgi/blob/ce82b08e/samples/rest/impl-jackson/src/main/java/org/apache/cxf/dosgi/samples/rest/impl/JacksonIntent.java ---------------------------------------------------------------------- diff --git a/samples/rest/impl-jackson/src/main/java/org/apache/cxf/dosgi/samples/rest/impl/JacksonIntent.java b/samples/rest/impl-jackson/src/main/java/org/apache/cxf/dosgi/samples/rest/impl/JacksonIntent.java new file mode 100644 index 0000000..ba38d67 --- /dev/null +++ b/samples/rest/impl-jackson/src/main/java/org/apache/cxf/dosgi/samples/rest/impl/JacksonIntent.java @@ -0,0 +1,39 @@ +/** + * 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.cxf.dosgi.samples.rest.impl; + +import java.util.Arrays; +import java.util.List; +import java.util.concurrent.Callable; + +import com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider; +import org.osgi.service.component.annotations.Component; + +@Component // +(// + property = "org.apache.cxf.dosgi.IntentName=jackson" // +) +public class JacksonIntent implements Callable<List<Object>> { + + @Override + public List<Object> call() throws Exception { + return Arrays.asList((Object)new JacksonJaxbJsonProvider()); + } + +} http://git-wip-us.apache.org/repos/asf/cxf-dosgi/blob/ce82b08e/samples/rest/impl-jackson/src/main/java/org/apache/cxf/dosgi/samples/rest/impl/TaskResourceImpl.java ---------------------------------------------------------------------- diff --git a/samples/rest/impl-jackson/src/main/java/org/apache/cxf/dosgi/samples/rest/impl/TaskResourceImpl.java b/samples/rest/impl-jackson/src/main/java/org/apache/cxf/dosgi/samples/rest/impl/TaskResourceImpl.java new file mode 100644 index 0000000..0881c53 --- /dev/null +++ b/samples/rest/impl-jackson/src/main/java/org/apache/cxf/dosgi/samples/rest/impl/TaskResourceImpl.java @@ -0,0 +1,78 @@ +/** + * 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.cxf.dosgi.samples.rest.impl; + +import java.util.HashMap; +import java.util.Map; + +import org.apache.cxf.dosgi.samples.rest.Task; +import org.apache.cxf.dosgi.samples.rest.TaskResource; +import org.osgi.service.component.annotations.Component; + +@Component// +(// + immediate = true, // + name = "TaskResource", // + property = // + { // + "service.exported.interfaces=*", // + "service.exported.configs=org.apache.cxf.rs", // + "service.exported.intents=jackson", // + "org.apache.cxf.rs.address=/tasks", // + "cxf.bus.prop.skip.default.json.provider.registration=true" + } // +) +public class TaskResourceImpl implements TaskResource { + Map<Integer, Task> taskMap; + + public TaskResourceImpl() { + taskMap = new HashMap<Integer, Task>(); + Task task = new Task(); + task.setId(1); + task.setTitle("Buy some coffee"); + task.setDescription("Take the extra strong"); + addOrUpdate(task); + task = new Task(); + task.setId(2); + task.setTitle("Finish DOSGi example"); + task.setDescription(""); + addOrUpdate(task); + } + + @Override + public Task get(Integer id) { + return taskMap.get(id); + } + + @Override + public void addOrUpdate(Task task) { + taskMap.put(task.getId(), task); + } + + @Override + public Task[] getAll() { + return taskMap.values().toArray(new Task[]{}); + } + + @Override + public void delete(Integer id) { + taskMap.remove(id); + } + +} http://git-wip-us.apache.org/repos/asf/cxf-dosgi/blob/ce82b08e/samples/rest/pom.xml ---------------------------------------------------------------------- diff --git a/samples/rest/pom.xml b/samples/rest/pom.xml index 12c8c25..5637257 100644 --- a/samples/rest/pom.xml +++ b/samples/rest/pom.xml @@ -27,6 +27,7 @@ <modules> <module>api</module> <module>impl</module> + <module>impl-jackson</module> <module>client</module> </modules>
