Skip to main content
 首页 » 编程设计

spring-boot之Swagger 类型错误 : Failed to execute 'fetch' on 'Window' : Request with GET/HEAD method cannot have body

2024年12月31日17telwanggs

我已将 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. 



我做错了什么以及如何解决?

请您参考如下方法:

错误消息实际上说明了问题所在。您使用 curl 发布数据在尝试使用 GET 时使用 -d 选项。

如果你使用 -d 选项 curl 会做 POST .
如果你使用 -X GET 选项 curl 会做 GET .

HTTP GET 方法用于请求指定资源的表示。使用 GET 的请求应该只检索数据,因此不能有正文。

更多信息 GET vs POST