【发布时间】:2019-07-06 13:51:51
【问题描述】:
这是我的第一篇文章,如果我做错了什么,请随时留下一些反馈:)
我正在使用 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();
}
}
当我在调试模式中启动应用程序时,我输入了以前的 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:找不到完整路径的资源: http://localhost:8092/test/v2/api-docs"。
我尝试通过添加属性来修改默认 URL:springfox.documentation.swagger.v2.path=v2/api-docs-test
仍然是 404。
我从头开始尝试了一个新的空项目,它工作得很好。我猜我的项目有问题。
我确定使用的 URL。
你知道我该如何调试这个问题吗?我可以从 Swagger 看到一些创建的源代码吗? 如果我可以放一些日志以了解问题出在哪里?
【问题讨论】:
-
你好@Pierrot 你在使用 Spring Security,如果是,你必须添加一些配置
-
@Pierrot
/test路径定义在哪里? -
嗨 Madhu:在 JAXRS 配置中:
@Component @ApplicationPath("/test/") public class SituationServicesJaxrsApplication extends ServiceApplication { } -
刚刚发生了一些非常奇怪的事情:我将其删除并再次粘贴,我已经能够访问 HTML :o 但它没有信息......:“规范中没有定义操作!”总比没有好,但仍然不明白刚刚发生了什么以及为什么我无法获得 API 的信息:s Docket Bean 仍然非常开放:
.apis(RequestHandlerSelectors.any()) .paths(PathSelectors.any()) -
如果未指定“/test/”,则它不起作用... @ApplicationPath("/")
标签: java spring-boot swagger-ui swagger-2.0 springfox