Swagger Implementation with REST API
This is the post to explain how we can integrate swagger UI with existing REST API application for live documentation.
This is the post to explain how we can integrate swagger UI with existing REST API application for live documentation.
Pre-requisites:
Swagger UI
- Download the UI files from Git Hub repository and extract the zip file from
- https://github.com/swagger-api/swagger-ui/releases (download latest version API)
- Find the dist folder and copy all the files to our project path “web” folder in our project.
Swagger Jars
- To download the jar dependency go to the repository site to get all jar without missing dependency files(swagger-jersey2-jaxrs version 1.5.9)
- Download it from the above link and extract and import into project path.
then clean build the
application once the mentioned steps completed.
Setup REST API path:
•
Find the ApplicationConfig.java file from the
source code package and remove the files to avoid the JavaBean based
configuration of API path.
•
Go to web.xml
file and add the following configuration parameters to provide service path to
API’s.
•
com.restapi contains the API classes change the package according while configure your rest
packages into web.xml
Html File Configuration
•
Open index.html (coped from dest folder) and change the default
url to your application path
RestApi configuration
·
Finally we need to annotate our rest api classes
with swagger annotation parameters.it will expose our service methods with
field’s description for end user understanding.
·
Find the below code for reference,
@Path("email")
@Api(value =
"/email", description = "Email")
public class email {
@POST
@Path("data")
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(value = "Email api", response = Response.class)
@ApiResponses(value = {
@ApiResponse(code = 200, message =
"Success"),
@ApiResponse(code = 401, message =
"You are not authorized to view the resource"),
@ApiResponse(code = 403, message =
"Accessing the resource you were trying to reach is forbidden"),
@ApiResponse(code = 404, message =
"The resource you were trying to reach is not found") })
public response postMethod(String request body)
{
// some business logic
return response;
}
Find this below link for further details,
Comments
Post a Comment