Below is the step-by-step guide to configuring Swagger 2 with Spring MVC using an XML-based configuration.
Env
spring version: 4.0.5.RELEASE
servlet-api: 2.5
Springfox-swagger: 2.6.1
Steps
pom.xml
1 2 3 4 5 6 7 8 9 10 11 12 13
<!--Dependency for swagger 2 --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.6.1</version> </dependency> <!--Dependency for swagger ui --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.6.1</version> </dependency>
servlet-context.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
<!--Here com.example.service is the base package for swagger configuration --> <context:component-scanbase-package="com.example.service"use-default-filters="false"> <context:include-filtertype="annotation"expression="org.springframework.stereotype.Controller" /> <context:include-filtertype="annotation"expression="org.springframework.web.bind.annotation.ControllerAdvice" /> </context:component-scan>
<!-- for swagger --> <beanid="swagger2Config" class="springfox.documentation.swagger2.configuration.Swagger2DocumentationConfiguration"> </bean>