Created
March 25, 2014 18:06
-
-
Save malalanayake/9767621 to your computer and use it in GitHub Desktop.
REST Api Documentation with Swagger
This file contains hidden or 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
<dependency> | |
<groupId>com.wordnik</groupId> | |
<artifactId>swagger-jaxrs_2.9.1</artifactId> | |
<scope>compile</scope> | |
<version>1.3.0-RC1</version> | |
</dependency> |
This file contains hidden or 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
/** | |
* Rest web service for operate the admin details | |
* | |
* @author dinuka | |
* | |
*/ | |
@Path("/" + APINames.ADMIN) | |
@Api(value = "/" + APINames.ADMIN, description = "Rest api for do operations on admin", produces = MediaType.APPLICATION_JSON) | |
@Produces({ MediaType.APPLICATION_JSON }) | |
public class AdminAPI { | |
@Context | |
private UriInfo context; | |
private static final String ACCEPT_HEADERS = "accept"; | |
@Context | |
private HttpHeaders headers; | |
@Context | |
private HttpServletRequest httpServletRequest; | |
@GET | |
@Path("/{userName}") | |
@Produces({ MediaType.APPLICATION_JSON }) | |
@Consumes({ MediaType.APPLICATION_JSON }) | |
@ApiOperation(value = "Get specific admin", httpMethod = "GET", notes = "Fetch the admin user details", response = Response.class) | |
@ApiResponses(value = { @ApiResponse(code = 200, message = "Given admin user found"), | |
@ApiResponse(code = 404, message = "Given admin user not found"), | |
@ApiResponse(code = 500, message = "Internal server error due to encoding the data"), | |
@ApiResponse(code = 400, message = "Bad request due to decoding the data"), | |
@ApiResponse(code = 412, message = "Pre condition failed due to required data not found") }) | |
public Response getAdmin( | |
@ApiParam(value = "user_name of admin", required = true) @PathParam("userName") String userName) { | |
AdminService adminService = AppContext.getInstance().getBean(AdminService.class); | |
try { | |
String adminData = ""; | |
// process the JSON type request | |
if (headers.getRequestHeaders().get(ACCEPT_HEADERS).contains(MediaType.APPLICATION_JSON)) { | |
adminData = adminService.getAdmin(EncoderDecoderType.JSON, "{user_name:\"" + userName + "\"}"); | |
} | |
// TODO: Need to process the XML type requests | |
if (adminData != "") { | |
return Response.ok().entity(adminData).build(); | |
} else { | |
return Response.status(404).build(); | |
} | |
} catch (EncodingException e) { | |
return Response.status(Status.INTERNAL_SERVER_ERROR).build(); | |
} catch (DecodingException e) { | |
return Response.status(Status.BAD_REQUEST).build(); | |
} catch (RequiredDataNotFoundException e) { | |
return Response.status(Status.PRECONDITION_FAILED).build(); | |
} | |
} |
This file contains hidden or 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 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" | |
version="3.0"> | |
<!--Specify the local deployment or openshift --> | |
<context-param> | |
<param-name>local-deployment</param-name> | |
<param-value>true</param-value> | |
</context-param> | |
<listener> | |
<listener-class>com.sw.protection.backend.listners.BackEndContextListner</listener-class> | |
</listener> | |
<session-config> | |
<session-timeout>30</session-timeout> | |
</session-config> | |
<servlet> | |
<servlet-name>Jersey Servlet</servlet-name> | |
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class> | |
<init-param> | |
<param-name>com.sun.jersey.config.property.packages</param-name> | |
<param-value>com.sw.protection.backend.rest;com.wordnik.swagger.jaxrs.listing</param-value> | |
</init-param> | |
<load-on-startup>1</load-on-startup> | |
</servlet> | |
<servlet-mapping> | |
<servlet-name>Jersey Servlet</servlet-name> | |
<url-pattern>/rest/*</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>3</param-value> | |
</init-param> | |
<init-param> | |
<param-name>swagger.api.basepath</param-name> | |
<param-value>http://localhost:8080/SW-Protection-Backend-1.0-SNAPSHOT/rest</param-value> | |
</init-param> | |
<load-on-startup>2</load-on-startup> | |
</servlet> | |
</web-app> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment