Skip to content

Commit 81b3542

Browse files
committed
added sample for configuration without web.xml
1 parent c89b9c2 commit 81b3542

File tree

26 files changed

+1826
-0
lines changed

26 files changed

+1826
-0
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Swagger Sample App
2+
3+
## Overview
4+
This is a java project to build a stand-alone server which implements the Swagger spec. You can find out
5+
more about both the spec and the framework at http://swagger.io. For more information
6+
about Wordnik's APIs, please visit http://developer.wordnik.com. There is an online version of this
7+
server at http://petstore.swagger.io/api/api-docs
8+
9+
### To build from source
10+
Please follow instructions to build the top-level [swagger-core project](https://github.com/wordnik/swagger-core)
11+
12+
### To run (with Maven)
13+
To run the server, run this task:
14+
<pre>
15+
mvn package -Dlog4j.configuration=file:./conf/log4j.properties jetty:run
16+
</pre>
17+
18+
This will start Jetty embedded on port 8002.
19+
20+
### Testing the server
21+
Once started, you can navigate to http://localhost:8002/api/api-docs to view the Swagger Resource Listing.
22+
This tells you that the server is up and ready to demonstrate Swagger.
23+
24+
### Using the UI
25+
There is an HTML5-based API tool available in a separate project. This lets you inspect the API using an
26+
intuitive UI. You can pull this code from here: https://github.com/wordnik/swagger-ui
27+
28+
You can then open the dist/index.html file in any HTML5-enabled browser. Upen opening, enter the
29+
URL of your server in the top-centered input box (default is http://localhost:8002/api/api-docs). Click the "Explore"
30+
button and you should see the resources available on the server.
31+
32+
### Applying an API key
33+
The sample app has an implementation of the Swagger ApiAuthorizationFilter. This restricts access to resources
34+
based on api-key. There are two keys defined in the sample app:
35+
36+
<li>- default-key</li>
37+
38+
<li>- special-key</li>
39+
40+
When no key is applied, the "default-key" is applied to all operations. If the "special-key" is entered, a
41+
number of other resources are shown in the UI, including sample CRUD operations. Note this behavior is similar
42+
to that on http://developer.wordnik.com/docs but the behavior is entirely up to the implementor.
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.com.wordnik=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/wordnik.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

samples/java-jaxrs-no-webxml/logs/wordnik.log

Whitespace-only changes.
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
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>com.wordnik</groupId>
4+
<artifactId>swagger-project</artifactId>
5+
<version>1.5.3-M1-SNAPSHOT</version>
6+
<relativePath>../..</relativePath>
7+
</parent>
8+
<modelVersion>4.0.0</modelVersion>
9+
<groupId>com.wordnik</groupId>
10+
<artifactId>swagger-java-sample-app</artifactId>
11+
<packaging>war</packaging>
12+
<name>swagger-java-jaxrs-app</name>
13+
<version>1.5.3-M1-SNAPSHOT</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/v${swagger-ui-version}.tar.gz</url>
87+
<unpack>true</unpack>
88+
<outputDirectory>${project.build.directory}</outputDirectory>
89+
</configuration>
90+
</execution>
91+
</executions>
92+
</plugin>
93+
</plugins>
94+
</build>
95+
<dependencies>
96+
<dependency>
97+
<groupId>com.wordnik</groupId>
98+
<artifactId>swagger-jaxrs</artifactId>
99+
<version>${project.parent.version}</version>
100+
<scope>compile</scope>
101+
</dependency>
102+
<dependency>
103+
<groupId>ch.qos.logback</groupId>
104+
<artifactId>logback-classic</artifactId>
105+
<version>${logback-version}</version>
106+
<scope>compile</scope>
107+
</dependency>
108+
<dependency>
109+
<groupId>ch.qos.logback</groupId>
110+
<artifactId>logback-core</artifactId>
111+
<version>${logback-version}</version>
112+
<scope>compile</scope>
113+
</dependency>
114+
<dependency>
115+
<groupId>org.scalatest</groupId>
116+
<artifactId>scalatest_2.10</artifactId>
117+
<scope>test</scope>
118+
</dependency>
119+
<dependency>
120+
<groupId>junit</groupId>
121+
<artifactId>junit</artifactId>
122+
<scope>test</scope>
123+
</dependency>
124+
<dependency>
125+
<groupId>javax.servlet</groupId>
126+
<artifactId>servlet-api</artifactId>
127+
</dependency>
128+
<dependency>
129+
<groupId>com.sun.jersey</groupId>
130+
<artifactId>jersey-core</artifactId>
131+
</dependency>
132+
<dependency>
133+
<groupId>com.sun.jersey</groupId>
134+
<artifactId>jersey-json</artifactId>
135+
</dependency>
136+
<dependency>
137+
<groupId>com.sun.jersey</groupId>
138+
<artifactId>jersey-servlet</artifactId>
139+
</dependency>
140+
</dependencies>
141+
</project>
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.wordnik.swagger.sample;
2+
3+
import com.wordnik.swagger.config.ScannerFactory;
4+
import com.wordnik.swagger.models.*;
5+
import com.wordnik.swagger.jaxrs.config.ReflectiveJaxrsScanner;
6+
7+
import com.wordnik.swagger.models.auth.*;
8+
9+
import javax.servlet.http.HttpServlet;
10+
import javax.servlet.ServletContext;
11+
import javax.servlet.ServletConfig;
12+
import javax.servlet.ServletException;
13+
14+
public class Bootstrap extends HttpServlet {
15+
@Override
16+
public void init(ServletConfig config) throws ServletException {
17+
ReflectiveJaxrsScanner scanner = new ReflectiveJaxrsScanner();
18+
scanner.setResourcePackage("com.wordnik.swagger.sample.resource");
19+
ScannerFactory.setScanner(scanner);
20+
Info info = new Info()
21+
.title("Swagger Sample App")
22+
.description("This is a sample server Petstore server. You can find out more about Swagger " +
23+
"at <a href=\"http://swagger.io\">http://swagger.io</a> or on irc.freenode.net, #swagger. For this sample, " +
24+
"you can use the api key \"special-key\" to test the authorization filters")
25+
.termsOfService("http://helloreverb.com/terms/")
26+
.contact(new Contact()
27+
.email("apiteam@wordnik.com"))
28+
.license(new License()
29+
.name("Apache 2.0")
30+
.url("http://www.apache.org/licenses/LICENSE-2.0.html"));
31+
32+
ServletContext context = config.getServletContext();
33+
Swagger swagger = new Swagger().info(info);
34+
swagger.securityDefinition("api_key", new ApiKeyAuthDefinition("api_key", In.HEADER));
35+
swagger.securityDefinition("petstore_auth",
36+
new OAuth2Definition()
37+
.implicit("http://petstore.swagger.io/api/oauth/dialog")
38+
.scope("read:pets", "read your pets")
39+
.scope("write:pets", "modify pets in your account"));
40+
swagger.tag(new Tag()
41+
.name("pet")
42+
.description("Everything about your Pets")
43+
.externalDocs(new ExternalDocs("Find out more", "http://swagger.io")));
44+
swagger.tag(new Tag()
45+
.name("store")
46+
.description("Operations about user"));
47+
swagger.tag(new Tag()
48+
.name("user")
49+
.description("Access to Petstore orders")
50+
.externalDocs(new ExternalDocs("Find out more about our store", "http://swagger.io")));
51+
52+
context.setAttribute("swagger", swagger);
53+
}
54+
}

0 commit comments

Comments
 (0)