gpt4 book ai didi

Spring 应用 : Why is swagger-ui not able to do HTTP GET on swagger-resources/configuration/ui?

转载 作者:行者123 更新时间:2023-12-04 15:41:36 30 4
gpt4 key购买 nike

我一直在尝试使用我的 spring REST 应用程序设置 swagger-ui。
我正在使用 spring 4.2.6.RELEASE 和 swagger core & ui 2.5.0 。我没有使用 swagger annotations ,期望 swagger 选择 spring REST annotations。

我可以 Swagger 生成 api 文档,并且可以在 v2/api-docs 下查看它。

我能够点击 swagger-ui.html 页面,但它没有在页面上显示任何 api-docs 或 Controller 信息。在浏览器上启用调试器模式时,我发现尝试获取“swagger-resources/configuration/ui”时出错 - 返回 404(未找到)。

我已按照以下链接设置 swagger-ui http://www.baeldung.com/swagger-2-documentation-for-spring-rest-api

我最初设置了上面链接中指定的资源处理程序,但这也无济于事,并给了我相同的 404 错误。我尝试调整资源处理程序,所以看看它是否可以帮助 swagger-ui 在 swagger-resources/configuration/ui 上执行 GET。

为什么 swagger-ui 无法获取资源 swagger-resources/configuration/ui?

我已经设置了我的资源处理程序,如下所示。

Swagger配置

@Configuration
@EnableSwagger2
public class SwaggerConfiguration {

@Bean
public Docket api(){
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build();
//.apiInfo(apiInfo());
}
}

网页配置文件
@EnableWebMvc
@Configuration
@Import(SwaggerConfiguration.class)
@ComponentScan("com.bank.direct.services")

public class WebConfig extends WebMvcConfigurerAdapter {

@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> pConverters) {
pConverters.add(RestUtils.getJSONMessageConverter());
}

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

}

我确实为 webapp 设置了 SecurityConfig,但我将它保持在最低限度,以防万一它可能导致任何问题。
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {


@Autowired
private AuthenticationService _authenticationService;


@Autowired
public void globalUserDetails(AuthenticationManagerBuilder pAuth) throws Exception {

pAuth.userDetailsService(_authenticationService);
}


@Override
protected void configure(HttpSecurity pHttp) throws Exception {

// Enable HTTP caching
pHttp.headers().cacheControl().disable();

// Configure security
pHttp.httpBasic()

// -- Allow unauthenticated request (must be done before allowing only authenticated requests)
.and()
.authorizeRequests()
.antMatchers("/rest/application/information/").permitAll();

}

当应用程序启动时,我确实看到了一些资源映射

2016-08-31 11:24:55 INFO [localhost-startStop-1] RequestMappingHandlerMapping - Mapped "{[/v2/api-docs],methods=[GET],produces=[application/json || application/hal+json]}" onto public org.springframework.http.ResponseEntity springfox.documentation.swagger2.web.Swagger2Controller.getDocumentation(java.lang.String,javax.servlet.http.HttpServletRequest) 2016-08-31 11:24:55 INFO [localhost-startStop-1] RequestMappingHandlerMapping - Mapped "{[/swagger-resources/configuration/security]}" onto org.springframework.http.ResponseEntity springfox.documentation.swagger.web.ApiResourceController.securityConfiguration() 2016-08-31 11:24:55 INFO [localhost-startStop-1] RequestMappingHandlerMapping - Mapped "{[/swagger-resources]}" onto org.springframework.http.ResponseEntity> springfox.documentation.swagger.web.ApiResourceController.swaggerResources() 2016-08-31 11:24:55 INFO [localhost-startStop-1] RequestMappingHandlerMapping - Mapped "{[/swagger-resources/configuration/ui]}" onto org.springframework.http.ResponseEntity springfox.documentation.swagger.web.ApiResourceController.uiConfiguration()

最佳答案

假设您密切关注该指南( http://www.baeldung.com/swagger-2-documentation-for-spring-rest-api ),您最终会得到不同版本的 swagger 依赖项,这似乎导致了 404(至少对我而言......)

尝试更改 springfox-swagger-ui 依赖项的版本以匹配 springfox-swagger2。

我使用了以下内容(指南将 swagger-ui 设为 2.4.0):

<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>

关于 Spring 应用 : Why is swagger-ui not able to do HTTP GET on swagger-resources/configuration/ui?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39247501/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com