swagger-ui 找不到 HTTP 请求的映射

2022-09-02 11:08:44

我正在尝试记录和现有的Rest API一个Spring MVC项目(不是Spring Boot!

我的应用程序称为 api,因此 http://localhost:9090/api 将是根终结点。因为我使用的是 spring-data-rest,所以在该 URL 上,我可以看到我所有公开的存储库的 json。目前为止,一切都好。

我也可以访问swagger JSON http://localhost:9090/api/v2/api-docs

问题

我无法访问 http://localhost:9090/api/swagger-ui.html 上的 swagger-UI 组件。它给了我

WARN  org.springframework.web.servlet.PageNotFound- No mapping found for HTTP request with URI [/api/swagger-ui.html] in DispatcherServlet with name 'dispatcher'

启动tomcat时检查春季日志,我可以看到一些奇怪的东西

DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory- Finished creating instance of bean 'swaggerApiListingReader'
DEBUG org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping- Rejected bean name 'swaggerConfig': no URL paths identified
DEBUG org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping- Rejected bean name 'swagger2Controller': no URL paths identified
DEBUG org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping- Rejected bean name 'swaggerMediaTypeReader': no URL paths identified
DEBUG org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping- Rejected bean name 'swaggerOperationModelsProvider': no URL paths identified
DEBUG org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping- Rejected bean name 'swaggerOperationResponseClassReader': no URL paths identified
DEBUG org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping- Rejected bean name 'swaggerOperationTagsReader': no URL paths identified
DEBUG org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping- Rejected bean name 'swaggerResponseMessageReader': no URL paths identified
DEBUG org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping- Rejected bean name 'swaggerParameterDescriptionReader': no URL paths identified
DEBUG org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping- Rejected bean name 'swaggerExpandedParameterBuilder': no URL paths identified
DEBUG org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping- Rejected bean name 'swaggerApiListingReader': no URL paths identified
DEBUG org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping- Rejected bean name 'swaggerProperties': no URL paths identified
DEBUG org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping- Rejected bean name 'springfox.documentation.swagger.configuration.SwaggerCommonConfiguration': no URL paths identified
DEBUG org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping- Rejected bean name 'springfox.documentation.swagger2.configuration.Swagger2DocumentationConfiguration': no URL paths identified
DEBUG org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping- Rejected bean name 'swagger2Module': no URL paths identified

这似乎表明由于某种原因,swaggerController未与任何URL相关联,因此出现404错误。

这些是我正在使用的版本

  <spring.version>4.2.8.RELEASE</spring.version>
  <spring-data.version>Gosling-SR4</spring-data.version>
  <spring-data-rest>2.4.6.RELEASE</spring-data-rest> 
   <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger2</artifactId>
        <version>2.6.1</version>
    </dependency>
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger-ui</artifactId>
        <version>2.6.1</version>
    </dependency>
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-data-rest</artifactId>
        <version>2.6.1</version>
    </dependency>

这是我的Java会议。值得指出的是,addResourceHandlers方法永远不会被执行。

    @Configuration
    @EnableSwagger2
    @EnableWebMvc

  @Import({springfox.documentation.spring.data.rest.configuration.SpringDataRestConfiguration.class})
public class SwaggerConfig extends WebMvcConfigurerAdapter {
    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any())
                .build();
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {

        System.out.println("******************************Configuring swagger resource handler");
        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
    }
}

有什么建议吗?简而言之,swagger-ui不起作用。


答案 1

这个解决方案是由@oksett

创建一个配置类,该类扩展并重写以下方法:WebMvcConfigurerAdapter

如果使用 Spring 5,则使用扩展实现WebMvcConfigurerAdapterWebMvcConfigurer

@Override
public void addViewControllers(ViewControllerRegistry registry) {
    registry.addRedirectViewController("/api/v2/api-docs", "/v2/api-docs");
    registry.addRedirectViewController("/api/swagger-resources/configuration/ui", "/swagger-resources/configuration/ui");
    registry.addRedirectViewController("/api/swagger-resources/configuration/security", "/swagger-resources/configuration/security");
    registry.addRedirectViewController("/api/swagger-resources", "/swagger-resources");
}

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/api/swagger-ui.html**").addResourceLocations("classpath:/META-INF/resources/swagger-ui.html");
    registry.addResourceHandler("/api/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
}

在您的情况下,您需要添加和实现 到 。另请注意方法中要附加到救援处理程序位置前面的更改。addViewControllersSwaggerConfigaddResourceHandlers/api

您现在应该能够访问我的 swagger-ui.html @http://localhost:9090/api/swagger-ui.html


答案 2

以下是有关如何使用 swagger 文档和工作 UI 配置 Spring boot 的完整代码。

啪.xml

<!-- Spring boot version is 2.0.3.RELEASE-->

<dependencies>
  <!-- Swagger 2 Doc Dependency -->
  <dependency>
      <groupId>io.springfox</groupId>
      <artifactId>springfox-swagger2</artifactId>
      <version>2.9.2</version>
  </dependency>

  <!-- Swagger 2 UI -->
  <dependency>
      <groupId>io.springfox</groupId>
      <artifactId>springfox-swagger-ui</artifactId>
      <version>2.9.2</version>
  </dependency>
</dependencies>

SwaggerdocConfig.java

package com.example.springbootswagger.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.PropertySource;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import springfox.bean.validators.configuration.BeanValidatorPluginsConfiguration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class SwaggerDocConfig implements WebMvcConfigurer {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {

        registry
                .addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");

        registry
                .addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
    }

    @Bean
    public Docket apiDocket() {

        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(getApiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.example.springbootswagger.controller"))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo getApiInfo() {

        return new ApiInfoBuilder()
                .title("Swagger API Doc")
                .description("More description about the API")
                .version("1.0.0")
                .build();
    }
}

推荐