Post4

Restful API Documentation using SWAGGER with Spring MVC

Posted by Vinit Kumar Tyagi

What is Swagger?

It is a specification for documenting REST API. It specifies the format (URL, method, and representation) to describe REST web services. Swagger is meant to enable the service producer to update the service documentation in real time so that the client and documentation systems are moving at the same pace as the server.

Steps to integrate Swagger in Spring Project:

Step 1:-    we should be adding following maven dependencies into the pom.xml.

              <dependency>

<groupId>io.springfox</groupId>

<artifactId>springfox-swagger2</artifactId>

<version>2.4.0</version>

</dependency>

<dependency>

<groupId>io.springfox</groupId>

<artifactId>springfox-swagger-ui</artifactId>

<version>2.4.0</version>

</dependency>

Note: – We can change swagger version based on the project requirement.

Step 2:- Add swagger configuration bean into the mvc-context.xml

       <mvc:resources mapping=“swagger-ui.html”location=“classpath:/META-INF/resources/” />

<mvc:resources mapping=“/webjars/**”location=“classpath:/META-INF/resources/webjars/” />

<bean id=“swagger2Config”class=“springfox.documentation.swagger2.configuration.Swagger2DocumentationConfiguration”></bean>

Make sure to have these configurations enabled:

       <mvc:default-servlet-handler />

<mvc:annotation-driven />

Step 3:- After the configurations are enabled, we can start using Swagger to document our controllers

@RestController

@Api(value=”getMovieName”, description=”Get movie name detail”)

@RequestMapping(“/movie”)

public class MovieController {

@RequestMapping(value=”/{name}”, method = RequestMethod.GET)

public String getMovie(@PathVariable String name, ModelMapmodel) {

model.addAttribute(“movie”, name);

return “list”;

}

}

Below are some key points if in case it is not working:-

  1. Make sure our project will be filter html,js and other components, if not enable in web.
<servlet-mapping>

<servlet-name>default</servlet-name>

<url-pattern>*.js</url-pattern>

<url-pattern>*.css</url-pattern>

<url-pattern>*.html</url-pattern>

<url-pattern>*.png</url-pattern>

<url-pattern>*.ttf</url-pattern>

<url-pattern>*.gif</url-pattern>

</servlet-mapping>

For more such blogs, keep following us on TechBlog!

Until Next time!

Related Posts

  • Flyway Integration with Spring MVCFlyway Integration with Spring MVC

    Flyway is an open-source database migration tool that supports simplicity and convention over configuration. It makes setting up and maintaining database schemas a breeze. Flyway is used across all environments…

  • Introduction to Messaging

    Messaging is one of the most important aspects of modern programming techniques. Majority of today's systems consist of several modules and external dependencies. If they weren't able to communicate with…

  • Incorporating Spring Retry in ProjectsIncorporating Spring Retry in Projects

    Spring Retry is used to automatically re-invoke a failed operation. It helps in making the overall process more robust and less prone to failure. Spring Retry is very helpful in…

  • Real Time Data Ingestion (DiP) – Spark Streaming (co-dev opportunity)Real Time Data Ingestion (DiP) – Spark Streaming (co-dev opportunity)

    This blog is an extension to that and it focuses on integrating Spark Streaming to Data Ingestion Platform for performing real time data ingestion and visualization. The previous blog DiP (Storm Streaming) showed how…

  • Data Ingestion Platform(DiP) – Real time data analysis – Flink StreamingData Ingestion Platform(DiP) – Real time data analysis – Flink Streaming

    This blog is an extension to that and it focuses on using Flink Streaming for performing real time data ingestion. The previous blog DiP (Storm Streaming) showed how we can leverage the power of…

  • Hadoop Cluster Verification (HCV)Hadoop Cluster Verification (HCV)

    Verification scripts basically composed of idea to run a smoke test against any Hadoop component using shell script. HCV is a set of artifacts developed to verify successful implementation of…

Leave a Reply

Your email address will not be published. Required fields are marked *