【问题标题】:Springfox UI docs access throws up HTTP 405 errorSpringfox UI 文档访问引发 HTTP 405 错误
【发布时间】:2018-05-14 02:51:56
【问题描述】:

我正在开发基于 SpringBoot 的 REST Api 并使用 Springfox 生成 swagger 文档。

这是 Swagger 的配置:

@Configuration
@EnableSwagger2
public class SwaggerConfiguration {
    @Autowired
    private AppConfiguration appConfiguration;

    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("app-api")
                .apiInfo(apiInfo())
                .select()
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("App API")
                .termsOfServiceUrl(appConfiguration.getApiTosUrl())
                .version("1.0").build();
    }
}

这是我正在使用的网络安全配置:

@Override
    protected void configure(HttpSecurity http) throws Exception {
        http.cors().and().csrf().disable().authorizeRequests()
                .antMatchers("/no-auth/**").permitAll()
                .antMatchers("/v2/api-docs", "/swagger-resources/configuration/ui", "/swagger-resources", "/swagger-resources/configuration/security", "/swagger-ui.html", "/webjars/**").permitAll()
                .anyRequest().authenticated()
                .and()
                .addFilter(new JWTAuthenticationFilter(securityConfiguration, authenticationManager()))
                // this disables session creation on Spring Security
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
    }

    @Override
    public void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailsService).passwordEncoder(bCryptPasswordEncoder);
    }

    @Bean
    CorsConfigurationSource corsConfigurationSource() {
        final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", new CorsConfiguration().applyPermitDefaultValues());
        return source;
    }

访问网址:http://localhost:8080/swagger-ui.html 抛出 HTTP 错误 405: GET request not supported。

虽然我可以访问网址:http://localhost:8080/v2/api-docs?group=app-api

我已在网上搜索以找到解决方案,但我不明白为什么会发生这种情况。

您可以指导我如何解决此问题。

编辑: 我注意到 swagger-ui.htm 根本没有被映射! 我正在使用 springfox 2.8.0

2018-05-14 12:17:47.019 INFO 1012 --- [restartedMain] s.w.s.m.m.a.RequestMappingHandlerMapping : 映射 “{[/swagger-resources]}”公开 org.springframework.http.ResponseEntity> springfox.documentation.swagger.web.ApiResourceController.swaggerResources() 2018-05-14 12:17:47.020 信息 1012 --- [restartedMain] s.w.s.m.m.a.RequestMappingHandlerMapping : 映射 “{[/swagger-resources/configuration/ui]}”公开 org.springframework.http.ResponseEntity springfox.documentation.swagger.web.ApiResourceController.uiConfiguration() 2018-05-14 12:17:47.021 信息 1012 --- [restartedMain] s.w.s.m.m.a.RequestMappingHandlerMapping : 映射 “{[/swagger-resources/configuration/security]}”公开 org.springframework.http.ResponseEntity springfox.documentation.swagger.web.ApiResourceController.securityConfiguration() 2018-05-14 12:17:47.026 信息 1012 --- [restartedMain] s.w.s.m.m.a.RequestMappingHandlerMapping :将“{[/error]}”映射到 上市 org.springframework.http.ResponseEntity> org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.error(javax.servlet.http.HttpServletRequest)

【问题讨论】:

标签: spring-boot springfox


【解决方案1】:

当我有一个请求映射/**时,我也有同样的情况。 405的原因是Swagger请求由另一个requestmapping处理。您可以在 DispatcherServlet 中将其更改为 swagger URL。我在https://github.com/pmoerenhout/springfox-boot-bug 做了一个最小的例子。

您可以使用此 Spring 映射来排除 Swagger UI 模式:

@RequestMapping(value = { "/", "/{part:^(?!swagger-ui$).*}/**" })

【讨论】:

    【解决方案2】:

    This comment 节省了我很多时间。 简而言之 - 我发现我的项目中有人像这样向控制器添加了映射:

    @RestController("/api/test")
    

    当然应该是这样的:

    @RestController
    @RequestMapping("/api/test")
    

    由于上述原因,我在尝试查看 swagger-ui 时收到了 405 个响应。

    @RestConroller 的文档更准确地解释了这个问题:

    该值可能表示对逻辑组件名称的建议,在自动检测到组件的情况下将其转换为 Spring bean。 回报: 建议的组件名称,如果有的话(否则为空字符串) 自从: 4.0.1

    【讨论】:

      猜你喜欢
      • 2021-03-19
      • 1970-01-01
      • 2017-05-01
      • 1970-01-01
      • 2012-04-10
      • 2017-10-30
      • 2011-04-10
      • 1970-01-01
      • 2014-03-01
      相关资源
      最近更新 更多