我已将 Swagger 添加到我的 Spring Boot 2 应用程序中:
这是我的 Swagger 配置:
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket api() {
// @formatter:off
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build();
// @formatter:on
}
}
这是 Maven 依赖项:
<!-- Swagger2 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.8.0</version>
</dependency>
当我尝试调用例如 http://localhost:8080/api/actuator/auditevents 它失败并出现以下错误:
TypeError: Failed to execute 'fetch' on 'Window': Request with GET/HEAD method cannot have body.
我做错了什么以及如何解决?
原文由 alexanoid 发布,翻译遵循 CC BY-SA 4.0 许可协议
我遇到了这个问题。这是我解决它的方法:
我有这样的方法:
我得到了错误。我相信 swagger UI 将 Get 参数解释为 FromBody,因此它使用
curl -d
标志。我添加了 [FromQuery] 装饰器,问题得到解决:仅供参考,这也改变了该方法的 UI 体验。您将为参数对象的每个属性提供一个表单字段,而不是提供 json。