Skip to content

Commit 3f18a37

Browse files
committed
added spring boot sample
1 parent 445906d commit 3f18a37

20 files changed

+1152
-0
lines changed

java/java-spring-boot/README.md

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Swagger + SpringFox + SpringBoot example
2+
3+
This is a simple example to demonstrate Swagger with Spring Boot. The integration
4+
is handled by SpringFox, a 3rd party library for enabling Swagger on Spring MVC projects.
5+
6+
For more information on SpringFox, pleas visit [http://springfox.io](http://springfox.io)
7+
8+
## Building
9+
10+
This project requires Java 7 or greater.
11+
12+
To build:
13+
14+
```
15+
mvn clean package
16+
```
17+
18+
This produces an exectuable jar in the `target` folder.
19+
20+
To run:
21+
22+
```
23+
java -jar target/swaggerhub-spring-boot-sample-1.0.0-SNAPSHOT.jar
24+
```
25+

java/java-spring-boot/pom.xml

+148
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
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+
<modelVersion>4.0.0</modelVersion>
3+
<groupId>io.swagger</groupId>
4+
<artifactId>swaggerhub-spring-boot-sample</artifactId>
5+
<packaging>jar</packaging>
6+
<name>swaggerhub-spring-boot-sample</name>
7+
<version>1.0.0-SNAPSHOT</version>
8+
<prerequisites>
9+
<maven>2.2.0</maven>
10+
</prerequisites>
11+
12+
<build>
13+
<finalName>${project.artifactId}-${project.version}</finalName>
14+
<plugins>
15+
<plugin>
16+
<groupId>org.apache.maven.plugins</groupId>
17+
<artifactId>maven-jar-plugin</artifactId>
18+
<version>2.4</version>
19+
<configuration>
20+
<excludes>
21+
<exclude>**/logback.xml</exclude>
22+
</excludes>
23+
</configuration>
24+
</plugin>
25+
<!-- allows the src/gen/java folder to be used for compilation -->
26+
<plugin>
27+
<groupId>org.codehaus.mojo</groupId>
28+
<artifactId>build-helper-maven-plugin</artifactId>
29+
<version>1.9.1</version>
30+
<executions>
31+
<execution>
32+
<id>add-source</id>
33+
<phase>generate-sources</phase>
34+
<goals>
35+
<goal>add-source</goal>
36+
</goals>
37+
<configuration>
38+
<sources>
39+
<source>src/gen/java</source>
40+
</sources>
41+
</configuration>
42+
</execution>
43+
</executions>
44+
</plugin>
45+
<!-- plugin for running unit tests and local execution -->
46+
<plugin>
47+
<groupId>org.eclipse.jetty</groupId>
48+
<artifactId>jetty-maven-plugin</artifactId>
49+
<version>${jetty-version}</version>
50+
<configuration>
51+
<webApp>
52+
<contextPath>/</contextPath>
53+
</webApp>
54+
<webAppSourceDirectory>target/${project.artifactId}-${project.version}</webAppSourceDirectory>
55+
<stopPort>8079</stopPort>
56+
<stopKey>stopit</stopKey>
57+
<httpConnector>
58+
<port>8080</port>
59+
<idleTimeout>60000</idleTimeout>
60+
</httpConnector>
61+
</configuration>
62+
<executions>
63+
<execution>
64+
<id>start-jetty</id>
65+
<phase>pre-integration-test</phase>
66+
<goals>
67+
<goal>start</goal>
68+
</goals>
69+
<configuration>
70+
<scanIntervalSeconds>0</scanIntervalSeconds>
71+
</configuration>
72+
</execution>
73+
<execution>
74+
<id>stop-jetty</id>
75+
<phase>post-integration-test</phase>
76+
<goals>
77+
<goal>stop</goal>
78+
</goals>
79+
</execution>
80+
</executions>
81+
</plugin>
82+
<plugin>
83+
<groupId>org.springframework.boot</groupId>
84+
<artifactId>spring-boot-maven-plugin</artifactId>
85+
<configuration>
86+
<mainClass>io.swagger.sample.Application</mainClass>
87+
</configuration>
88+
<executions>
89+
<execution>
90+
<goals>
91+
<goal>repackage</goal>
92+
</goals>
93+
</execution>
94+
</executions>
95+
</plugin>
96+
<plugin>
97+
<groupId>org.apache.maven.plugins</groupId>
98+
<artifactId>maven-compiler-plugin</artifactId>
99+
<configuration>
100+
<source>1.6</source>
101+
<target>1.6</target>
102+
</configuration>
103+
</plugin>
104+
</plugins>
105+
</build>
106+
<dependencies>
107+
<dependency>
108+
<groupId>org.springframework.boot</groupId>
109+
<artifactId>spring-boot-starter</artifactId>
110+
<version>${spring-boot-version}</version>
111+
</dependency>
112+
<dependency>
113+
<groupId>org.springframework.boot</groupId>
114+
<artifactId>spring-boot-starter-jetty</artifactId>
115+
<version>${spring-boot-version}</version>
116+
</dependency>
117+
<dependency>
118+
<groupId>org.springframework</groupId>
119+
<artifactId>spring-webmvc</artifactId>
120+
<version>${spring-version}</version>
121+
</dependency>
122+
<dependency>
123+
<groupId>io.springfox</groupId>
124+
<artifactId>springfox-swagger2</artifactId>
125+
<version>${springfox-version}</version>
126+
</dependency>
127+
<dependency>
128+
<groupId>io.swagger</groupId>
129+
<artifactId>swagger-core</artifactId>
130+
<version>${swagger-core-version}</version>
131+
</dependency>
132+
<dependency>
133+
<groupId>org.hibernate</groupId>
134+
<artifactId>hibernate-validator</artifactId>
135+
<version>${hibernate-validator-version}</version>
136+
</dependency>
137+
</dependencies>
138+
<properties>
139+
<jetty-version>9.2.9.v20150224</jetty-version>
140+
<slf4j-version>1.6.3</slf4j-version>
141+
<junit-version>4.8.1</junit-version>
142+
<spring-boot-version>1.4.0.RELEASE</spring-boot-version>
143+
<springfox-version>2.5.0</springfox-version>
144+
<swagger-core-version>1.5.10</swagger-core-version>
145+
<spring-version>4.3.2.RELEASE</spring-version>
146+
<hibernate-validator-version>5.2.4.Final</hibernate-validator-version>
147+
</properties>
148+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package io.swagger.sample;
2+
3+
import com.fasterxml.jackson.annotation.JsonInclude;
4+
import com.fasterxml.jackson.databind.DeserializationFeature;
5+
import com.fasterxml.jackson.databind.ObjectMapper;
6+
import com.fasterxml.jackson.databind.SerializationFeature;
7+
import com.google.common.base.Predicates;
8+
import io.swagger.sample.resource.PetResource;
9+
import io.swagger.sample.resource.StoreResource;
10+
import io.swagger.sample.utils.SimpleCORSFilter;
11+
import org.springframework.boot.SpringApplication;
12+
import org.springframework.boot.autoconfigure.SpringBootApplication;
13+
import org.springframework.context.ApplicationContext;
14+
import org.springframework.context.annotation.Bean;
15+
import org.springframework.context.annotation.ComponentScan;
16+
import org.springframework.context.annotation.Primary;
17+
import org.springframework.stereotype.Component;
18+
import springfox.documentation.builders.ApiInfoBuilder;
19+
import springfox.documentation.builders.PathSelectors;
20+
import springfox.documentation.service.ApiInfo;
21+
import springfox.documentation.service.Contact;
22+
import springfox.documentation.spi.DocumentationType;
23+
import springfox.documentation.spring.web.plugins.Docket;
24+
import springfox.documentation.swagger2.annotations.EnableSwagger2;
25+
26+
@SpringBootApplication
27+
@EnableSwagger2
28+
@ComponentScan(basePackageClasses = {
29+
PetResource.class,
30+
StoreResource.class,
31+
SimpleCORSFilter.class
32+
})
33+
public class Application {
34+
public static void main(String[] args) {
35+
ApplicationContext ctx = SpringApplication.run(Application.class, args);
36+
}
37+
38+
@Bean
39+
public Docket swaggerSpringMvcPlugin() {
40+
return new Docket(DocumentationType.SWAGGER_2)
41+
.useDefaultResponseMessages(false)
42+
.apiInfo(apiInfo())
43+
.select()
44+
.paths(Predicates.not(PathSelectors.regex("/error.*")))
45+
.build();
46+
}
47+
48+
@Component
49+
@Primary
50+
public class CustomObjectMapper extends ObjectMapper {
51+
public CustomObjectMapper() {
52+
setSerializationInclusion(JsonInclude.Include.NON_NULL);
53+
configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
54+
configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
55+
configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
56+
enable(SerializationFeature.INDENT_OUTPUT);
57+
}
58+
}
59+
60+
private ApiInfo apiInfo() {
61+
return new ApiInfoBuilder()
62+
.title("Springfox petstore API")
63+
.description("description")
64+
.contact(new Contact("name", "url", "email"))
65+
.license("Apache License Version 2.0")
66+
.licenseUrl("https://github.com/springfox/springfox/blob/master/LICENSE")
67+
.version("2.0")
68+
.build();
69+
}
70+
}
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.models.Category;
20+
import io.swagger.sample.models.Pet;
21+
import io.swagger.sample.models.Tag;
22+
23+
import java.util.ArrayList;
24+
import java.util.List;
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 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 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 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+
}

0 commit comments

Comments
 (0)