@ExampleObject can be used for @RequestBody and @ApiResponse to add examples for springdoc openapi : https://github.com/springdoc/springdoc-openapi/blob/master/springdoc-openapi-javadoc/src/test/java/test/org/springdoc/api/app90/HelloController.java
@RestController
public class HelloController {
/**
* Test 1.
*
* @param hello the hello
*/
@GetMapping("/test")
@ApiResponses(value = { @ApiResponse(description = "successful operation",content = { @Content(examples = @ExampleObject(name = "500", ref = "#/components/examples/http500Example"), mediaType = "application/json", schema = @Schema(implementation = User.class)), @Content(mediaType = "application/xml", schema = @Schema(implementation = User.class)) }) })
public void test1(String hello) {
}
/**
* Test 2.
*
* @param hello the hello
*/
@PostMapping("/test2")
@RequestBody(
description = "Details of the Item to be created",
required = true,
content = @Content(
schema = @Schema(implementation = User.class),
mediaType = MediaType.APPLICATION_JSON_VALUE,
examples = {
@ExampleObject(
name = "An example request with the minimum required fields to create.",
value = "min",
summary = "Minimal request"),
@ExampleObject(
name = "An example request with all fields provided with example values.",
value = "full",
summary = "Full request") }))
public void test2(String hello) {
}
}
if you want to add OpenApiCustomiser in order to load your examples from resources you will need to start with something like this:
https://github.com/springdoc/springdoc-openapi/blob/master/springdoc-openapi-javadoc/src/test/java/test/org/springdoc/api/app90/SpringDocTestApp.java
@Bean
public OpenApiCustomiser openApiCustomiser(Collection<Entry<String, Example>> examples) {
return openAPI -> {
examples.forEach(example -> {
openAPI.getComponents().addExamples(example.getKey(), example.getValue());
});
};
}
For header, cookie, query or path params you can use @Parameter: https://docs.swagger.io/swagger-core/v2.0.0-RC3/apidocs/io/swagger/v3/oas/annotations/Parameter.html
with ParameterIn enum: https://docs.swagger.io/swagger-core/v2.0.0/apidocs/io/swagger/v3/oas/annotations/enums/ParameterIn.html
e.g.
@Parameter(in = ParameterIn.PATH, name = "id", description="Id of the entity to be update. Cannot be empty.",
required=true, examples = {@ExampleObject(name = "id value example", value = "6317260b825729661f71eaec")})
Another way is to add @Schema(type = "string", example = "min") on your DTO. e.g. : https://www.dariawan.com/tutorials/spring/documenting-spring-boot-rest-api-springdoc-openapi-3/