6

I'm documention one of my api with multiple examples like this:

@Operation(summary = "Create new")
@PostMapping("")
public ResponseEntity<Object> createOne(
        @Parameter(description = "MyDto")
        @io.swagger.v3.oas.annotations.parameters.RequestBody(
                content = @Content(examples = {
                        @ExampleObject(name = "one", value = EXAMPLE_ONE),
                        @ExampleObject(name = "two", value = EXAMPLE_TWO),
                        @ExampleObject(name = "three", value = EXAMPLE_THREE)}
                ))
        @RequestBody MyDTO body
) {
    ...
}

This works fine, though EXAMPLE_ONE is a string value. This is pretty unclear as you can see from the example below

    private static final String EXAMPLE_ONE = "{\"glossary\":{\"title\":\"example glossary\",\"GlossDiv\":{\"title\":\"S\",\"GlossList\":{\"GlossEntry\":{\"ID\":\"SGML\",\"SortAs\":\"SGML\",\"GlossTerm\":\"Standard Generalized Markup Language\",\"Acronym\":\"SGML\",\"Abbrev\":\"ISO 8879:1986\",\"GlossDef\":{\"para\":\"A meta-markup language, used to create markup languages such as DocBook.\",\"GlossSeeAlso\":[\"GML\",\"XML\"]},\"GlossSee\":\"markup\"}}}}}";

I looking for a better way to provide the example. A json file would be nice, but I couldn't find anything about that.

Robin Dijkhof
  • 18,665
  • 11
  • 65
  • 116

2 Answers2

0

You can use externalValue instead of value. See here

0

Use java text-block instead of normal quoted string e.g. putting triple double (""") see https://www.baeldung.com/java-text-blocks