0

我有一个spring mvc 我正在迁移springfoxspringdoc swagger 我已按照此链接中提到的步骤进行操作,但是当我尝试访问 swagger 页面时,它给了我以下错误

未找到带有 URL“/swagger-ui/index.html”的 http 请求的映射

这是我的依赖项

org.springdoc springdoc-openapi-ui 1.5.8

web.xml

<?xml version="1.0" encoding="UTF-8"?>

<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/j2ee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
         http://java.sun.com/xml/ns/j2ee/web-app_2_5.xsd">

    <display-name>TestService</display-name>

    <context-param>
        <param-name>applicationName</param-name>
        <param-value>Test</param-value>
    </context-param>

    <listener>
        <listener-class>org.apache.logging.log4j.web.Log4jServletContextListener</listener-class>
    </listener>

    <!-- Provide access to the ServletContext through ServletContextSingleton -->
    <listener>
        <listener-class>com.example.test.servlet.ApplicationListener</listener-class>
    </listener>

    <servlet>
        <servlet-name>Test</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

        <load-on-startup>1</load-on-startup>
    </servlet>
    

    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>/swagger-ui.html</url-pattern>
    </servlet-mapping>

    <servlet-mapping>
        <servlet-name>Test</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <filter>
        <filter-name>RequestContextFilter</filter-name>
        <filter-class>com.example.test.rest.web.filter.RequestContextFilter</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>RequestContextFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

</web-app>

WebConfig.java

@Configuration
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter {

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

这是我的拦截器

<mvc:interceptors>
        <mvc:interceptor>
            <mvc:mapping path="/**"/>
            <mvc:exclude-mapping path="/swagger**"/>
            <mvc:exclude-mapping path="/swagger-ui**"/>
            <mvc:exclude-mapping path="/swagger-ui/**"/>
            <mvc:exclude-mapping path="/swagger-resources/**"/>
            <mvc:exclude-mapping path="/v3/**"/>
            <mvc:exclude-mapping path="/v2/api-docs**"/>
            <mvc:exclude-mapping path="/v3/api-docs**"/>
            <mvc:exclude-mapping path="/configuration/security**"/>
            <mvc:exclude-mapping path="/configuration/ui**"/>
            <mvc:exclude-mapping path="/webjars/**"/>
            <beans:ref bean="localAuthorizationInterceptor"/>
        </mvc:interceptor>
    </mvc:interceptors>

我不确定我在这里做错了什么,

任何帮助,将不胜感激。

4

1 回答 1

0

Springdoc 与 Swagger 直接不兼容,但都支持 open-api3。由于最小分母是 oa3,因此您应该v3在 url 中有一个。

尝试其中一个

http://localhost:8080/v3/swagger-ui/
http://localhost:8080/v3/swagger-ui.html
http://localhost:8080/v3/api-docs/swagger-config

请注意,springdoc 有与 oa3/swagger 不兼容的“投影”。如果您喜欢使用“投影”,您最好留在 springdoc 上,但这只是我的意见。

于 2021-11-29T11:05:28.670 回答