Springfox: API docs url is empty and endpoints not accessible (404)

Created on 21 Jul 2015  路  3Comments  路  Source: springfox/springfox

I think this might be more of a setup problem however the api-docs endpoint offers only an empty map and the endpoints for the documentation result in a 404.

The logs for my app show that the controllers are being scanned.

My configuration class:

package com.myorg.enterprise.docs;

import com.google.common.base.Predicate;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;


/**
 * A Configuration file that allows the generation of Swagger
 */
@Configuration
@EnableWebMvc
@EnableSwagger2
public class MyOrgSwaggerConfig {

    /**
     * Every SwaggerSpringMvcPlugin bean is picked up by the swagger-mvc framework - allowing for multiple
     * swagger groups i.e. same code base multiple swagger resource listings.
     *
     * @return SwaggerSpringMvcPlugin
     */
    @Bean
    public Docket customImplementation() {

        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(getApiInfo())
                .select()
                .paths(paths())
                .build()
                .pathMapping("/");
    }

    private Predicate<String> paths() {
        return PathSelectors.any();
    }

    /**
     * A method that returns the API Info
     * @return ApiInfo The Information including description
     */
    public ApiInfo getApiInfo() {
        return  new ApiInfo(
                "My REST API",
                "This REST API allows the user to manage domain objects",
                "1e",
                "",
                "[email protected]",
                null,
                null
        );
    }
}

The versions I'm using are:

        <!-- Swagger -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.1.1</version>
        </dependency>

        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.1.1</version>
        </dependency>

        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-spring-web</artifactId>
            <version>2.1.1</version>
        </dependency>

        <dependency>
            <groupId>io.swagger</groupId>
            <artifactId>swagger-annotations</artifactId>
            <version>1.5.0</version>
        </dependency>

        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-common</artifactId>
            <version>2.1.1</version>
        </dependency>
        <!-- End of Swagger -->

The application is running as ROOT context so there shouldn't be any problems with

My logs contain:

Created resource listing Path: /default/my-controller

However this is not accessible on http://localhost:8080/v2/api-docs/default/my-controller as expected.

Accessing http://localhost:8080/v2/api-docs returns:

{}

Please note I have had this working using the old com.mangofactory version and followed the migration documentation

Finally the swagger-ui.html is trying to access undefined:

http://localhost:8080undefined/

Any help or advice would be gratefully received

can-use-for-docs

Most helpful comment

The solution for Gson use in Swagger looks intriguing. However, when I try to use the code, none of the Swagger...Serializer classes are found. I included all the dependencies that were given in the original posting. Is there a particular dependency I need to add to get this to work?

All 3 comments

This was due to me using the GSON library to generate the Json. I switched on the excludeFieldsWithoutExposeAnnotation() which meant that none of the objects would produce any data. To get around this I created several serialisers for the following classes:

.registerTypeAdapter(springfox.documentation.service.ApiListing.class, new SwaggerApiListingJsonSerializer())
                .registerTypeAdapter(springfox.documentation.spring.web.json.Json.class, new SwaggerJsonSerializer())
                .registerTypeAdapter(springfox.documentation.swagger.web.SwaggerResource.class, new SwaggerResourceSerializer())
                .registerTypeAdapter(springfox.documentation.service.ResourceListing.class, new SwaggerResourceListingJsonSerializer())
                .registerTypeAdapter(springfox.documentation.swagger.web.SwaggerResource.class, new SwaggerResourceSerializer())
                .registerTypeAdapter(springfox.documentation.swagger.web.SecurityConfiguration.class, new SwaggerSecurityConfigurationSerializer())
                .registerTypeAdapter(springfox.documentation.swagger.web.UiConfiguration.class, new SwaggerUiConfigurationSerializer());

Ah great! Thanks for reporting your solution back :metal: I'll update the docs to reflect your fix.

In addition since we're not really supporting gson, would you be able to create an adapter for gson such that it always works if gson is found in the classpath? I might attempt it but since your more intimate it would be great if you could shepherd that! I'd be happy to collaborate 馃嵒

The solution for Gson use in Swagger looks intriguing. However, when I try to use the code, none of the Swagger...Serializer classes are found. I included all the dependencies that were given in the original posting. Is there a particular dependency I need to add to get this to work?

Was this page helpful?
0 / 5 - 0 ratings