Last active
November 8, 2016 21:06
-
-
Save knutwalker/7913344 to your computer and use it in GitHub Desktop.
Jersey2 + Swagger with Resource inheritance
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
package com.example; | |
import com.wordnik.swagger.annotations.ApiParam; | |
import javax.ws.rs.GET; | |
import javax.ws.rs.Path; | |
import javax.ws.rs.PathParam; | |
import javax.ws.rs.Produces; | |
import javax.ws.rs.core.MediaType; | |
import javax.ws.rs.core.Response; | |
abstract public class AbstractResource<T> { | |
protected abstract String name(); | |
@GET | |
@Path("/{id}") | |
@Produces(MediaType.TEXT_PLAIN) | |
public Response getObject(@ApiParam(value = "identifier", required = true) @PathParam("id") final T id) { | |
return Response.ok(name() + " : " + id).build(); | |
} | |
} |
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
GET /myapp/api-docs/resource/ HTTP/1.1 | |
{ | |
"apiVersion": "1.0.0", | |
"apis": [ | |
{ | |
"operations": [ | |
{ | |
"method": "GET", | |
"nickname": "getObject", | |
"notes": "", | |
"parameters": [ | |
{ | |
"name": "id", | |
"paramType": "path", | |
"required": true, | |
"type": "string" | |
} | |
], | |
"produces": [ | |
"text/plain" | |
], | |
"summary": "get an object matching the ID", | |
"type": "void" | |
} | |
], | |
"path": "/resource/{id}" | |
} | |
], | |
"basePath": "http://localhost:8080/myapp", | |
"resourcePath": "/resource", | |
"swaggerVersion": "1.2" | |
} |
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
GET /myapp/api-docs/resource/ HTTP/1.1 | |
{ | |
"apiVersion": "1.0.0", | |
"apis": [ | |
{ | |
"operations": [ | |
{ | |
"method": null, | |
"nickname": "getObject", | |
"notes": "", | |
"parameters": [ | |
{ | |
"name": "body", | |
"paramType": "body", | |
"required": false, | |
"type": "string" | |
} | |
], | |
"summary": "get an object matching the ID", | |
"type": "void" | |
} | |
], | |
"path": "/resource" | |
} | |
], | |
"basePath": "http://localhost:8080/myapp", | |
"resourcePath": "/resource", | |
"swaggerVersion": "1.2" | |
} |
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
package com.example; | |
import com.wordnik.swagger.jaxrs.listing.ApiListingResourceJSON; | |
import com.wordnik.swagger.jersey.listing.JerseyApiDeclarationProvider; | |
import com.wordnik.swagger.jersey.listing.JerseyResourceListingProvider; | |
import org.glassfish.jersey.server.ResourceConfig; | |
public class Application extends ResourceConfig { | |
public Application() { | |
packages("com.example"); | |
register(ApiListingResourceJSON.class); | |
register(JerseyApiDeclarationProvider.class); | |
register(JerseyResourceListingProvider.class); | |
} | |
} |
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
package com.example; | |
import com.wordnik.swagger.annotations.Api; | |
import com.wordnik.swagger.annotations.ApiOperation; | |
import javax.ws.rs.Path; | |
import javax.ws.rs.core.Response; | |
@Api(value = "/resource", description = "concrete operations") | |
@Path("resource") | |
public class ConcreteResource extends AbstractResource<String> { | |
@Override | |
protected String name() { | |
return "concrete"; | |
} | |
@ApiOperation(value = "get an object matching the ID") | |
@Override | |
public Response getObject(final String id) { | |
return super.getObject(id); | |
} | |
} |
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
package com.example; | |
import javax.servlet.*; | |
import javax.servlet.http.HttpServletResponse; | |
import java.io.IOException; | |
public class CorsFilter implements Filter { | |
@Override | |
public void init(FilterConfig filterConfig) throws ServletException {} | |
@Override | |
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { | |
final HttpServletResponse res = (HttpServletResponse) response; | |
res.addHeader("Access-Control-Allow-Origin", "*"); | |
res.addHeader("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT"); | |
res.addHeader("Access-Control-Allow-Headers", "Content-Type"); | |
chain.doFilter(request, response); | |
} | |
@Override | |
public void destroy() {} | |
} |
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
<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"> | |
<modelVersion>4.0.0</modelVersion> | |
<groupId>com.example</groupId> | |
<artifactId>swagger-abstract-annotations</artifactId> | |
<packaging>jar</packaging> | |
<version>1.0-SNAPSHOT</version> | |
<name>swagger-abstract-annotations</name> | |
<dependencyManagement> | |
<dependencies> | |
<dependency> | |
<groupId>org.glassfish.jersey</groupId> | |
<artifactId>jersey-bom</artifactId> | |
<version>${jersey.version}</version> | |
<type>pom</type> | |
<scope>import</scope> | |
</dependency> | |
</dependencies> | |
</dependencyManagement> | |
<dependencies> | |
<dependency> | |
<groupId>org.glassfish.jersey.containers</groupId> | |
<artifactId>jersey-container-servlet</artifactId> | |
</dependency> | |
<dependency> | |
<groupId>com.wordnik</groupId> | |
<artifactId>swagger-core_2.10</artifactId> | |
<version>1.3.1</version> | |
</dependency> | |
<dependency> | |
<groupId>com.wordnik</groupId> | |
<artifactId>swagger-jersey2-jaxrs_2.10</artifactId> | |
<version>1.3.1</version> | |
<exclusions> | |
<exclusion> | |
<groupId>javax.ws.rs</groupId> | |
<artifactId>jsr311-api</artifactId> | |
</exclusion> | |
</exclusions> | |
</dependency> | |
<dependency> | |
<groupId>javax.servlet</groupId> | |
<artifactId>javax.servlet-api</artifactId> | |
<version>3.1.0</version> | |
</dependency> | |
</dependencies> | |
<build> | |
<plugins> | |
<plugin> | |
<groupId>org.apache.maven.plugins</groupId> | |
<artifactId>maven-compiler-plugin</artifactId> | |
<version>2.5.1</version> | |
<inherited>true</inherited> | |
<configuration> | |
<source>1.7</source> | |
<target>1.7</target> | |
</configuration> | |
</plugin> | |
<plugin> | |
<groupId>org.apache.maven.plugins</groupId> | |
<artifactId>maven-war-plugin</artifactId> | |
<version>2.4</version> | |
<executions> | |
<execution> | |
<phase>package</phase> | |
<goals><goal>war</goal></goals> | |
</execution> | |
</executions> | |
</plugin> | |
<plugin> | |
<groupId>org.eclipse.jetty</groupId> | |
<artifactId>jetty-maven-plugin</artifactId> | |
<version>9.0.6.v20130930</version> | |
<executions> | |
<execution> | |
<phase>package</phase> | |
<goals><goal>run-war</goal></goals> | |
</execution> | |
</executions> | |
<configuration> | |
<webAppConfig> | |
<contextPath>/myapp</contextPath> | |
</webAppConfig> | |
</configuration> | |
</plugin> | |
</plugins> | |
</build> | |
<properties> | |
<jersey.version>2.4.1</jersey.version> | |
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | |
</properties> | |
</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
<?xml version="1.0" encoding="UTF-8"?> | |
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> | |
<servlet> | |
<servlet-name>Jersey</servlet-name> | |
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class> | |
<init-param> | |
<param-name>javax.ws.rs.Application</param-name> | |
<param-value>com.example.Application</param-value> | |
</init-param> | |
<load-on-startup>1</load-on-startup> | |
</servlet> | |
<servlet-mapping> | |
<servlet-name>Jersey</servlet-name> | |
<url-pattern>/*</url-pattern> | |
</servlet-mapping> | |
<servlet> | |
<servlet-name>DefaultJaxrsConfig</servlet-name> | |
<servlet-class>com.wordnik.swagger.jaxrs.config.DefaultJaxrsConfig</servlet-class> | |
<init-param> | |
<param-name>api.version</param-name> | |
<param-value>1.0.0</param-value> | |
</init-param> | |
<init-param> | |
<param-name>swagger.api.basepath</param-name> | |
<param-value>http://localhost:8080/myapp</param-value> | |
</init-param> | |
<load-on-startup>2</load-on-startup> | |
</servlet> | |
<filter> | |
<filter-name>CorsFilter</filter-name> | |
<filter-class>com.example.CorsFilter</filter-class> | |
</filter> | |
<filter-mapping> | |
<filter-name>CorsFilter</filter-name> | |
<url-pattern>/*</url-pattern> | |
</filter-mapping> | |
</web-app> |
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
package com.example; | |
import com.wordnik.swagger.annotations.Api; | |
import com.wordnik.swagger.annotations.ApiOperation; | |
import javax.ws.rs.GET; | |
import javax.ws.rs.Path; | |
import javax.ws.rs.PathParam; | |
import javax.ws.rs.Produces; | |
import javax.ws.rs.core.MediaType; | |
import javax.ws.rs.core.Response; | |
@Api(value = "/resource", description = "concrete operations") | |
@Path("resource") | |
public class WorkingConcreteResource extends AbstractResource<String> { | |
@Override | |
protected String name() { | |
return "concrete"; | |
} | |
@ApiOperation(value = "get an object matching the ID") | |
@GET | |
@Path("/{id}") | |
@Produces(MediaType.TEXT_PLAIN) | |
@Override | |
public Response getObject(@PathParam("id") final String id) { | |
return super.getObject(id); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment