Home > Software design >  Why I am unable to customize Request Body in Swagger UI for an endpoint that I created using Spring
Why I am unable to customize Request Body in Swagger UI for an endpoint that I created using Spring

Time:08-22

I have created an endpoint which allows you to get the details of book by id.

@ResponseBody
@RequestMapping(value = "/books/{id}", method = RequestMethod.GET)
public BookResponse getDetails(
                    @PathVariable(name = "id", required = true) int id,
                    @PathVariable(name = "name", required = false) String name,
                    @RequestBody BookRequest request)
{
    return controllerBook.getBookDetails(request, id, name);
}

The RequestBody is

@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder(toBuilder = true)
@JsonInclude(JsonInclude.Include.ALWAYS)
@JsonIgnoreProperties(ignoreUnknown = true)
@ApiModel("BookRequest")
public class BookRequest {
    @NotNull(message = "Id of user created while registration")
    @ApiModelProperty(value = "Id of user created while registration")
    private int userId;
    @NotNull(message = "Id of book")
    @ApiModelProperty(value = "Id of book")
    private int bookId;
    @NotNull
    @ApiModelProperty(value = "Detail of book")
    private String detail1;
    @ApiModelProperty(value = "Detail of book")
    private String detail2;
    @ApiModelProperty(value = "Name of book")
    private String name;
    @ApiModelProperty(value = "Author of book")
    private String author;
    @ApiModelProperty(value = "Category of book")
    private String category;
    @ApiModelProperty(value = "Language of book")
    private String language;
}

I tried using @Api instead of @ApiModel and tried changing the keyword of arguments to example, name in @ApiModelProperty and also tried using arguments without keywords.

I am using springdoc openapi ui

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

Every time I run the application and open swagger UI I am unable to view these changes in the request Body and only default values can be seen. As you can see Request Body has not been customized but only takes default values

As you can see Request Body has not been customized but only takes default values

Am I missing something? Or Am I using @ApiModelProperty for what is not intended? If so what is the correct wrapper or annotation to use here? It would be of great help if anyone clarifies this.

Problem is resolved. Check answer posted by pan-leszeczek and the corresponding thread.

CodePudding user response:

You need to use "example" property of that annotation:

@ApiModelProperty(example = "example value of property")

As mentioned in the comment ApiModelProperty -> example would work fine in case you use springfox dependency But since you use springdoc-openapi-ui you should use

@Schema(... example = "example value")

in your Request/Response models

CodePudding user response:

The request model documentation should be in another section called "Schemas" of the swagger ui page, scroll down the swagger ui page if you define it correctly it should be down there.

Edit: or you can see it just by pressing the tab "Schema" to the right of "Example Value"

enter image description here

  • Related