Search code examples
springspring-bootswaggerspring-restcontrollerspringfox

how to describe the parameters of a hashmap in swagger v3?


I need to customize the request body in swagger, because in my controller I get a HashMap<String, String> and in the documentation it appears as

enter image description here

I would like to inform which properties I want to receive in the hashmap and I don't know how to annotate.

My Controller

@PostMapping("/add")
@ResponseStatus(HttpStatus.CREATED)
public Atendimento create(@RequestBody HashMap<String, String> param) throws Exception {
    return service.create(param);
}

Solution

  • Maybe the following will help you, but still the same question stands why you are not using a model (DTO) class directly instead.

    Here is an example using Kotlin, which can help you:

    @RestController
    class SampleController {
    
        data class RequestBodyMetaData(
            val key1: String,
            val key2: String,
            val key3: String,
        )
    
        @PostMapping("/sample")
        fun sample(
            @io.swagger.v3.oas.annotations.parameters.RequestBody(
                content = [
                    Content(
                        schema = Schema(implementation = RequestBodyMetaData::class),
                    )
                ]
            )
            @org.springframework.web.bind.annotation.RequestBody
            sample: Map<String, String>,
        ) {
        }
    }
    

    Screen capture from the sample result