Issue
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
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.
Solution
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
Answered By - pan-leszeczek
Answer Checked By - Terry (JavaFixing Volunteer)