gpt4 book ai didi

java - 尝试访问 swagger-ui 或 api/api-docs 但收到 404 错误

转载 作者:太空宇宙 更新时间:2023-11-04 09:32:28 25 4
gpt4 key购买 nike

这是我的第一篇文章,所以请随时留下一些反馈或者如果我做错了什么:)

我正在使用 spring-boot 和 Resteasy :

<!-- Spring Boot -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.1.0.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.paypal.springboot</groupId>
<artifactId>resteasy-spring-boot-starter</artifactId>
<version>2.3.4-RELEASE</version>
</dependency>
<小时/>

我正在尝试使用 Swagger 来查看我的端点,因此我添加了此依赖项:

<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>

此依赖项已加载到我的存储库中,一切看起来都很好。

<小时/>

我添加这个类是为了制作最简单的配置类:

@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build();
}
}

当我在 Debug模式中启动应用程序时,我会输入之前的 Bean。一切看起来都很好。

<小时/>

当我启动 Spring 应用程序时:

@SpringBootApplication
@EnableAutoConfiguration(exclude={MongoAutoConfiguration.class,RabbitAutoConfiguration.class})
public class SituationServicesApplication implements CommandLineRunner {
public static void main(String[] args) {
SpringApplication.run(SituationServicesApplication.class, args);
}
@Override
public void run(String... args) throws Exception {
}
}

一切看起来都很好:

2019-07-06 15:43:27.222  INFO 6336 --- [           main] pertySourcedRequestMappingHandlerMapping : Mapped URL path [v2/api-docs] onto method [public org.springframework.http.ResponseEntity<springfox.documentation.spring.web.json.Json> springfox.documentation.swagger2.web.Swagger2Controller.getDocumentation(java.lang.String,javax.servlet.http.HttpServletRequest)]
<小时/>

但是当我尝试访问 swagger-ui.html 时:

http://localhost:8092/test/v2/api-docs

http://localhost:8092/test/swagger-ui.html

我遇到了 404 错误:

"RESTEASY003210: Could not find resource for full path: http://localhost:8092/test/v2/api-docs".

我尝试通过添加属性来修改默认 URL:springfox.documentation.swagger.v2.path=v2/api-docs-test

仍然是 404。

我从头开始尝试了一个新的空项目,效果很好。我猜我的项目出了问题。

我确定所使用的 URL。

你知道我该如何调试这个问题吗?我可以看到 Swagger 创建的一些源代码吗?我是否可以在其上记录一些日志以了解问题来自何处?

最佳答案

如果您使用 Spring Security,那么您必须添加一些配置,如下所示。

Complete Working Example

SecurityConfig.java

package com.swagger.demo.security;


import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;


@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter implements WebMvcConfigurer{

//Swagger Resources
@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/");
}

//If you are using Spring Security Add Below Configuration

@Override
public void configure(WebSecurity web) throws Exception {
web
.ignoring()
.antMatchers("/v2/api-docs", "/configuration/**", "/swagger*/**", "/webjars/**")
.antMatchers(HttpMethod.OPTIONS,"/**");
}

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

http
.csrf().disable()
.authorizeRequests()
.antMatchers("/**", "/v2/api-docs", "/configuration/**", "/swagger*/**", "/webjars/**")
.permitAll()
.anyRequest().authenticated();
}

}

SwaggerConfig.java

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket apiDocket() {

Docket docket = new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.basePackage("com.swagger.demo"))
.paths(PathSelectors.any())
.build();

return docket;

}
}
<小时/>

pom.xml

        <dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>

<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-core</artifactId>
<version>2.9.2</version>
</dependency>

<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>

关于java - 尝试访问 swagger-ui 或 api/api-docs 但收到 404 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56914725/

25 4 0
文章推荐: java - TextView 无法通过 ontouchListener 选择
文章推荐: html - Firefox 显示
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com