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

Commit d7bb19e

Browse files
committed
additional multi swagger contexts samples
1 parent 9d0a896 commit d7bb19e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+3935
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Swagger Sample App
2+
3+
This is the pet store sample hosted at http://petstore.swagger.io!
4+
5+
## Overview
6+
This is a java project to build a stand-alone server which implements the OpenAPI Spec. You can find out
7+
more about both the spec and the framework at http://swagger.io.
8+
9+
### To run (with Maven)
10+
To run the server, run this task:
11+
12+
```
13+
mvn package -Dlog4j.configuration=file:./conf/log4j.properties jetty:run
14+
```
15+
16+
This will start Jetty embedded on port 8002.
17+
18+
### Testing the server
19+
This sample demonstrate configuring two different "swagger contexts" (config + scanner) defined in two separate jersey servlets in same web app.
20+
Once started, you can navigate to http://localhost:8002/api1/swagger.json and http://localhost:8002/api2/swagger.json to view the Swagger Resource Listing.
21+
This tells you that the server is up and ready to demonstrate Swagger.
22+
23+
### Using the UI
24+
There is an HTML5-based API tool bundled in this sample--you can view it it at [http://localhost:8002](http://localhost:8002). 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)
25+
26+
### Applying an API key
27+
The sample app has an implementation of the Swagger ApiAuthorizationFilter. This restricts access to resources
28+
based on api-key. There are two keys defined in the sample app:
29+
30+
<li>- default-key</li>
31+
32+
<li>- special-key</li>
33+
34+
When no key is applied, the "default-key" is applied to all operations. If the "special-key" is entered, a
35+
number of other resources are shown in the UI, including sample CRUD operations.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
log4j.rootCategory=ERROR, CONSOLE, LOGFILE
2+
3+
log4j.logger.io.swagger=ERROR
4+
log4j.logger.org.atmosphere=ERROR
5+
6+
# CONSOLE is set to be a ConsoleAppender using a PatternLayout.
7+
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
8+
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
9+
log4j.appender.CONSOLE.layout.ConversionPattern=%p %d{yyyy-MM-dd HH:mm:ss.SSS Z} %c{1} - %m%n
10+
11+
# LOGFILE is set to be a File appender using a PatternLayout.
12+
log4j.appender.LOGFILE=org.apache.log4j.RollingFileAppender
13+
log4j.appender.LOGFILE.File=logs/swagger.log
14+
log4j.appender.LOGFILE.Append=true
15+
log4j.appender.LOGFILE.layout=org.apache.log4j.PatternLayout
16+
log4j.appender.LOGFILE.layout.ConversionPattern=%p %d{yyyy-MM-dd HH:mm:ss.SSS Z} %c{1} - %m%n
17+
log4j.appender.LOGFILE.MaxFileSize=10MB
18+
log4j.appender.LOGFILE.MaxBackupIndex=10
Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
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</groupId>
4+
<artifactId>swagger-samples-project</artifactId>
5+
<version>1.0.0</version>
6+
<relativePath>../..</relativePath>
7+
</parent>
8+
<modelVersion>4.0.0</modelVersion>
9+
<groupId>io.swagger</groupId>
10+
<artifactId>swagger-java-jersey-multi-simple-servlet-sample-app</artifactId>
11+
<packaging>war</packaging>
12+
<name>swagger-java-jersey-jaxrs-multi-simple-servlet-app</name>
13+
<version>1.0.0</version>
14+
<build>
15+
<sourceDirectory>src/main/java</sourceDirectory>
16+
<plugins>
17+
<plugin>
18+
<groupId>org.apache.maven.plugins</groupId>
19+
<artifactId>maven-war-plugin</artifactId>
20+
<version>2.1.1</version>
21+
</plugin>
22+
<plugin>
23+
<artifactId>maven-failsafe-plugin</artifactId>
24+
<version>2.6</version>
25+
<executions>
26+
<execution>
27+
<goals>
28+
<goal>integration-test</goal>
29+
<goal>verify</goal>
30+
</goals>
31+
</execution>
32+
</executions>
33+
</plugin>
34+
<plugin>
35+
<groupId>org.mortbay.jetty</groupId>
36+
<artifactId>jetty-maven-plugin</artifactId>
37+
<version>${jetty-version}</version>
38+
<configuration>
39+
<webAppConfig>
40+
<contextPath>/</contextPath>
41+
</webAppConfig>
42+
<webAppSourceDirectory>target/${project.artifactId}-${project.version}</webAppSourceDirectory>
43+
<webDefaultXml>${project.basedir}/conf/jetty/webdefault.xml</webDefaultXml>
44+
<stopPort>8079</stopPort>
45+
<stopKey>stopit</stopKey>
46+
<connectors>
47+
<connector implementation="org.eclipse.jetty.server.nio.SelectChannelConnector">
48+
<port>8002</port>
49+
<maxIdleTime>60000</maxIdleTime>
50+
<confidentialPort>8443</confidentialPort>
51+
</connector>
52+
</connectors>
53+
</configuration>
54+
<executions>
55+
<execution>
56+
<id>start-jetty</id>
57+
<phase>pre-integration-test</phase>
58+
<goals>
59+
<goal>run</goal>
60+
</goals>
61+
<configuration>
62+
<scanIntervalSeconds>0</scanIntervalSeconds>
63+
<daemon>true</daemon>
64+
</configuration>
65+
</execution>
66+
<execution>
67+
<id>stop-jetty</id>
68+
<phase>post-integration-test</phase>
69+
<goals>
70+
<goal>stop</goal>
71+
</goals>
72+
</execution>
73+
</executions>
74+
</plugin>
75+
<plugin>
76+
<groupId>com.googlecode.maven-download-plugin</groupId>
77+
<artifactId>download-maven-plugin</artifactId>
78+
<version>1.2.1</version>
79+
<executions>
80+
<execution>
81+
<id>swagger-ui</id>
82+
<goals>
83+
<goal>wget</goal>
84+
</goals>
85+
<configuration>
86+
<url>https://github.com/swagger-api/swagger-ui/archive/master.tar.gz</url>
87+
<unpack>true</unpack>
88+
<outputDirectory>${project.build.directory}</outputDirectory>
89+
</configuration>
90+
</execution>
91+
</executions>
92+
</plugin>
93+
<plugin>
94+
<artifactId>maven-resources-plugin</artifactId>
95+
<version>2.6</version>
96+
<executions>
97+
<execution>
98+
<id>copy-resources</id>
99+
<phase>validate</phase>
100+
<goals>
101+
<goal>copy-resources</goal>
102+
</goals>
103+
<configuration>
104+
<outputDirectory>target/${project.artifactId}-${project.version}</outputDirectory>
105+
<resources>
106+
<resource>
107+
<directory>${project.build.directory}/swagger-ui-master/dist</directory>
108+
<filtering>true</filtering>
109+
<excludes>
110+
<exclude>index.html</exclude>
111+
</excludes>
112+
</resource>
113+
</resources>
114+
</configuration>
115+
</execution>
116+
</executions>
117+
</plugin>
118+
</plugins>
119+
</build>
120+
<dependencies>
121+
<dependency>
122+
<groupId>io.swagger</groupId>
123+
<artifactId>swagger-jersey-jaxrs</artifactId>
124+
<version>${swagger-version}</version>
125+
</dependency>
126+
<dependency>
127+
<groupId>ch.qos.logback</groupId>
128+
<artifactId>logback-classic</artifactId>
129+
<version>${logback-version}</version>
130+
</dependency>
131+
<dependency>
132+
<groupId>ch.qos.logback</groupId>
133+
<artifactId>logback-core</artifactId>
134+
<version>${logback-version}</version>
135+
</dependency>
136+
<dependency>
137+
<groupId>commons-io</groupId>
138+
<artifactId>commons-io</artifactId>
139+
<version>1.3.2</version>
140+
</dependency>
141+
<dependency>
142+
<groupId>javax.servlet</groupId>
143+
<artifactId>servlet-api</artifactId>
144+
</dependency>
145+
<dependency>
146+
<groupId>com.sun.jersey</groupId>
147+
<artifactId>jersey-core</artifactId>
148+
</dependency>
149+
<dependency>
150+
<groupId>com.sun.jersey</groupId>
151+
<artifactId>jersey-json</artifactId>
152+
</dependency>
153+
<dependency>
154+
<groupId>com.sun.jersey</groupId>
155+
<artifactId>jersey-servlet</artifactId>
156+
</dependency>
157+
<dependency>
158+
<groupId>org.scalatest</groupId>
159+
<artifactId>scalatest_2.10</artifactId>
160+
<scope>test</scope>
161+
</dependency>
162+
<dependency>
163+
<groupId>junit</groupId>
164+
<artifactId>junit</artifactId>
165+
<scope>test</scope>
166+
</dependency>
167+
</dependencies>
168+
</project>
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package io.swagger.sample;
2+
3+
import io.swagger.jaxrs.config.BeanConfig;
4+
import io.swagger.jaxrs.config.SwaggerContextService;
5+
import io.swagger.models.*;
6+
7+
import javax.servlet.http.HttpServlet;
8+
import javax.servlet.ServletConfig;
9+
import javax.servlet.ServletException;
10+
11+
public class Bootstrap extends HttpServlet {
12+
@Override
13+
public void init(ServletConfig config) throws ServletException {
14+
Info info = new Info()
15+
.title("Swagger Petstore")
16+
.version("1.0.0");
17+
Swagger swagger = new Swagger().info(info);
18+
swagger.tag(new Tag()
19+
.name("store")
20+
.description("Access to Petstore orders"));
21+
22+
// specify packages to be included by providing a Scanner implementation (BeanConfig) using reflection to scan classes
23+
// included in packages provided in "ResourcePackage". This can be useful e.g. to specify a subset (or different set)
24+
// of packages/classes defined in jersey servlet/jaxrs application.
25+
BeanConfig beanConfig = new BeanConfig();
26+
beanConfig.setResourcePackage("io.swagger.sample.storeresource");
27+
beanConfig.setInfo(info); // needed if using BeanConfig
28+
29+
new SwaggerContextService()
30+
.withContextId("test.1")
31+
.withBasePath("/api1")
32+
.withScanner(beanConfig) // if we need to specify packages
33+
.updateSwagger(swagger)
34+
.initScanner();
35+
36+
37+
Info info2 = new Info()
38+
.title("Swagger Petstore 2")
39+
.version("1.0.0");
40+
Swagger swagger2 = new Swagger().info(info2);
41+
swagger2.tag(new Tag()
42+
.name("pet")
43+
.description("Everything about your Pets"));
44+
45+
// specify packages to be included by providing a Scanner implementation (BeanConfig) using reflection to scan classes
46+
// included in packages provided in "ResourcePackage". This can be useful e.g. to specify a subset (or different set)
47+
// of packages/classes defined in jersey servlet/jaxrs application.
48+
BeanConfig beanConfig2 = new BeanConfig();
49+
beanConfig2.setResourcePackage("io.swagger.sample.userresource");
50+
beanConfig2.setInfo(info2); // needed if using BeanConfig
51+
52+
new SwaggerContextService()
53+
.withContextId("test.2")
54+
.withBasePath("/api2")
55+
.withScanner(beanConfig2) // if we need to specify packages
56+
.updateSwagger(swagger2)
57+
.initScanner();
58+
}
59+
}

0 commit comments

Comments
 (0)