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

Commit 14400e2

Browse files
committed
adapted Jersey minimal sample for CXF
1 parent 7f0fed4 commit 14400e2

16 files changed

Lines changed: 814 additions & 3 deletions

File tree

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Swagger Sample App
2+
3+
## Overview
4+
This is a java project to build a stand-alone server which implements the OpenAPI Spec. You can find out
5+
more about both the spec and the framework at http://swagger.io.
6+
7+
This sample is based on CXF and Spring Boot, and provides a minimal example of integration of swagger into a CXF based app.
8+
9+
### To run (with Maven)
10+
To run the server, run this task:
11+
12+
```
13+
mvn spring-boot:run
14+
```
15+
16+
This will start Tomcat embedded on port 8080.
17+
18+
### Testing the server
19+
Once started, you can navigate to http://localhost:8080/services/openapi.json to view the Swagger Resource Listing.
20+
This tells you that the server is up and ready to demonstrate Swagger.
21+
22+
Swagger-UI is reachable at: http://localhost:8080/services/api-docs?url=/services/openapi.yaml
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<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">
2+
<parent>
3+
<groupId>io.swagger.samples.v3</groupId>
4+
<artifactId>swagger-samples-project</artifactId>
5+
<version>2.0.0</version>
6+
<relativePath>../..</relativePath>
7+
</parent>
8+
<modelVersion>4.0.0</modelVersion>
9+
<groupId>io.swagger.samples.v3</groupId>
10+
<artifactId>swagger-cxf-spring-boot-minimal-sample-app</artifactId>
11+
<packaging>jar</packaging>
12+
<name>swagger-cxf-spring-boot-minimal-sample-app</name>
13+
<version>2.0.0</version>
14+
<properties>
15+
<cxf.version>3.2.4</cxf.version>
16+
<spring-boot.version>2.0.1.RELEASE</spring-boot.version>
17+
</properties>
18+
<build>
19+
<sourceDirectory>src/main/java</sourceDirectory>
20+
<plugins>
21+
<plugin>
22+
<groupId>org.apache.maven.plugins</groupId>
23+
<artifactId>maven-compiler-plugin</artifactId>
24+
<configuration>
25+
<source>1.8</source>
26+
<target>1.8</target>
27+
</configuration>
28+
</plugin>
29+
<plugin>
30+
<groupId>org.springframework.boot</groupId>
31+
<artifactId>spring-boot-maven-plugin</artifactId>
32+
<version>${spring-boot.version}</version>
33+
<executions>
34+
<execution>
35+
<goals>
36+
<goal>repackage</goal>
37+
</goals>
38+
</execution>
39+
</executions>
40+
</plugin>
41+
</plugins>
42+
</build>
43+
<dependencyManagement>
44+
<dependencies>
45+
<dependency>
46+
<!-- Import dependency management from Spring Boot -->
47+
<groupId>org.springframework.boot</groupId>
48+
<artifactId>spring-boot-dependencies</artifactId>
49+
<version>${spring-boot.version}</version>
50+
<type>pom</type>
51+
<scope>import</scope>
52+
</dependency>
53+
</dependencies>
54+
</dependencyManagement>
55+
<dependencies>
56+
<dependency>
57+
<groupId>io.swagger.core.v3</groupId>
58+
<artifactId>swagger-jaxrs2</artifactId>
59+
<scope>compile</scope>
60+
<version>${swagger-version}</version>
61+
</dependency>
62+
<dependency>
63+
<groupId>org.apache.cxf</groupId>
64+
<artifactId>cxf-spring-boot-starter-jaxrs</artifactId>
65+
<version>${cxf.version}</version>
66+
</dependency>
67+
<dependency>
68+
<groupId>org.webjars</groupId>
69+
<artifactId>swagger-ui</artifactId>
70+
<version>3.13.6</version>
71+
</dependency>
72+
<dependency>
73+
<groupId>org.apache.cxf</groupId>
74+
<artifactId>cxf-rt-rs-service-description-openapi-v3</artifactId>
75+
<version>${cxf.version}</version>
76+
</dependency>
77+
</dependencies>
78+
</project>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package io.swagger.sample;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
import javax.ws.rs.ApplicationPath;
7+
import javax.ws.rs.core.Application;
8+
9+
@SpringBootApplication
10+
public class MyApplication {
11+
12+
public static void main(String[] args) {
13+
SpringApplication.run(MyApplication.class, args);
14+
}
15+
16+
}
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
/**
2+
* Copyright 2016 SmartBear Software
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.swagger.sample.data;
18+
19+
import io.swagger.sample.model.Category;
20+
import io.swagger.sample.model.Pet;
21+
import io.swagger.sample.model.Tag;
22+
23+
import java.util.List;
24+
import java.util.ArrayList;
25+
26+
public class PetData {
27+
static List<Pet> pets = new ArrayList<Pet>();
28+
static List<Category> categories = new ArrayList<Category>();
29+
30+
static {
31+
categories.add(createCategory(1, "Dogs"));
32+
categories.add(createCategory(2, "Cats"));
33+
categories.add(createCategory(3, "Rabbits"));
34+
categories.add(createCategory(4, "Lions"));
35+
36+
pets.add(createPet(1, categories.get(1), "Cat 1", new String[] {
37+
"url1", "url2" }, new String[] { "tag1", "tag2" }, "available"));
38+
pets.add(createPet(2, categories.get(1), "Cat 2", new String[] {
39+
"url1", "url2" }, new String[] { "tag2", "tag3" }, "available"));
40+
pets.add(createPet(3, categories.get(1), "Cat 3", new String[] {
41+
"url1", "url2" }, new String[] { "tag3", "tag4" }, "pending"));
42+
43+
pets.add(createPet(4, categories.get(0), "Dog 1", new String[] {
44+
"url1", "url2" }, new String[] { "tag1", "tag2" }, "available"));
45+
pets.add(createPet(5, categories.get(0), "Dog 2", new String[] {
46+
"url1", "url2" }, new String[] { "tag2", "tag3" }, "sold"));
47+
pets.add(createPet(6, categories.get(0), "Dog 3", new String[] {
48+
"url1", "url2" }, new String[] { "tag3", "tag4" }, "pending"));
49+
50+
pets.add(createPet(7, categories.get(3), "Lion 1", new String[] {
51+
"url1", "url2" }, new String[] { "tag1", "tag2" }, "available"));
52+
pets.add(createPet(8, categories.get(3), "Lion 2", new String[] {
53+
"url1", "url2" }, new String[] { "tag2", "tag3" }, "available"));
54+
pets.add(createPet(9, categories.get(3), "Lion 3", new String[] {
55+
"url1", "url2" }, new String[] { "tag3", "tag4" }, "available"));
56+
57+
pets.add(createPet(10, categories.get(2), "Rabbit 1", new String[] {
58+
"url1", "url2" }, new String[] { "tag3", "tag4" }, "available"));
59+
}
60+
61+
public Pet getPetById(long petId) {
62+
for (Pet pet : pets) {
63+
if (pet.getId() == petId) {
64+
return pet;
65+
}
66+
}
67+
return null;
68+
}
69+
70+
public List<Pet> findPetByStatus(String status) {
71+
String[] statues = status.split(",");
72+
List<Pet> result = new java.util.ArrayList<Pet>();
73+
for (Pet pet : pets) {
74+
for (String s : statues) {
75+
if (s.equals(pet.getStatus())) {
76+
result.add(pet);
77+
}
78+
}
79+
}
80+
return result;
81+
}
82+
83+
public List<Pet> findPetByTags(String tags) {
84+
String[] tagList = tags.split(",");
85+
List<Pet> result = new java.util.ArrayList<Pet>();
86+
for (Pet pet : pets) {
87+
if (null != pet.getTags()) {
88+
for (Tag tag : pet.getTags()) {
89+
for (String tagListString : tagList) {
90+
if (tagListString.equals(tag.getName()))
91+
result.add(pet);
92+
}
93+
}
94+
}
95+
}
96+
return result;
97+
}
98+
99+
public void addPet(Pet pet) {
100+
if (pets.size() > 0) {
101+
for (int i = pets.size() - 1; i >= 0; i--) {
102+
if (pets.get(i).getId() == pet.getId()) {
103+
pets.remove(i);
104+
}
105+
}
106+
}
107+
pets.add(pet);
108+
}
109+
110+
static Pet createPet(long id, Category cat, String name, String[] urls,
111+
String[] tags, String status) {
112+
Pet pet = new Pet();
113+
pet.setId(id);
114+
pet.setCategory(cat);
115+
pet.setName(name);
116+
if (null != urls) {
117+
List<String> urlObjs = new ArrayList<String>();
118+
for (String urlString : urls) {
119+
urlObjs.add(urlString);
120+
}
121+
pet.setPhotoUrls(urlObjs);
122+
}
123+
List<Tag> tagObjs = new java.util.ArrayList<Tag>();
124+
int i = 0;
125+
if (null != tags) {
126+
for (String tagString : tags) {
127+
i = i + 1;
128+
Tag tag = new Tag();
129+
tag.setId(i);
130+
tag.setName(tagString);
131+
tagObjs.add(tag);
132+
}
133+
}
134+
pet.setTags(tagObjs);
135+
pet.setStatus(status);
136+
return pet;
137+
}
138+
139+
static Category createCategory(long id, String name) {
140+
Category category = new Category();
141+
category.setId(id);
142+
category.setName(name);
143+
return category;
144+
}
145+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* Copyright 2016 SmartBear Software
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.swagger.sample.exception;
18+
19+
public class ApiException extends Exception{
20+
private int code;
21+
public ApiException (int code, String msg) {
22+
super(msg);
23+
this.code = code;
24+
}
25+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* Copyright 2016 SmartBear Software
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.swagger.sample.exception;
18+
19+
public class BadRequestException extends ApiException{
20+
private int code;
21+
public BadRequestException (int code, String msg) {
22+
super(code, msg);
23+
this.code = code;
24+
}
25+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* Copyright 2016 SmartBear Software
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.swagger.sample.exception;
18+
19+
public class NotFoundException extends ApiException {
20+
private int code;
21+
public NotFoundException (int code, String msg) {
22+
super(code, msg);
23+
this.code = code;
24+
}
25+
}

0 commit comments

Comments
 (0)