spring-bootswaggerswagger-ui

spring boot swagger exampleObject static resource externalValue base path


I'm using org.springdoc:springdoc-openapi-starter-webmvc-ui:2.8.3 to add swagger documentation to my project. I wanted to add an example json response but this is a big json file so I'm just getting it from the resources folder.

Here's what the controller endpoint looks like

@Operation(summary = "summary")
@ApiResponses(value = {
        @ApiResponse(responseCode = "200",
                description = "Response",
                content = @Content(mediaType = "application/json",
                        examples = {
                        @ExampleObject(name="Resource response", externalValue = "static/Response.json"),
                        }

                ))})
@PostMapping
public ResponseEntity<Object> myapi() {
        return new ResponseEntity<>(response, HttpStatus.OK);
}

As you can see I'm getting the sample response json using externalValue = "static/Response.json" but when I load the swagger page it will load it from v3/static... which seems to be the default used by springdoc/swagger.

Picture showing swagger loading from /v3/static

I guess I'm just nitpicking at this point but is it possible to change this base url or even remove /v3/?


Solution

  • Yes, you are right v3 is default path for api docs. see https://springdoc.org/properties.html

    You can update it in application.yml, if you want.

    I assume that Response.json is located in src/main/resources/static/. Than it seems that your external path is still not correct.

    Spring Boot automatically serves files from src/main/resources/static/ under /. see: https://spring.io/blog/2013/12/19/serving-static-web-content-with-spring-boot

    You can update your external path to

    externalValue = "static/Response.json"
    

    or for better code organization - better will be to move Response.json to src/main/resources/static/api-docs/Response.json and update external path to

    externalValue = "/api-docs/Response.json"