Swagger 2 Configuration With Spring (XML)

There are a lot of descriptions on how to configure Swagger for work with a Java Spring project.

Basically, they describe an integration Swagger via annotation type in the Java configuration files.

We have a project that was configured via Spring XML files. It was a bit difficult to find a properly working solution for our case. By the way, it exists.

First of all, it needs to add dependencies to your maven or gradle file:

compile group: ‘io.springfox’, name: ‘springfox-swagger2’, version: ‘2.9.2’
compile group: ‘io.springfox’, name: ‘springfox-swagger-ui’, version: ‘2.9.2’

Other examples enable on the Maven repository site.

Then, it needs to find your configuration file for Spring MVC and add to it the next rows (in our case, it was restContext.xml), where “com.amg.html.controller” is your controller package:

<bean id="swagger2Config"

<mvc:resources order="1" location="classpath:/META-INF/resources/" mapping="/resources/**" />
<mvc:resources mapping="swagger-ui.html"
location="classpath:/META-INF/resources/" />
<mvc:resources mapping="/webjars/**"
location="classpath:/META-INF/resources/webjars/" />

<mvc:default-servlet-handler />

<context:component-scan base-package="com.amg.html.controller">
<context:include-filter type="annotation"
expression="org.springframework.stereotype.Controller" />
<context:include-filter type="annotation"
expression="org.springframework.web.bind.annotation.ControllerAdvice" />

This is everything. After building and deploying, Swagger will automatically find all the resources in your controller package and generate the documentation.

Swagger UI will be enabled on this URL:
http://<your host>:<port>/<context>/swagger-ui.html

To check your swagger configuration you can type in a browser:
http://<your host>:<port>/<context>/v2/api-docs
It will display swagger’s JSON dump. If you see the JSON dump you implement all properly.

Just for the record, in the configuration file for Spring MVC (see above) we use a link to default Java configuration file for Swagger (Swagger2DocumentationConfiguration). But you always can use your configuration file. It may be look like this:

public class SwaggerConfig extends WebMvcConfigurationSupport {

public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)

protected ApiInfo metaData() {
return new ApiInfo(
"My project REST API",
"Rest API description",
"Terms of service",
new Contact("Me", "", "me@test.ru"),
"License of API", "API license URL", Collections.emptyList());

In this case, it should change the link to Swagger2DocumentationConfiguration on your SwaggerConfig in your configuration file for Spring MVC.

I hope this article will help you to configure Swagger via XML.