Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit e2a353e
Showing
32 changed files
with
11,183 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Using Swagger for REST API documentations | ||
============== | ||
|
||
- localhost:8080/swagger/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
<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/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<groupId>com.example</groupId> | ||
<artifactId>jax-rs-2.0-swagger</artifactId> | ||
<version>0.0.1-SNAPSHOT</version> | ||
<packaging>jar</packaging> | ||
|
||
<properties> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
<org.apache.cxf.version>2.7.7</org.apache.cxf.version> | ||
<org.springframework.version>3.2.4.RELEASE</org.springframework.version> | ||
<org.eclipse.jetty.version>9.0.5.v20130815</org.eclipse.jetty.version> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>ch.qos.logback</groupId> | ||
<artifactId>logback-classic</artifactId> | ||
<version>1.0.13</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>com.wordnik</groupId> | ||
<artifactId>swagger-jaxrs_2.10</artifactId> | ||
<version>1.3.0</version> | ||
<exclusions> | ||
<exclusion> | ||
<groupId>javax.ws.rs</groupId> | ||
<artifactId>jsr311-api</artifactId> | ||
</exclusion> | ||
</exclusions> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.apache.cxf</groupId> | ||
<artifactId>cxf-rt-frontend-jaxrs</artifactId> | ||
<version>${org.apache.cxf.version}</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>javax.inject</groupId> | ||
<artifactId>javax.inject</artifactId> | ||
<version>1</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>com.fasterxml.jackson.jaxrs</groupId> | ||
<artifactId>jackson-jaxrs-json-provider</artifactId> | ||
<version>2.2.2</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>com.fasterxml.jackson.core</groupId> | ||
<artifactId>jackson-core</artifactId> | ||
<version>2.2.2</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.springframework</groupId> | ||
<artifactId>spring-core</artifactId> | ||
<version>${org.springframework.version}</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.springframework</groupId> | ||
<artifactId>spring-context</artifactId> | ||
<version>${org.springframework.version}</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.springframework</groupId> | ||
<artifactId>spring-web</artifactId> | ||
<version>${org.springframework.version}</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.eclipse.jetty</groupId> | ||
<artifactId>jetty-server</artifactId> | ||
<version>${org.eclipse.jetty.version}</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.eclipse.jetty</groupId> | ||
<artifactId>jetty-webapp</artifactId> | ||
<version>${org.eclipse.jetty.version}</version> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
<version>3.0</version> | ||
<configuration> | ||
<source>1.7</source> | ||
<target>1.7</target> | ||
</configuration> | ||
</plugin> | ||
|
||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-jar-plugin</artifactId> | ||
<version>2.4</version> | ||
<configuration> | ||
<archive> | ||
<manifest> | ||
<mainClass>com.example.Starter</mainClass> | ||
</manifest> | ||
</archive> | ||
</configuration> | ||
</plugin> | ||
|
||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-shade-plugin</artifactId> | ||
<version>1.6</version> | ||
<configuration> | ||
<filters> | ||
<filter> | ||
<artifact>*:*</artifact> | ||
<excludes> | ||
<exclude>META-INF/*.SF</exclude> | ||
<exclude>META-INF/*.DSA</exclude> | ||
<exclude>META-INF/*.RSA</exclude> | ||
</excludes> | ||
</filter> | ||
</filters> | ||
</configuration> | ||
<executions> | ||
<execution> | ||
<phase>package</phase> | ||
<goals> | ||
<goal>shade</goal> | ||
</goals> | ||
<configuration> | ||
<transformers> | ||
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> | ||
<mainClass>com.example.Starter</mainClass> | ||
</transformer> | ||
<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer"> | ||
<resource>META-INF/cxf/bus-extensions.txt</resource> | ||
</transformer> | ||
</transformers> | ||
</configuration> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
<repositories> | ||
<repository> | ||
<id>maven2-repository.dev.java.net</id> | ||
<name>Java.net Repository for Maven</name> | ||
<url>http://download.java.net/maven/2/</url> | ||
<layout>default</layout> | ||
</repository> | ||
</repositories> | ||
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package com.example; | ||
|
||
import org.apache.cxf.transport.servlet.CXFServlet; | ||
import org.eclipse.jetty.server.Server; | ||
import org.eclipse.jetty.server.handler.HandlerList; | ||
import org.eclipse.jetty.servlet.DefaultServlet; | ||
import org.eclipse.jetty.servlet.ServletContextHandler; | ||
import org.eclipse.jetty.servlet.ServletHolder; | ||
import org.eclipse.jetty.util.resource.Resource; | ||
import org.springframework.core.io.ClassPathResource; | ||
import org.springframework.web.context.ContextLoaderListener; | ||
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; | ||
|
||
import com.example.config.AppConfig; | ||
|
||
public class Starter { | ||
private static final int SERVER_PORT = 8080; | ||
private static final String CONTEXT_PATH = "rest"; | ||
|
||
public static void main( final String[] args ) throws Exception { | ||
Resource.setDefaultUseCaches( false ); | ||
|
||
final Server server = new Server( SERVER_PORT ); | ||
System.setProperty( AppConfig.SERVER_PORT, Integer.toString( SERVER_PORT ) ); | ||
System.setProperty( AppConfig.SERVER_HOST, "localhost" ); | ||
System.setProperty( AppConfig.CONTEXT_PATH, CONTEXT_PATH ); | ||
|
||
// Configuring Apache CXF servlet and Spring listener | ||
final ServletHolder servletHolder = new ServletHolder( new CXFServlet() ); | ||
final ServletContextHandler context = new ServletContextHandler(); | ||
context.setContextPath( "/" ); | ||
context.addServlet( servletHolder, "/" + CONTEXT_PATH + "/*" ); | ||
context.addEventListener( new ContextLoaderListener() ); | ||
context.setInitParameter( "contextClass", AnnotationConfigWebApplicationContext.class.getName() ); | ||
context.setInitParameter( "contextConfigLocation", AppConfig.class.getName() ); | ||
|
||
// Configuring Swagger as static web resource | ||
final ServletHolder swaggerHolder = new ServletHolder( new DefaultServlet() ); | ||
final ServletContextHandler swagger = new ServletContextHandler(); | ||
swagger.setContextPath( "/swagger" ); | ||
swagger.addServlet( swaggerHolder, "/*" ); | ||
swagger.setResourceBase( new ClassPathResource( "/webapp" ).getURI().toString() ); | ||
|
||
final HandlerList handlers = new HandlerList(); | ||
handlers.addHandler( context ); | ||
handlers.addHandler( swagger ); | ||
|
||
server.setHandler( handlers ); | ||
server.start(); | ||
server.join(); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package com.example.config; | ||
|
||
import java.util.Arrays; | ||
|
||
import javax.ws.rs.ext.RuntimeDelegate; | ||
|
||
import org.apache.cxf.bus.spring.SpringBus; | ||
import org.apache.cxf.endpoint.Server; | ||
import org.apache.cxf.jaxrs.JAXRSServerFactoryBean; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.context.annotation.DependsOn; | ||
import org.springframework.core.env.Environment; | ||
|
||
import com.example.resource.Person; | ||
import com.example.rs.JaxRsApiApplication; | ||
import com.example.rs.PeopleRestService; | ||
import com.example.services.PeopleService; | ||
import com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider; | ||
import com.wordnik.swagger.jaxrs.config.BeanConfig; | ||
import com.wordnik.swagger.jaxrs.listing.ApiDeclarationProvider; | ||
import com.wordnik.swagger.jaxrs.listing.ApiListingResourceJSON; | ||
import com.wordnik.swagger.jaxrs.listing.ResourceListingProvider; | ||
|
||
@Configuration | ||
public class AppConfig { | ||
public static final String SERVER_PORT = "server.port"; | ||
public static final String SERVER_HOST = "server.host"; | ||
public static final String CONTEXT_PATH = "context.path"; | ||
|
||
@Bean( destroyMethod = "shutdown" ) | ||
public SpringBus cxf() { | ||
return new SpringBus(); | ||
} | ||
|
||
@Bean @DependsOn( "cxf" ) | ||
public Server jaxRsServer() { | ||
JAXRSServerFactoryBean factory = RuntimeDelegate.getInstance().createEndpoint( jaxRsApiApplication(), JAXRSServerFactoryBean.class ); | ||
factory.setServiceBeans( Arrays.< Object >asList( peopleRestService(), apiListingResourceJson() ) ); | ||
factory.setAddress( factory.getAddress() ); | ||
factory.setProviders( Arrays.< Object >asList( jsonProvider(), resourceListingProvider(), apiDeclarationProvider() ) ); | ||
return factory.create(); | ||
} | ||
|
||
@Bean @Autowired | ||
public BeanConfig swaggerConfig( Environment environment ) { | ||
final BeanConfig config = new BeanConfig(); | ||
|
||
config.setVersion( "1.0.0" ); | ||
config.setScan( true ); | ||
config.setResourcePackage( Person.class.getPackage().getName() ); | ||
config.setBasePath( | ||
String.format( "http://%s:%s/%s%s", | ||
environment.getProperty( SERVER_HOST ), | ||
environment.getProperty( SERVER_PORT ), | ||
environment.getProperty( CONTEXT_PATH ), | ||
jaxRsServer().getEndpoint().getEndpointInfo().getAddress() | ||
) | ||
); | ||
|
||
return config; | ||
} | ||
|
||
@Bean | ||
public ApiDeclarationProvider apiDeclarationProvider() { | ||
return new ApiDeclarationProvider(); | ||
} | ||
|
||
@Bean | ||
public ApiListingResourceJSON apiListingResourceJson() { | ||
return new ApiListingResourceJSON(); | ||
} | ||
|
||
@Bean | ||
public ResourceListingProvider resourceListingProvider() { | ||
return new ResourceListingProvider(); | ||
} | ||
|
||
@Bean | ||
public JaxRsApiApplication jaxRsApiApplication() { | ||
return new JaxRsApiApplication(); | ||
} | ||
|
||
@Bean | ||
public PeopleRestService peopleRestService() { | ||
return new PeopleRestService(); | ||
} | ||
|
||
@Bean | ||
public PeopleService peopleService() { | ||
return new PeopleService(); | ||
} | ||
|
||
@Bean | ||
public JacksonJsonProvider jsonProvider() { | ||
return new JacksonJsonProvider(); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
src/main/java/com/example/exceptions/PersonAlreadyExistsException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.example.exceptions; | ||
|
||
import javax.ws.rs.WebApplicationException; | ||
import javax.ws.rs.core.Response; | ||
import javax.ws.rs.core.Response.Status; | ||
|
||
public class PersonAlreadyExistsException extends WebApplicationException { | ||
private static final long serialVersionUID = 6817489620338221395L; | ||
|
||
public PersonAlreadyExistsException( final String email ) { | ||
super( | ||
Response | ||
.status( Status.CONFLICT ) | ||
.build() | ||
); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
src/main/java/com/example/exceptions/PersonNotFoundException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.example.exceptions; | ||
|
||
import javax.ws.rs.WebApplicationException; | ||
import javax.ws.rs.core.Response; | ||
import javax.ws.rs.core.Response.Status; | ||
|
||
public class PersonNotFoundException extends WebApplicationException { | ||
private static final long serialVersionUID = -2894269137259898072L; | ||
|
||
public PersonNotFoundException( final String email ) { | ||
super( | ||
Response | ||
.status( Status.NOT_FOUND ) | ||
.build() | ||
); | ||
} | ||
} |
Oops, something went wrong.