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

Commit 5f336a8

Browse files
committed
refs swagger-api/swagger-core#2515 - add jersey sample with resource registration
1 parent ed47d4a commit 5f336a8

29 files changed

+1938
-0
lines changed
+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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 jersey2, and provides an example of integration of swagger into a jersey2 based app, with code based configuration/initialization
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+
Once started, you can navigate to http://localhost:8002/sample/openapi.json to view the Swagger Resource Listing.
20+
This tells you that the server is up and ready to demonstrate Swagger.
21+
22+
### Using the UI
23+
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)
24+
25+
### Applying an API key
26+
The sample app has an implementation of the Swagger ApiAuthorizationFilter. This restricts access to resources
27+
based on api-key. There are two keys defined in the sample app:
28+
29+
`default-key`
30+
31+
`special-key`
32+
33+
When no key is applied, the "default-key" is applied to all operations. If the "special-key" is entered, a
34+
number of other resources are shown in the UI, including sample CRUD operations.
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
+190
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
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-jersey2-resourceinit-sample-app</artifactId>
11+
<packaging>war</packaging>
12+
<name>swagger-jersey2-resourceinit-sample-app</name>
13+
<version>2.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+
<configuration>
22+
<failOnMissingWebXml>false</failOnMissingWebXml>
23+
</configuration>
24+
</plugin>
25+
<plugin>
26+
<artifactId>maven-failsafe-plugin</artifactId>
27+
<version>2.6</version>
28+
<executions>
29+
<execution>
30+
<goals>
31+
<goal>integration-test</goal>
32+
<goal>verify</goal>
33+
</goals>
34+
</execution>
35+
</executions>
36+
</plugin>
37+
<plugin>
38+
<groupId>org.eclipse.jetty</groupId>
39+
<artifactId>jetty-maven-plugin</artifactId>
40+
<version>${jetty-version}</version>
41+
<configuration>
42+
<webApp>
43+
<contextPath>/</contextPath>
44+
</webApp>
45+
<webAppSourceDirectory>target/${project.artifactId}-${project.version}</webAppSourceDirectory>
46+
<stopPort>8079</stopPort>
47+
<stopKey>stopit</stopKey>
48+
<httpConnector>
49+
<port>8002</port>
50+
<idleTimeout>60000</idleTimeout>
51+
</httpConnector>
52+
<systemProperties>
53+
<systemProperty>
54+
<name>org.eclipse.jetty.util.log.Log</name>
55+
<value>org.eclipse.jetty.util.log.Slf4jLog</value>
56+
</systemProperty>
57+
<systemProperty>
58+
<name>logback.configurationFile</name>
59+
<value>src/main/resources/logback.xml</value>
60+
</systemProperty>
61+
</systemProperties>
62+
</configuration>
63+
<executions>
64+
<execution>
65+
<id>start-jetty</id>
66+
<phase>pre-integration-test</phase>
67+
<goals>
68+
<goal>stop</goal>
69+
<goal>start</goal>
70+
</goals>
71+
<configuration>
72+
<scanIntervalSeconds>0</scanIntervalSeconds>
73+
</configuration>
74+
</execution>
75+
<execution>
76+
<id>stop-jetty</id>
77+
<phase>post-integration-test</phase>
78+
<goals>
79+
<goal>stop</goal>
80+
</goals>
81+
</execution>
82+
</executions>
83+
</plugin>
84+
<plugin>
85+
<groupId>com.googlecode.maven-download-plugin</groupId>
86+
<artifactId>download-maven-plugin</artifactId>
87+
<version>1.2.1</version>
88+
<executions>
89+
<execution>
90+
<id>swagger-ui</id>
91+
<goals>
92+
<goal>wget</goal>
93+
</goals>
94+
<configuration>
95+
<skipCache>true</skipCache>
96+
<url>https://github.com/swagger-api/swagger-ui/archive/master.tar.gz</url>
97+
<unpack>true</unpack>
98+
<outputDirectory>${project.build.directory}</outputDirectory>
99+
</configuration>
100+
</execution>
101+
</executions>
102+
</plugin>
103+
<plugin>
104+
<artifactId>maven-resources-plugin</artifactId>
105+
<version>2.6</version>
106+
<executions>
107+
<execution>
108+
<id>copy-resources</id>
109+
<phase>validate</phase>
110+
<goals>
111+
<goal>copy-resources</goal>
112+
</goals>
113+
<configuration>
114+
<outputDirectory>target/${project.artifactId}-${project.version}</outputDirectory>
115+
<resources>
116+
<resource>
117+
<directory>${project.build.directory}/swagger-ui-master/dist</directory>
118+
<filtering>true</filtering>
119+
<excludes>
120+
<exclude>index.html</exclude>
121+
</excludes>
122+
</resource>
123+
</resources>
124+
</configuration>
125+
</execution>
126+
</executions>
127+
</plugin>
128+
<plugin>
129+
<groupId>org.apache.maven.plugins</groupId>
130+
<artifactId>maven-compiler-plugin</artifactId>
131+
<configuration>
132+
<source>1.8</source>
133+
<target>1.8</target>
134+
</configuration>
135+
</plugin>
136+
</plugins>
137+
</build>
138+
<dependencies>
139+
<dependency>
140+
<groupId>io.swagger.core.v3</groupId>
141+
<artifactId>swagger-jaxrs2</artifactId>
142+
<scope>compile</scope>
143+
<version>${swagger-version}</version>
144+
</dependency>
145+
<dependency>
146+
<groupId>com.fasterxml.jackson.jaxrs</groupId>
147+
<artifactId>jackson-jaxrs-json-provider</artifactId>
148+
<version>${jackson-version}</version>
149+
</dependency>
150+
151+
<dependency>
152+
<groupId>ch.qos.logback</groupId>
153+
<artifactId>logback-classic</artifactId>
154+
<version>${logback-version}</version>
155+
<scope>compile</scope>
156+
</dependency>
157+
<dependency>
158+
<groupId>ch.qos.logback</groupId>
159+
<artifactId>logback-core</artifactId>
160+
<version>${logback-version}</version>
161+
<scope>compile</scope>
162+
</dependency>
163+
164+
<dependency>
165+
<groupId>junit</groupId>
166+
<artifactId>junit</artifactId>
167+
<scope>test</scope>
168+
</dependency>
169+
<dependency>
170+
<groupId>javax.servlet</groupId>
171+
<artifactId>javax.servlet-api</artifactId>
172+
<scope>provided</scope>
173+
</dependency>
174+
<dependency>
175+
<groupId>org.glassfish.jersey.containers</groupId>
176+
<artifactId>jersey-container-servlet</artifactId>
177+
<version>${jersey2-version}</version>
178+
</dependency>
179+
<dependency>
180+
<groupId>org.glassfish.jersey.media</groupId>
181+
<artifactId>jersey-media-multipart</artifactId>
182+
<version>${jersey2-version}</version>
183+
</dependency>
184+
<dependency>
185+
<groupId>org.glassfish.jersey.inject</groupId>
186+
<artifactId>jersey-hk2</artifactId>
187+
<version>${jersey2-version}</version>
188+
</dependency>
189+
</dependencies>
190+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package io.swagger.sample;
2+
3+
import io.swagger.v3.jaxrs2.integration.resources.OpenApiResource;
4+
import io.swagger.v3.oas.integration.SwaggerConfiguration;
5+
import io.swagger.v3.oas.models.OpenAPI;
6+
import io.swagger.v3.oas.models.info.Contact;
7+
import io.swagger.v3.oas.models.info.Info;
8+
import io.swagger.v3.oas.models.info.License;
9+
import io.swagger.v3.oas.models.security.SecurityScheme;
10+
import org.glassfish.jersey.server.ResourceConfig;
11+
12+
import javax.servlet.ServletConfig;
13+
import javax.ws.rs.ApplicationPath;
14+
import javax.ws.rs.core.Context;
15+
import java.util.stream.Collectors;
16+
import java.util.stream.Stream;
17+
18+
@ApplicationPath("/sample")
19+
public class MyApplication extends ResourceConfig {
20+
21+
public MyApplication(@Context ServletConfig servletConfig) {
22+
super();
23+
OpenAPI oas = new OpenAPI();
24+
Info info = new Info()
25+
.title("Swagger Sample App bootstrap code")
26+
.description("This is a sample server Petstore server. You can find out more about Swagger " +
27+
"at [http://swagger.io](http://swagger.io) or on [irc.freenode.net, #swagger](http://swagger.io/irc/). For this sample, " +
28+
"you can use the api key `special-key` to test the authorization filters.")
29+
.termsOfService("http://swagger.io/terms/")
30+
.contact(new Contact()
31+
.email("[email protected]"))
32+
.license(new License()
33+
.name("Apache 2.0")
34+
.url("http://www.apache.org/licenses/LICENSE-2.0.html"));
35+
36+
oas.info(info);
37+
38+
// SecurityScheme api-key
39+
SecurityScheme securitySchemeApiKey = new SecurityScheme();
40+
securitySchemeApiKey.setName("api-key");
41+
securitySchemeApiKey.setType(SecurityScheme.Type.APIKEY);
42+
securitySchemeApiKey.setIn(SecurityScheme.In.HEADER);
43+
44+
OpenApiResource openApiResource = new OpenApiResource();
45+
46+
oas.schemaRequirement(securitySchemeApiKey.getName(), securitySchemeApiKey);
47+
48+
SwaggerConfiguration oasConfig = new SwaggerConfiguration()
49+
.openAPI(oas)
50+
.prettyPrint(true)
51+
.resourcePackages(Stream.of("io.swagger.sample.resource").collect(Collectors.toSet()));
52+
53+
openApiResource.setOpenApiConfiguration(oasConfig);
54+
register(openApiResource);
55+
}
56+
}

0 commit comments

Comments
 (0)