I’m trying to add some examples to the swagger documentation. I want to do this using the @ExampleObject annotation.
Here is my test controller.
@Operation(summary = "Create new Employee", responses = {
@ApiResponse(responseCode = "200", description = "OK", content = {
@Content(
mediaType = "application/json",
array = @ArraySchema(schema = @Schema(implementation = EmployeeCreateRequestDto.class)),
examples = {
@ExampleObject(name = "Example 1", summary = "Summary 1", description = "Some desc",
value = "[{"firstName":"Ram","lastName":"Gurram"},{"firstName":"Krishna","lastName":"N"}]")
}
)
})
})
@PostMapping
public ResponseEntity<List<EmployeeCreateRequestDto>> createEmployee(
@RequestBody(description = "Create new user", required = true,
content = @Content(mediaType = "application/json",
array = @ArraySchema(schema = @Schema(implementation = EmployeeCreateRequestDto.class)),
examples = {
@ExampleObject(name = "Example 1", summary = "Summary 1", description = "sample payload",
value = "[{"firstName":"Ram","lastName":"Gurram"},{"firstName":"Krishna","lastName":"N"}]")
}))
@org.springframework.web.bind.annotation.RequestBody List<EmployeeCreateRequestDto> dtos) {
return new ResponseEntity<>(dtos, HttpStatus.CREATED);
}
private static class EmployeeCreateRequestDto {
private String firstName;
private String lastName;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
I tried updating dependency versions, tried using @ExamleObjects in the @Parametrs annotation – nothing helps.
New contributor
Анастасия Иванова is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.