DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Building a CRUD Application With Spring and SimpleJdbcMapper
  • How to Marry MDC With Spring Integration
  • How Spring and Hibernate Simplify Web and Database Management
  • Functional Endpoints: Alternative to Controllers in WebFlux

Trending

  • Contract-First Integration: Building Scalable Systems With Flyway, OpenAPI, and Kafka
  • How We Diagnosed a Hidden Scheduler Failure in a Docker Swarm Cluster Serving 2 Million Users
  • Beyond Conversation: Mastering Context with Claude Code Skills and Agents
  • Spring Boot Done Right: Lessons From a 400-Module Codebase
  1. DZone
  2. Coding
  3. Frameworks
  4. XML-Based Swagger 2 Configuration With Spring MVC

XML-Based Swagger 2 Configuration With Spring MVC

Check out a step-by-step tutorial that explains how to configure Swagger 2 with Spring MVC using an XML-based configuration.

By 
Tapas Joshi user avatar
Tapas Joshi
·
Aug. 28, 18 · Tutorial
Likes (4)
Comment
Save
Tweet
Share
32.4K Views

Join the DZone community and get the full member experience.

Join For Free

Recently, I got a requirement to integrate Swagger 2 with Spring MVC. As the Spring configuration was done using XML based on the project, I had to go for that only, and I started looking for the example but could not find the proper article on the web immediately. Below is the step-by-step guide to configuring Swagger 2 with Spring MVC using an XML-based configuration.

  • First of all, add these below dependencies to your pom.xml.

<!--Dependency for swagger 2 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.7.0</version>
</dependency>

<!--Dependency for swagger ui -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.7.0</version>
</dependency>
  • Then, find out your MVC dispatcher servlet XML. Sometimes, people name it differently. Specify the base package for the component scan. Note that whatever package you have specified here, the REST services available under this package only will be eligible for Swagger.

<!--Here com.example.service is the base package for swagger configuration -->
<context:component-scan
base-package="com.example.service" use-default-filters="false">
<context:include-filter type="annotation"
expression="org.springframework.stereotype.Controller" />
<context:include-filter type="annotation"
expression="org.springframework.web.bind.annotation.ControllerAdvice" />
</context:component-scan>

  • Then declare the bean for Swagger 2 inside the same dispatcher servlet as below.

<bean id="swagger2Config"
class="springfox.documentation.swagger2.configuration.Swagger2DocumentationConfiguration">
</bean>
  • Then declare the resource mapping for the Swagger UI HTML file and webjars as below.

<mvc:resources order="1" location="/resources/"
mapping="/resources/**" />
<mvc:resources mapping="swagger-ui.html"
location="classpath:/META-INF/resources/" />
<mvc:resources mapping="/webjars/**"
location="classpath:/META-INF/resources/webjars/" />
  • Then declare this as the default servlet handler.

<mvc:default-servlet-handler />

Once you are done with the above steps, then your application is configured with Swagger 2. Now, during the deployment, Swagger will automatically find all the resources under the base package and generate the Swagger documentation.

To access your Swagger UI, you need to use the below URL:

http://<Your Host>:<Port>/<Context>/Swagger-ui.html 

Example: http://localhost:8080/ExampleProject/Swagger-ui.html

You can specify your REST APIs with Swagger specific annotation to provide better documentation to your services. For that, you can refer to the official Swagger core annotation page.

If you are using Spring Boot, then Swagger 2 configuration is very easy. There are lots of articles on the web for annotation-based Swagger configuration. One of them you can refer to is Setting Up Swagger 2 with a Spring REST API.

If you want to configure Swagger 2 with your XML based existing Spring application without changing any Java code, then I hope this helped!

Spring Framework

Opinions expressed by DZone contributors are their own.

Related

  • Building a CRUD Application With Spring and SimpleJdbcMapper
  • How to Marry MDC With Spring Integration
  • How Spring and Hibernate Simplify Web and Database Management
  • Functional Endpoints: Alternative to Controllers in WebFlux

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook