Skip to content
This repository has been archived by the owner on Oct 30, 2023. It is now read-only.

adapted Jersey Petstore sample for CXF #128

Open
wants to merge 1 commit into
base: 2.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions java/java-cxf-spring-boot/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Swagger Sample App

## Overview
This is a java project to build a stand-alone server which implements the OpenAPI Spec. You can find out
more about both the spec and the framework at http://swagger.io.

This sample is based on CXF and Spring-Boot, and provides an example of integration of swagger into a CXF based app, with config file based configuration/initialization

### To run (with Maven)
To run the server, run this task:

```
mvn spring-boot:run
```

This will start Tomcat embedded on port 8080.

### Testing the server
Once started, you can navigate to http://localhost:8080/petstore/openapi.json to view the Swagger Resource Listing.
This tells you that the server is up and ready to demonstrate Swagger.

### Using the UI
There is an HTML5-based API tool bundled in this sample--you can view it it at [localhost:8080/services/api-docs?url=/services/openapi.yaml](localhost:8080/services/api-docs?url=/services/openapi.yaml). This lets you inspect the API using an interactive UI. You can access the source of this code from [here](https://github.com/swagger-api/swagger-ui)

### Applying an API key
The sample app has an implementation of the Swagger ApiAuthorizationFilter. This restricts access to resources
based on api-key. There are two keys defined in the sample app:

`default-key`

`special-key`

When no key is applied, the "default-key" is applied to all operations. If the "special-key" is entered, a
number of other resources are shown in the UI, including sample CRUD operations.
78 changes: 78 additions & 0 deletions java/java-cxf-spring-boot/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<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">
<parent>
<groupId>io.swagger.samples.v3</groupId>
<artifactId>swagger-samples-project</artifactId>
<version>2.0.0</version>
<relativePath>../..</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>io.swagger.samples.v3</groupId>
<artifactId>swagger-cxf-spring-boot-petstore-sample-app</artifactId>
<packaging>jar</packaging>
<name>swagger-cxf-spring-boot-petstore-sample-app</name>
<version>2.0.0</version>
<properties>
<cxf.version>3.2.4</cxf.version>
<spring-boot.version>2.0.1.RELEASE</spring-boot.version>
</properties>
<build>
<sourceDirectory>src/main/java</sourceDirectory>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot.version}</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencyManagement>
<dependencies>
<dependency>
<!-- Import dependency management from Spring Boot -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>io.swagger.core.v3</groupId>
<artifactId>swagger-jaxrs2</artifactId>
<scope>compile</scope>
<version>${swagger-version}</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-spring-boot-starter-jaxrs</artifactId>
<version>${cxf.version}</version>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>swagger-ui</artifactId>
<version>3.13.6</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-rs-service-description-openapi-v3</artifactId>
<version>${cxf.version}</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package io.swagger.sample;

import org.apache.cxf.jaxrs.openapi.OpenApiCustomizer;
import org.apache.cxf.jaxrs.openapi.OpenApiFeature;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

import javax.ws.rs.ApplicationPath;

@SpringBootApplication
public class MyApplication {

public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}

@Bean
public OpenApiFeature createOpenApiFeature() {
OpenApiFeature openApiFeature = new OpenApiFeature();
openApiFeature.setPrettyPrint(true);
OpenApiCustomizer customizer = new OpenApiCustomizer();
customizer.setDynamicBasePath(true);
openApiFeature.setCustomizer(customizer);
openApiFeature.setConfigLocation("openapi-configuration.json");
return openApiFeature;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
/**
* Copyright 2016 SmartBear Software
*
* Licensed 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 io.swagger.sample.data;

import io.swagger.sample.model.Category;
import io.swagger.sample.model.Pet;
import io.swagger.sample.model.Tag;

import java.util.List;
import java.util.ArrayList;

public class PetData {
static List<Pet> pets = new ArrayList<Pet>();
static List<Category> categories = new ArrayList<Category>();

static {
categories.add(createCategory(1, "Dogs"));
categories.add(createCategory(2, "Cats"));
categories.add(createCategory(3, "Rabbits"));
categories.add(createCategory(4, "Lions"));

pets.add(createPet(1, categories.get(1), "Cat 1", new String[] {
"url1", "url2" }, new String[] { "tag1", "tag2" }, "available"));
pets.add(createPet(2, categories.get(1), "Cat 2", new String[] {
"url1", "url2" }, new String[] { "tag2", "tag3" }, "available"));
pets.add(createPet(3, categories.get(1), "Cat 3", new String[] {
"url1", "url2" }, new String[] { "tag3", "tag4" }, "pending"));

pets.add(createPet(4, categories.get(0), "Dog 1", new String[] {
"url1", "url2" }, new String[] { "tag1", "tag2" }, "available"));
pets.add(createPet(5, categories.get(0), "Dog 2", new String[] {
"url1", "url2" }, new String[] { "tag2", "tag3" }, "sold"));
pets.add(createPet(6, categories.get(0), "Dog 3", new String[] {
"url1", "url2" }, new String[] { "tag3", "tag4" }, "pending"));

pets.add(createPet(7, categories.get(3), "Lion 1", new String[] {
"url1", "url2" }, new String[] { "tag1", "tag2" }, "available"));
pets.add(createPet(8, categories.get(3), "Lion 2", new String[] {
"url1", "url2" }, new String[] { "tag2", "tag3" }, "available"));
pets.add(createPet(9, categories.get(3), "Lion 3", new String[] {
"url1", "url2" }, new String[] { "tag3", "tag4" }, "available"));

pets.add(createPet(10, categories.get(2), "Rabbit 1", new String[] {
"url1", "url2" }, new String[] { "tag3", "tag4" }, "available"));
}

public Pet getPetById(long petId) {
for (Pet pet : pets) {
if (pet.getId() == petId) {
return pet;
}
}
return null;
}

public List<Pet> findPetByStatus(String status) {
String[] statues = status.split(",");
List<Pet> result = new java.util.ArrayList<Pet>();
for (Pet pet : pets) {
for (String s : statues) {
if (s.equals(pet.getStatus())) {
result.add(pet);
}
}
}
return result;
}

public List<Pet> findPetByTags(String tags) {
String[] tagList = tags.split(",");
List<Pet> result = new java.util.ArrayList<Pet>();
for (Pet pet : pets) {
if (null != pet.getTags()) {
for (Tag tag : pet.getTags()) {
for (String tagListString : tagList) {
if (tagListString.equals(tag.getName()))
result.add(pet);
}
}
}
}
return result;
}

public void addPet(Pet pet) {
if (pets.size() > 0) {
for (int i = pets.size() - 1; i >= 0; i--) {
if (pets.get(i).getId() == pet.getId()) {
pets.remove(i);
}
}
}
pets.add(pet);
}

static Pet createPet(long id, Category cat, String name, String[] urls,
String[] tags, String status) {
Pet pet = new Pet();
pet.setId(id);
pet.setCategory(cat);
pet.setName(name);
if (null != urls) {
List<String> urlObjs = new ArrayList<String>();
for (String urlString : urls) {
urlObjs.add(urlString);
}
pet.setPhotoUrls(urlObjs);
}
List<Tag> tagObjs = new java.util.ArrayList<Tag>();
int i = 0;
if (null != tags) {
for (String tagString : tags) {
i = i + 1;
Tag tag = new Tag();
tag.setId(i);
tag.setName(tagString);
tagObjs.add(tag);
}
}
pet.setTags(tagObjs);
pet.setStatus(status);
return pet;
}

static Category createCategory(long id, String name) {
Category category = new Category();
category.setId(id);
category.setName(name);
return category;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/**
* Copyright 2016 SmartBear Software
*
* Licensed 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 io.swagger.sample.data;

import io.swagger.sample.model.Order;

import java.util.Date;
import java.util.List;
import java.util.ArrayList;

public class StoreData {
static List<Order> orders = new ArrayList<Order>();

static {
orders.add(createOrder(1, 1, 2, new Date(), "placed"));
orders.add(createOrder(2, 1, 2, new Date(), "delivered"));
orders.add(createOrder(3, 2, 2, new Date(), "placed"));
orders.add(createOrder(4, 2, 2, new Date(), "delivered"));
orders.add(createOrder(5, 3, 2, new Date(), "placed"));
orders.add(createOrder(6, 3, 2, new Date(), "placed"));
orders.add(createOrder(7, 3, 2, new Date(), "placed"));
orders.add(createOrder(8, 3, 2, new Date(), "placed"));
orders.add(createOrder(9, 3, 2, new Date(), "placed"));
orders.add(createOrder(10, 3, 2, new Date(), "placed"));
}

public Order findOrderById(long orderId) {
for (Order order : orders) {
if (order.getId() == orderId) {
return order;
}
}
return null;
}

public void placeOrder(Order order) {
if (orders.size() > 0) {
for (int i = orders.size() - 1; i >= 0; i--) {
if (orders.get(i).getId() == order.getId()) {
orders.remove(i);
}
}
}
orders.add(order);
}

public boolean deleteOrder(long orderId) {
if (orders.size() > 0) {
for (int i = orders.size() - 1; i >= 0; i--) {
if (orders.get(i).getId() == orderId) {
orders.remove(i);
return true;
}
}
}
return false;
}

private static Order createOrder(long id, long petId, int quantity,
Date shipDate, String status) {
Order order = new Order();
order.setId(id);
order.setPetId(petId);
order.setQuantity(quantity);
order.setShipDate(shipDate);
order.setStatus(status);
return order;
}
}
Loading