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

adapted Jersey minimal sample for CXF #129

Open
wants to merge 6 commits 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
22 changes: 22 additions & 0 deletions java/java-cxf-spring-boot-minimal/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# 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 a minimal example of integration of swagger into a CXF based app.

### 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/services/openapi.json to view the Swagger Resource Listing.
This tells you that the server is up and ready to demonstrate Swagger.

Swagger-UI is reachable at: http://localhost:8080/services/api-docs?url=/services/openapi.yaml
82 changes: 82 additions & 0 deletions java/java-cxf-spring-boot-minimal/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<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-minimal-sample-app</artifactId>
<packaging>jar</packaging>
<name>swagger-cxf-spring-boot-minimal-sample-app</name>
<version>2.0.0</version>
<properties>
<cxf.version>3.3.0-SNAPSHOT</cxf.version>
<spring-boot.version>2.1.0.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.19.5</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-rs-service-description-openapi-v3</artifactId>
<version>${cxf.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.jaxrs</groupId>
<artifactId>jackson-jaxrs-json-provider</artifactId>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package io.swagger.sample;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class MyApplication {

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

}
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,25 @@
/**
* 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.exception;

public class ApiException extends Exception{
private int code;
public ApiException (int code, String msg) {
super(msg);
this.code = code;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* 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.exception;

public class BadRequestException extends ApiException{
private int code;
public BadRequestException (int code, String msg) {
super(code, msg);
this.code = code;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* 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.exception;

public class NotFoundException extends ApiException {
private int code;
public NotFoundException (int code, String msg) {
super(code, msg);
this.code = code;
}
}
Loading