无法在Springboot中加载API定义

jchrr9hc  于 2022-12-04  发布在  Spring
关注(0)|答案(8)|浏览(265)

我正在移动到Spring doc open Api并试图点击该URL。我从控制台获得以下错误和日志。
网址:http://localhost:8080/swagger-ui/index.html?url=v3/api-docs
记录档:

2020-03-24 13:21:03.930 DEBUG 32622 --- [nio-8080-exec-3] o.s.web.servlet.DispatcherServlet        : GET "/swagger-ui/index.html?url=v3/api-docs", parameters={masked}
2020-03-24 13:21:03.931 DEBUG 32622 --- [nio-8080-exec-3] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped to ResourceHttpRequestHandler ["classpath:/META-INF/resources/webjars/"]
2020-03-24 13:21:03.933 DEBUG 32622 --- [nio-8080-exec-3] o.s.web.servlet.DispatcherServlet        : Completed 304 NOT_MODIFIED
2020-03-24 13:21:03.992 DEBUG 32622 --- [nio-8080-exec-4] o.s.web.servlet.DispatcherServlet        : GET "/swagger-ui/v3/api-docs", parameters={}
2020-03-24 13:21:03.993 DEBUG 32622 --- [nio-8080-exec-4] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped to ResourceHttpRequestHandler ["classpath:/META-INF/resources/webjars/"]
2020-03-24 13:21:03.994 DEBUG 32622 --- [nio-8080-exec-4] o.s.w.s.r.ResourceHttpRequestHandler     : Resource not found
2020-03-24 13:21:03.994 DEBUG 32622 --- [nio-8080-exec-4] o.s.web.servlet.DispatcherServlet        : Completed 404 NOT_FOUND
2020-03-24 13:21:03.994 DEBUG 32622 --- [nio-8080-exec-4] o.s.web.servlet.DispatcherServlet        : "ERROR" dispatch for GET "/error", parameters={}
2020-03-24 13:21:03.995 TRACE 32622 --- [nio-8080-exec-4] s.w.s.m.m.a.RequestMappingHandlerMapping : 2 matching mappings: [{ /error}, { /error, produces [text/html]}]
2020-03-24 13:21:03.995 TRACE 32622 --- [nio-8080-exec-4] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped to org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController#error(HttpServletRequest)
2020-03-24 13:21:03.996 DEBUG 32622 --- [nio-8080-exec-4] o.s.w.s.m.m.a.HttpEntityMethodProcessor  : Using 'application/json', given [application/json, */*] and supported [application/json, application/*+json, application/json, application/*+json]
2020-03-24 13:21:03.997 DEBUG 32622 --- [nio-8080-exec-4] o.s.w.s.m.m.a.HttpEntityMethodProcessor  : Writing [{timestamp=Tue Mar 24 13:21:03 EDT 2020, status=404, error=Not Found, message=No message available,  (truncated)...]
2020-03-24 13:21:03.998 DEBUG 32622 --- [nio-8080-exec-4] o.s.web.servlet.DispatcherServlet        : Exiting from "ERROR" dispatch, status 404

当我尝试打开下面的URL时,我确实看到了我的JSON响应。
http://localhost:8080/v3/api-docs

z4bn682m

z4bn682m1#

我有这个相同的问题今天访问一个 swagger 的URL如下:

  • 您可以在此查看您的配置文件。

网页浏览器显示页面:

在我将路径/v3/api-docs添加到页面文本字段并单击浏览按钮后,文档显示。

查看springdoc-openapi configuration,我可以看到默认的swagger ui路径是/swagger-ui.html,因此当我访问以下URL时:

  • 您的位置:

它起作用了。

6vl6ewon

6vl6ewon2#

可能有这样的原因,当我们开始使用 Spring 安全,即使这样,我们也将无法访问swagger-ui,这里是我尝试,它开始工作。

@Override
protected void configure(HttpSecurity http) throws Exception {
    http.csrf().disable().sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and()
            .authorizeRequests().antMatchers("/user/register").permitAll().antMatchers("/swagger-ui/**").permitAll()
            .antMatchers("/v3/api-docs/**").permitAll().anyRequest().authenticated();
    http.apply(new JwtTokenConfigurer(tokenProvider));
}
ghhaqwfi

ghhaqwfi3#

是的,要引用的网址不正确。我试过新的网址,效果很好。
http://localhost:8080/swagger-ui/index.html?url=/v3/api-docs&validatorUrl=

9lowa7mx

9lowa7mx4#

如果您使用security或类似security的东西,您应该允许“../v3/api”文件。
http.authorizeRequests().antMatchers("/swagger/**","/v3/**").permitAll();

mi7gmzs6

mi7gmzs65#

如果你使用的是spring-security,你必须在安全配置中包含配置路径,我在安全配置中做的就是从websecurity中排除这些路径。

@Configuration
public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {
    @Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring().antMatchers("/swagger-ui/**", "/v3/api-docs/**");
    }
}
mznpcxlj

mznpcxlj6#

在我的例子中,我有一个过时的commons-lang3版本。一旦我将它更新到3.10版本,它就工作了。

<dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-lang3</artifactId>
        <version>3.10</version>
    </dependency>
58wvjzkj

58wvjzkj7#

如果您正在使用spring security,则在配置路径下添加security配置文件

http.authorizeRequests().antMatchers("/v3/api-docs/**","/swagger-ui/**").permitAll()
rjee0c15

rjee0c158#

用途

http.anyRequest().permitAll();

可作为临时解决方案

相关问题