Skip to content
Permalink
Browse files
Add swagger config to rest example
  • Loading branch information
cschneider committed Apr 11, 2017
1 parent 1fbc194 commit 89d9b395551dbc1b6107046751a5f11b699984fd
Showing 7 changed files with 45 additions and 9 deletions.
@@ -16,7 +16,7 @@ Unpack karaf 4 into a server and client directory.
Start the server karaf

```
feature:repo-add cxf-dosgi-samples 2.0.0
feature:repo-add cxf-dosgi-samples 2.2.0-SNAPSHOT
feature:install cxf-dosgi-sample-rest-impl
rsa:endpoints
```
@@ -40,6 +40,12 @@
<artifactId>org.apache.servicemix.specs.jsr339-api-2.0.1</artifactId>
<version>2.6.0</version>
</dependency>
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-annotations</artifactId>
<version>1.5.13</version>
<optional>true</optional>
</dependency>
</dependencies>

</project>
@@ -25,6 +25,9 @@
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;

import io.swagger.annotations.Api;

@Api
@Path("")
@Produces("application/xml")
public interface TaskResource {
@@ -0,0 +1,3 @@
Import-Package: \
io.swagger.jaxrs,\
*
@@ -38,6 +38,12 @@
<artifactId>cxf-dosgi-common</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-rs-service-description-swagger</artifactId>
<version>3.1.9</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.aries.rsa</groupId>
<artifactId>org.apache.aries.rsa.spi</artifactId>
@@ -20,23 +20,24 @@

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.Callable;

import com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider;
import org.apache.cxf.dosgi.common.api.IntentsProvider;
import org.osgi.service.component.annotations.Component;


/**
* Only needed if jackson is used thorugh an external intent
* Not needed in the current example config.
* This shows how to export a custom intent.
*/
@Component //
(//
property = "org.apache.cxf.dosgi.IntentName=jackson" //
)
public class JacksonIntent implements Callable<List<Object>> {
public class JacksonIntent implements IntentsProvider {

@Override
public List<Object> call() throws Exception {
public List<?> getIntents() {
return Arrays.asList((Object)new JacksonJaxbJsonProvider());
}

@@ -18,15 +18,20 @@
*/
package org.apache.cxf.dosgi.samples.rest.impl;

import java.util.Arrays;
import static java.util.Arrays.asList;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider;
import io.swagger.annotations.Api;
import org.apache.cxf.dosgi.common.api.IntentsProvider;
import org.apache.cxf.dosgi.samples.rest.Task;
import org.apache.cxf.dosgi.samples.rest.TaskResource;
import org.apache.cxf.ext.logging.LoggingFeature;
import org.apache.cxf.feature.Features;
import org.apache.cxf.jaxrs.swagger.Swagger2Feature;
import org.osgi.service.component.annotations.Component;

@Component//
@@ -37,12 +42,13 @@
{ //
"service.exported.interfaces=*", //
"service.exported.configs=org.apache.cxf.rs", //
//"service.exported.intents=jackson", // Only needed when defining jackson as external intent
"org.apache.cxf.rs.address=/tasks", //
// By default CXF will favor the default json provider
"cxf.bus.prop.skip.default.json.provider.registration=true"
} //
)
@Api(value = "mytest")
@Features(classes = {LoggingFeature.class})
public class TaskResourceImpl implements TaskResource, IntentsProvider {
Map<Integer, Task> taskMap;

@@ -59,7 +65,7 @@ public TaskResourceImpl() {
task.setDescription("");
addOrUpdate(task);
}

@Override
public Task get(Integer id) {
return taskMap.get(id);
@@ -70,6 +76,7 @@ public void addOrUpdate(Task task) {
taskMap.put(task.getId(), task);
}


@Override
public Task[] getAll() {
return taskMap.values().toArray(new Task[]{});
@@ -82,7 +89,17 @@ public void delete(Integer id) {

@Override
public List<?> getIntents() {
return Arrays.asList(new JacksonJaxbJsonProvider());
return asList(createSwaggerFeature(), new JacksonJaxbJsonProvider());
}

private Swagger2Feature createSwaggerFeature() {
Swagger2Feature swagger = new Swagger2Feature();
//swagger2Feature.setBasePath("/cxf/");
swagger.setUsePathBasedConfig(true);
swagger.setPrettyPrint(true);
swagger.setSupportSwaggerUi(true);
swagger.setScan(false);
return swagger;
}

}

0 comments on commit 89d9b39

Please sign in to comment.