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
Newsletter
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

  • Taming Parameter Sniffing in SQL Server 2022 With PSP Optimization
  • Evals in CI: How to Catch Agent Regressions Before Production Does
  • PWA on iOS, Part 2: HTTPS, Manifest and Service Worker
  • Closing the Drift Loop: An AI Agent That Drafts Terraform Fixes

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  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
·
Aug. 28, 18 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
32.5K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

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

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • 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