New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API docs url is empty and endpoints not accessible (404) #867

Closed
chrishuttonch opened this Issue Jul 21, 2015 · 3 comments

Comments

Projects
None yet
3 participants
@chrishuttonch

chrishuttonch commented Jul 21, 2015

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",
                "",
                "support@mysite.com",
                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

@chrishuttonch

This comment has been minimized.

chrishuttonch commented Jul 23, 2015

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());
@dilipkrish

This comment has been minimized.

Member

dilipkrish commented Jul 23, 2015

Ah great! Thanks for reporting your solution back 🤘 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 🍻

@dilipkrish dilipkrish added this to the 2.2.0 milestone Jul 23, 2015

@rstruthers1

This comment has been minimized.

rstruthers1 commented Dec 8, 2015

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment