Skip to content

Make it possible to mark parameters with @RequestParam annotation to be sent in form instead of query. #2826

Closed
@kkochanski

Description

@kkochanski

I'm using.

  • Spring Boot v3.4.0
  • Springdoc OpenAPI v2.7.0

The @RequestParam annotation is tricky. Spring Boot will scan query and form to pass data to annotated variable.

@PostMapping(path = "/process", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
@ResponseStatus(value = HttpStatus.OK)
public String process(
    @Boolean @RequestParam(name = "is_dummy", defaultValue = "false") String isDummy,
    @Min(10) @RequestParam(name = "age") int age
) {
    return "";
}

For this definition, doing a cURL or request from generated API Docs will not work.

cURL

curl -X 'POST' \
  'http://localhost:8080/process?is_dummy=false&age=11' \
  -H 'accept: application/json' \
  -d ''

Few issues here.

  1. Spring Boot will return org.springframework.web.HttpMediaTypeNotSupportedException: Content-Type is not supported exception.
  2. We got a POST so technically speaking parameters should be send in form instead of query.
  3. In endpoint definition there's a mention that it consumes Form Url Encoded, but still Swagger do not setup a content-type.

Instead, cURL should like below.

curl -X 'POST' \
  'http://localhost:8080/process' \
  -H 'accept: application/json' \
  -H 'Content-Type: application/x-www-form-urlencoded' \
  -d 'age=11&is_dummy=false'

It's possible to fix given code by removing consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE. So we got.

@PostMapping(path = "/process", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
@ResponseStatus(value = HttpStatus.OK)
public String process(
    @Boolean @RequestParam(name = "is_dummy", defaultValue = "false") String isDummy,
    @Min(10) @RequestParam(name = "age") int age
) {
    return "";
}

Then cURL works. Although I still we'd love to have a possibility to tell users to send a data in form instead of query.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions