I'm trying to create " /> I'm trying to create " /> I'm trying to create "/>

In SpringBoot, Springfox 3.0.0 ApiResponse Example is not shown when rest endpoint returns Object

57 views Asked by At

I have a several Rest endpoints, and some of their return type is just "Object" instead of, for example ResponseEntity<Customer>

I'm trying to create swagger documentation using SpringFox 3.0.0 in SpringBoot 2.7.15 my pom.xml

        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-boot-starter</artifactId>
            <version>3.0.0</version>
        </dependency>

        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>3.0.0</version>
        </dependency>

And I created very simple endpoint, deliberatively I set return type to Object, because in this project most of the endpoinds are returning plain object. Mostly it creates a JSON object inside the method and returns it.

    @Operation(
            summary = "PING PONG",
            description = "Returns a Pong"
    )
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "Ping Pong", content = @Content(mediaType = "application/json", examples = { @ExampleObject(name = "example1", description = "test description", value = "{\"name\": \"pong\", \"description\":\"test\"}") } ))
    })
    @RequestMapping(value = KD_KUNDEN + "/ping", method = RequestMethod.GET, produces = "application/json")
    public Object makePing() {
        Pong newPong = new Pong();
        newPong.setName("Pong");
        newPong.setDescription("Pong description");
        return ResponseEntity.ok(newPong);
    }

And unfortunately, I can't see the example or description in response part no example value

If I change the return type to ResponseEntity and in Pong.java if I give examples with @Schema annoation it is working well

public class Pong {
    @Schema(name = "name", example = "jack")
    String name;
    @Schema(name = "description", example = "example description")
    String description;
}

and change the return type

public ResponseEntity<Pong> makePing() {
...
return ResponseEntity.ok(newPong);

with example value

Problem is, I have lots of endpoints and I don't want to refactor every endpoint right now. Is there any solution that I can give custom examples in Response part ?

edit-1:

According to this link:

<Spring Boot / Springfox> Swagger UI not showing example value and model

I added below line in my application.properties

springfox.documentation.swagger.use-model-v3=false

and use old namespaces from io.swagger.annotations instead of io.swagger.v3.oas.annotations but still no luck, I can't see the examples.

0

There are 0 answers