Springdoc-openapi: Missing "consumes" on controller method results in Content-Type set to */*

Created on 17 Dec 2019  路  7Comments  路  Source: springdoc/springdoc-openapi

If for example a @PostMapping annotation on a RestController does not have the "consumes" variable set to anything, the org.springdoc.core.MethodAttributes.fillMethods method uses org.springframework.http.MediaType.ALL_VALUE as the value, which is */*.

This results in Spring throwing an exception saying java.lang.IllegalArgumentException: Content-Type cannot contain wildcard type '*' in org.springframework.http.HttpHeaders.setContentType

Could it be possible that instead of using */*, you would instead fill the array with a set of MediaTypes so that one could choose in the Swagger-ui, or default to org.springframework.http.MediaType.APPLICATION_JSON_VALUE if the class is a RestController?

Most helpful comment

I agree this defaulting to */*is not ideal. In my case the AWS Gateway import tool doesn't like that content type. At the very least we should be able to override this default value in some fashion.

All 7 comments

I agree this defaulting to */*is not ideal. In my case the AWS Gateway import tool doesn't like that content type. At the very least we should be able to override this default value in some fashion.

Hi @AlfSimen,

Can you add the example code on a HelloController, to reproduce: java.lang.IllegalArgumentException: Content-Type cannot contain wildcard type '*' ?

You can set the mediaType using swagger-annotations.
For example:

  • @RequestBody(content = @Content (mediaType = "application/json"))
  • @ApiResponse(description = "Successful Operation", responseCode = "200", content = @Content(mediaType = "application/json"))

Here's a small example

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(path = "api")
public class HelloController {

    @PostMapping(path = "hello")
    public String hello(@RequestBody SomeDto body) {
        return "Hello " + body.getMessage();
    }
}

We have added produces = MediaType.APPLICATION_JSON_VALUE on the @PostMapping-annotation so that swagger-ui works, but it would be a lot nicer if swagger-ui let us choose what we want to set Content-Type to instead of only giving us a default that does not work and which cannot be changed in the ui.

why you use *Mapping to config contentType in calculateConsumesProduces
if use ResponseBody(Spring) to config contentType better
when wo use ResponseBody(Spring) the contentType wo be json default?

duanhairuo
why you use *Mapping to config contentType in calculateConsumesProduces
if use ResponseBody(Spring) to config contentType better
when wo use ResponseBody(Spring) the contentType wo be json default?

That made absolutely no sense whatsoever.

Hi @AlfSimen,

The default value if consumes is missing will be set to org.springframework.http.MediaType.APPLICATION_JSON_VALUE, in the next release.
If its not enough, we can use an array with a set of default MediaTypes.

@springdoc awesome, thank you!

Was this page helpful?
0 / 5 - 0 ratings