Skip to content
Advertisement

OpenAPI @RequestBody variables of type Map are not populated

I’ve a simple POST API where the @RequestBody contains a variable of type HashMap.

The problem is that the variable “myMap” is never populated.

Swagger UI shows the entry field correctly, with corresponding default values. I also added the a static block in the Configuration file, otherwise Swagger UI doesn’t show the entry field of type Map.

What is needed so that the variable myMap is populated ?

Config class

static {
    SpringDocUtils.getConfig().removeRequestWrapperToIgnore(java.util.Map.class);
}

Controller class

@RequestMapping(value = "/process/start",method = RequestMethod.POST)
public void startProcess1(
        @RequestBody(description = "HashMap of strings", required = true) HashMap<String, String> myMap)  {
    Assert.notEmpty(myMap, "map may not be empty");
}

Swagger UI

enter image description here

pom.xml

    <dependency>
        <groupId>org.springdoc</groupId>
        <artifactId>springdoc-openapi-ui</artifactId>
        <version>1.5.12</version>
    </dependency>

Advertisement

Answer

I found out that I used the wrong annotation. @RequestBody is a class which resides in two packages :

  1. org.springframework.web.bind.annotation
  2. io.swagger.v3.oas.annotations.parameters

I used the annotation from package io.swagger.v3.oas.annotations.parameters which is wrong. The correct annotation is located in package org.springframework.web.bind.annotation

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement