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

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

How does AI transform chaos engineering from an experiment into a critical capability? Learn how to effectively operationalize the chaos.

Data quality isn't just a technical issue: It impacts an organization's compliance, operational efficiency, and customer satisfaction.

Are you a front-end or full-stack developer frustrated by front-end distractions? Learn to move forward with tooling and clear boundaries.

Developer Experience: Demand to support engineering teams has risen, and there is a shift from traditional DevOps to workflow improvements.

Related

  • How Spring and Hibernate Simplify Web and Database Management
  • Functional Endpoints: Alternative to Controllers in WebFlux
  • Graceful Shutdown: Spring Framework vs Golang Web Services
  • Actuator Enhancements: Spring Framework 6.2 and Spring Boot 3.4

Trending

  • Beyond Bytecode: Exploring the Relationship Between JVM, JIT, and Performance
  • OTel Me Why: The Case for OpenTelemetry Beyond the Shine
  • DevOps in the Cloud - How to Streamline Your CI/CD Pipeline for Multinational Teams
  • Master SQL Performance Optimization: Step-by-Step Techniques With Case Studies
  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.0K 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

  • How Spring and Hibernate Simplify Web and Database Management
  • Functional Endpoints: Alternative to Controllers in WebFlux
  • Graceful Shutdown: Spring Framework vs Golang Web Services
  • Actuator Enhancements: Spring Framework 6.2 and Spring Boot 3.4

Partner Resources

×

Comments

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

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • [email protected]

Let's be friends: