Skip to main content

Swagger UI with Java RESTful Service

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.


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)
                       https://jar-download.com/?search_box=swagger-jersey2-jaxrs
  •        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,


Finally run the application to see the Swagger Documentation UI.
ex: https://localhost:8080/swaggerapp/index.html





Comments

Popular posts from this blog

SAP - Java Application Development with Jco3 Connection

This is the post to explain the way of achieve the connection between non SAP Web application interacting with SAP backend RFC's through Jco3 connector. Mandatory Files: * sapjco3.jar (place in lib folder) * sapjco3.dll (required only windows based machine ,need to place under " C:\Windows\System32 " folder.) Required to download it from SAP market place with valid credentials,consider the version 32/64 bits depends on your work environment) Steps to connect SAP Backend: 1. Create a Stand alone/web application using any development tool. 2. Include jar and dll files in above specified folder 3. Create a connection class to connect backend with valid credentials 4. Create a service or consumer class with RFC call code to utilise the connection 5. Close the connection Make sure you have access and no firewall blocks for the SAP backend connection. Example Connection Code: sapConnect.java import com.sap.conn.jco.JCoDestination; import com.s...

Maven vs Gradle

What is Gradle? Gradle Build Tool is a fast, dependable, and adaptable open-source build automation tool with an elegant and extensible declarative build language. Gradle is a widely used and mature tool with an active community and a strong developer ecosystem. •            Gradle is the most popular build system for the JVM and is the default system for Android and Kotlin Multi-Platform projects. It has a rich community plugin ecosystem. •            Gradle can automate a wide range of software build scenarios using either its built-in functionality, third-party plugins, or custom build logic. •            Gradle provides a high-level, declarative, and expressive build language that makes it easy to read and write build logic. •            Gradle is fast, scalable, and can bui...

JSF2 with Mysql CRUD Web Application Demo

Java based CRUD demo application development with the combination of JSF2 framework ,Mysql database using Netbean IDE. Beginners can  understand initial configuration steps in JSF2  application development. Steps Followed: *  Install MySql Server with proper configuration and create some table with set of records or restore from your backup ,If you have anything. *  Download mySql Connector Drivers from official site. *  Install Netbean IDE with Server either Tomcat or Glassfish *  Create New Web Application and select the server and Framework (JSF2 used for this demo with Primefaces ). * Select the JPA framework to create a datasource connection and it will generate the the DAO and service classes based on Facade Design pattern. * Create JSF managed beans and Xhtml pages from the Pojo's (It's an automated process when you follow the video). * This video will help to understand the basic connection between frontend DAO's and business log...