使用 swagger-springmvc(现为 springfox)查看 swagger api-docs 时出现“404 Not Found”

Posted

技术标签:

【中文标题】使用 swagger-springmvc(现为 springfox)查看 swagger api-docs 时出现“404 Not Found”【英文标题】:"404 Not Found" when viewing swagger api-docs when using swagger-springmvc (now springfox) 【发布时间】:2015-07-22 20:57:31 【问题描述】:

我正在尝试在我的 spring 项目中配置 swagger,但点击“http://localhost:8080/api-docs”会显示“404 Not Found”。

Maven 依赖

<dependency>            
  <groupId>com.mangofactory</groupId>           
  <artifactId>swagger-springmvc</artifactId>                   
  <version>0.8.2</version>      
</dependency>

SwaggerConfig.java 文件

package com.ucap.swagger;

import com.mangofactory.swagger.configuration.JacksonScalaSupport;
import com.mangofactory.swagger.configuration.SpringSwaggerConfig;
import com.mangofactory.swagger.configuration.SpringSwaggerModelConfig;
import com.mangofactory.swagger.configuration.SwaggerGlobalSettings;
import com.mangofactory.swagger.core.DefaultSwaggerPathProvider;
import com.mangofactory.swagger.core.SwaggerApiResourceListing;
import com.mangofactory.swagger.core.SwaggerPathProvider;
import com.mangofactory.swagger.scanners.ApiListingReferenceScanner;
import com.wordnik.swagger.model.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static com.google.common.collect.Lists.newArrayList;

@Configuration
@ComponentScan(basePackages = "com.mangofactory.swagger")
public class SwaggerConfig 

    public static final List<String> DEFAULT_INCLUDE_PATTERNS = Arrays.asList("/news/.*");
    public static final String SWAGGER_GROUP = "mobile-api";

    @Value("$app.docs")
    private String docsLocation;

    @Autowired
    private SpringSwaggerConfig springSwaggerConfig;
    @Autowired
    private SpringSwaggerModelConfig springSwaggerModelConfig;

    /**
     * Adds the jackson scala module to the MappingJackson2HttpMessageConverter registered with spring
     * Swagger core models are scala so we need to be able to convert to JSON
     * Also registers some custom serializers needed to transform swagger models to swagger-ui required json format
     */
    @Bean
    public JacksonScalaSupport jacksonScalaSupport() 
        JacksonScalaSupport jacksonScalaSupport = new JacksonScalaSupport();
        //Set to false to disable
        jacksonScalaSupport.setRegisterScalaModule(true);
        return jacksonScalaSupport;
    

    /**
     * Global swagger settings
     */
    @Bean
    public SwaggerGlobalSettings swaggerGlobalSettings() 
        SwaggerGlobalSettings swaggerGlobalSettings = new SwaggerGlobalSettings();
        swaggerGlobalSettings.setGlobalResponseMessages(springSwaggerConfig.defaultResponseMessages());
        swaggerGlobalSettings.setIgnorableParameterTypes(springSwaggerConfig.defaultIgnorableParameterTypes());
        swaggerGlobalSettings.setParameterDataTypes(springSwaggerModelConfig.defaultParameterDataTypes());
        return swaggerGlobalSettings;
    

    /**
     * API Info as it appears on the swagger-ui page
     */
    private ApiInfo apiInfo() 
        ApiInfo apiInfo = new ApiInfo(
                "News API",
                "Mobile applications and beyond!",
                "https://helloreverb.com/terms/",
                "matt@raibledesigns.com",
                "Apache 2.0",
                "http://www.apache.org/licenses/LICENSE-2.0.html"
        );
        return apiInfo;
    

    /**
     * Configure a SwaggerApiResourceListing for each swagger instance within your app. e.g. 1. private  2. external apis
     * Required to be a spring bean as spring will call the postConstruct method to bootstrap swagger scanning.
     *
     * @return
     */
    @Bean
    public SwaggerApiResourceListing swaggerApiResourceListing() 
        //The group name is important and should match the group set on ApiListingReferenceScanner
        //Note that swaggerCache() is by DefaultSwaggerController to serve the swagger json
        SwaggerApiResourceListing swaggerApiResourceListing = new SwaggerApiResourceListing(springSwaggerConfig.swaggerCache(), SWAGGER_GROUP);

        //Set the required swagger settings
        swaggerApiResourceListing.setSwaggerGlobalSettings(swaggerGlobalSettings());

        //Use a custom path provider or springSwaggerConfig.defaultSwaggerPathProvider()
        swaggerApiResourceListing.setSwaggerPathProvider(apiPathProvider());

        //Supply the API Info as it should appear on swagger-ui web page
        swaggerApiResourceListing.setApiInfo(apiInfo());

        //Global authorization - see the swagger documentation
        swaggerApiResourceListing.setAuthorizationTypes(authorizationTypes());

        //Every SwaggerApiResourceListing needs an ApiListingReferenceScanner to scan the spring request mappings
        swaggerApiResourceListing.setApiListingReferenceScanner(apiListingReferenceScanner());
        return swaggerApiResourceListing;
    

    @Bean
    /**
     * The ApiListingReferenceScanner does most of the work.
     * Scans the appropriate spring RequestMappingHandlerMappings
     * Applies the correct absolute paths to the generated swagger resources
     */
    public ApiListingReferenceScanner apiListingReferenceScanner() 
        ApiListingReferenceScanner apiListingReferenceScanner = new ApiListingReferenceScanner();

        //Picks up all of the registered spring RequestMappingHandlerMappings for scanning
        apiListingReferenceScanner.setRequestMappingHandlerMapping(springSwaggerConfig.swaggerRequestMappingHandlerMappings());

        //Excludes any controllers with the supplied annotations
        apiListingReferenceScanner.setExcludeAnnotations(springSwaggerConfig.defaultExcludeAnnotations());

        //
        apiListingReferenceScanner.setResourceGroupingStrategy(springSwaggerConfig.defaultResourceGroupingStrategy());

        //Path provider used to generate the appropriate uri's
        apiListingReferenceScanner.setSwaggerPathProvider(apiPathProvider());

        //Must match the swagger group set on the SwaggerApiResourceListing
        apiListingReferenceScanner.setSwaggerGroup(SWAGGER_GROUP);

        //Only include paths that match the supplied regular expressions
        apiListingReferenceScanner.setIncludePatterns(DEFAULT_INCLUDE_PATTERNS);

        return apiListingReferenceScanner;
    

    /**
     * Example of a custom path provider
     */
    @Bean
    public ApiPathProvider apiPathProvider() 
        ApiPathProvider apiPathProvider = new ApiPathProvider(docsLocation);
        apiPathProvider.setDefaultSwaggerPathProvider(springSwaggerConfig.defaultSwaggerPathProvider());
        return apiPathProvider;
    


    private List<AuthorizationType> authorizationTypes() 
        ArrayList<AuthorizationType> authorizationTypes = new ArrayList<>();

        List<AuthorizationScope> authorizationScopeList = newArrayList();
        authorizationScopeList.add(new AuthorizationScope("global", "access all"));

        List<GrantType> grantTypes = newArrayList();

        LoginEndpoint loginEndpoint = new LoginEndpoint(apiPathProvider().getAppBasePath() + "/user/authenticate");
        grantTypes.add(new ImplicitGrant(loginEndpoint, "access_token"));

        return authorizationTypes;
    

    @Bean
    public SwaggerPathProvider relativeSwaggerPathProvider() 
        return new ApiRelativeSwaggerPathProvider();
    

    private class ApiRelativeSwaggerPathProvider extends DefaultSwaggerPathProvider 
        @Override
        public String getAppBasePath() 
            return "/";
        

        @Override
        public String getSwaggerDocumentationBasePath() 
            return "/api-docs";
        
    

ApiPathProvider.java 文件

package com.ucap.swagger;

import com.mangofactory.swagger.core.SwaggerPathProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.util.UriComponentsBuilder;

import javax.servlet.ServletContext;

public class ApiPathProvider implements SwaggerPathProvider 
    private SwaggerPathProvider defaultSwaggerPathProvider;
    @Autowired
    private ServletContext servletContext;

    private String docsLocation;

    public ApiPathProvider(String docsLocation) 
        this.docsLocation = docsLocation;
    

    @Override
    public String getApiResourcePrefix() 
        return defaultSwaggerPathProvider.getApiResourcePrefix();
    

    public String getAppBasePath() 
        return UriComponentsBuilder
                .fromHttpUrl(docsLocation)
                .path(servletContext.getContextPath())
                .build()
                .toString();
    

    @Override
    public String getSwaggerDocumentationBasePath() 
        return UriComponentsBuilder
                .fromHttpUrl(getAppBasePath())
                .pathSegment("api-docs/")
                .build()
                .toString();
    

    @Override
    public String getRequestMappingEndpoint(String requestMappingPattern) 
        return defaultSwaggerPathProvider.getRequestMappingEndpoint(requestMappingPattern);
    

    public void setDefaultSwaggerPathProvider(SwaggerPathProvider defaultSwaggerPathProvider) 
        this.defaultSwaggerPathProvider = defaultSwaggerPathProvider;
    

此外,资源文件夹中有一个属性文件,其中包含:

app.docs=http://localhost:8080

当我点击“http://localhost:8080/api-docs”时,它既没有给出任何结果也没有任何错误,在谷歌休息时,响应代码是 404。

请帮帮我。

【问题讨论】:

【参考方案1】:

您使用的版本 (0.8.2) 是非常旧的 springfox 版本。您应该尝试迁移到最新发布的版本,即2.0。

专门回答您的问题。

    移至最新的 2.0 之前的版本。将 pom 中 swagger-spingmvc 的版本更改为 1.0.2 在以后的版本中大大简化了 Swagger 配置。将您的 SwaggerConfig 更改为如下所示。
@Configuration
//Hard to tell without seeing all your configuration, but optionally, 
//add EnableWebMvc annotation in case its not working
@EnableWebMvc 
@EnableSwagger
//Assuming your controllers are in this package
@ComponentScan(basePackages = "com.ucap.swagger") 
public class SwaggerConfig 

    public static final List<String> DEFAULT_INCLUDE_PATTERNS 
             = Arrays.asList("/news/.*");
    //Unless you specifically want this group, I would recommend ignoring this
    public static final String SWAGGER_GROUP = "mobile-api"; 

    @Autowired
    private SpringSwaggerConfig springSwaggerConfig;



    @Bean
    public SwaggerSpringMvcPlugin customImplementation()
        return new SwaggerSpringMvcPlugin(this.springSwaggerConfig)
          .includePatterns(DEFAULT_INCLUDE_PATTERNS);
    

    您不再需要路径提供程序。 您不再需要属性文件

【讨论】:

代码行 " return new SwaggerSpringMvcPlugin(this.springSwaggerConfig) .includePatterns(DEFAULT_INCLUDE_PATTERNS);"抛出编译时错误“SwaggerSpringMvcPlugin 类型中的方法 includePatterns(String...) 不适用于参数 (List)

以上是关于使用 swagger-springmvc(现为 springfox)查看 swagger api-docs 时出现“404 Not Found”的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 Web GUI 停止 Google Cloud AutoML(现为 Vertex AI)批量预测作业?

当矩阵在 C 中实现为列表时重新排列矩阵行

为啥接口的泛型方法可以在 Java 中实现为非泛型?

使子视图在 html 中表现为浮动作品

如何检查是不是在 Selenium 中选中了在 ExtJs 中实现为按钮的复选框? [复制]

swagger使用总结