To solve the "Swagger TypeError: Failed to execute 'fetch' on 'Window': Request with GET/HEAD method cannot have body in Spring Boot" error, you need to ensure that your Swagger configuration is correct and that you're not sending a body with a GET or HEAD request.
Here's how I resolved the issue:
I initially had a method defined like this:
[HttpGet]
public IEnumerable Get(MyObject dto)
{
// Code implementation...
}
And I encountered an error. I suspected that Swagger UI was interpreting the parameters of the HTTP GET request as coming from the request body (FromBody), causing it to use the curl -d flag.
To fix this, I added the [FromQuery] decorator to the parameter, indicating that it should be taken from the query string:
[HttpGet]
public IEnumerable Get([FromQuery]MyObject dto)
{
// Code implementation...
}
After making this change, the problem was resolved, and Swagger UI correctly interpreted the parameters, resolving the issue with the curl -d flag.
To avoid encountering this error, ensure that you annotate parameters in your controller with @RequestParam
, like so:
@GetMapping("/get")
public Response getData(@RequestParam String param) {
// Code implementation...
}
By using @RequestParam
, you explicitly specify that the parameter should be obtained from the request parameters, helping to prevent any issues with parameter interpretation.
This error occurs due to an incorrect argument type. Simply change [FromBody]
to [FromQuery]
.