SpringMVC + Swagger

Swagger generates documentation for your REST api. It has out-of-the-box support for JAX-RS (e.g. Jersey), but with SpringMVC (in this case, 3.2.16) it’s a bit harder to get things running.

Configure Springfox

Springfox (I use 2.5.0) scans the Spring annotations and generates the swagger API description. Add the dependency to the Maven configuration.


After this, adding a Configuration bean is enough to get the Swagger api-docs resource running. If your DispatcherServlet is configured on the web-app root (/), you’ll find the api description on http://localhost:8080/v2/api-docs

public class SwaggerConfig {    
    public Docket swaggerDocket() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(new ApiInfo("Api", "Api Documentation", "x.x.x", "", DEFAULT_CONTACT, "", "")) 

Adding Swagger UI to the web application

We already added the springfox-swagger-ui dependency to Maven (see first code snippet). Springfox just wraps the normal Swagger UI to make it more convenient in Spring applications.

By default, swagger-ui will run under your base path. I like to put it under /documentation, so I had to fix 2 things. First, I had to map the static resources in the Swagger UI webjar to the right location.

<!-- Spring XML config to map the Swagger UI webjar -->
<mvc:resources mapping="/documentation/**" location="classpath:/META-INF/resources/"/>

Second, I had to redirect the existing swagger resources because the UI starts looking for them relative to its own location. (For this I couldn’t find a better solution, perhaps configuring this in XML will make it more convenient)

        description = "SwaggerUI is located under /documentation. This mapping redirects the necessary resources for the ui.",
        hidden = true
public class DocumentationController {

    private static final String DOCUMENTATION_API_DOCS = "/documentation/api-docs";
    private static final String SWAGGER_RESOURCES = "/swagger-resources";
    private static final String CONFIGURATION_UI = "/swagger-resources/configuration/ui";
    private static final String CONFIGURATION_SECURITY = "/swagger-resources/configuration/security";

    public RedirectView swaggerResources() {
        return new RedirectView(SWAGGER_RESOURCES, true);

    public RedirectView swaggerApiDocs() {
        return new RedirectView(DOCUMENTATION_API_DOCS, true);

    public RedirectView swaggerUI() {
        return new RedirectView(CONFIGURATION_UI, true);

    public RedirectView swaggerConfigSecurity() {
        return new RedirectView(CONFIGURATION_SECURITY, true);

Annotate your API’s

Springfox does a fine job in generating documentation based on the Spring annotations. If you want to provide more information, use the Swagger Core annotations. See this JAX-RS resource as example.

@io.swagger.annotations.Api(tags = "Agreement", description = "Agreement")
public class AgreementController extends AbstractController {

Hide deprecated API’s

To finish my first Swagger implemenation, I tuned the Springfox Docket a bit.

  1. I hide deprecated methods with a Guava predicate (see apis() method).
  2. I specified a regex for the Paths I want to show (that drops the earlier created DocumentationController in one go)
  3. I added some type conversions. If one use the API, these type conversions are done by our Jackson ObjectMapper to generate JSON. Springfox doesn’t seem to be clever enough to get these conversions right.
return new Docket(DocumentationType.SWAGGER_2)
        .select()        .apis(Predicates.not(RequestHandlerSelectors.withClassAnnotation(Deprecated.class)))
        .directModelSubstitute(LocalDateTime.class, int[].class)
        .directModelSubstitute(LocalDate.class, int[].class)
        .directModelSubstitute(CountryCode.class, String.class)


With a few steps I got Swagger to work for a SpringMVC project using Springfox. It doesn’t feel perfect yet, especially if you used Swagger for JAX-RS projects in the past.