Model class is not reflecting example and description in Swagger UI

59 Views Asked by At

I have spring boot application where we are using swagger for documentation using spring-fox. Issue is , in the swagger UI , model class is not showing the example and description , not sure what is missing .

after googling, it seems issue with some version mismatch or the media type which is being produced. Not sure though

I followed the solution mentioned here as well , but not getting what is missing ...

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

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.14</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>oe.test</groupId>
    <artifactId>swagger-ui</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>swagger-ui</name>
    <description>swagger project </description>
    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope> 
             <exclusions>
              <exclusion>
            <groupId>com.vaadin.external.google</groupId>
            <artifactId>android-json</artifactId>
            </exclusion>
          </exclusions>
        </dependency>
         <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.9.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>2.9.2</version>
</dependency>
<dependency>
  <groupId>io.springfox</groupId>
  <artifactId>springfox-schema</artifactId>
  <version>2.9.2</version>
</dependency>

<dependency>  
<groupId>org.springframework.boot</groupId>  
<artifactId>spring-boot-starter-security</artifactId>  
</dependency>

<dependency>
    <groupId>com.google.code.gson</groupId>
    <artifactId>gson</artifactId>
    </dependency>
    
     <dependency>
    <groupId>org.json</groupId>
    <artifactId>json</artifactId>
    <version>20230227</version>
</dependency>

<!-- https://mvnrepository.com/artifact/io.swagger.core.v3/swagger-annotations -->
<dependency>
    <groupId>io.swagger.core.v3</groupId>
    <artifactId>swagger-annotations</artifactId>
    <version>2.2.20</version>
</dependency>


    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

The model class

public class Product implements Serializable {

    @Schema(name = "Product ID", example = "1", required = true)
    private Long id;
    @Schema(name = "Product name", example = "Product 1", required = false)
    private String name;
    @Schema(name = "Product price", example = "$100.00", required = true)
    private String price;

    // constructor and getter/setters

    public Product(long id, String name, String price) {
        this.id = id;
        this.name = name;
        this.price = price;
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }
    
    public void setName(String name) {
        this.name = name;
    }

    public String getPrice() {
        return price;
    }

    public void setPrice(String price) {
        this.price = price;
    }
}

Controller class

@RestController
@Tag(name = "Cloud Controller API")
public class CloudController {

  @Operation(summary = "Get a product by id", description = "Returns a product as per the id")
        @ApiResponses(value = {
                @ApiResponse(code = 200, message = "Successfully retrieved"), @ApiResponse(code = 404, message = "Not found - The product was not found")
        })
        @GetMapping(value = "/products/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
      public ResponseEntity<Product> getProduct(@PathVariable("id") @Schema(name = "id", description = "Product id", example = "1") Long id) {
            //retrieval logic
            return ResponseEntity.ok(new Product(1, "Product 1", "$21.99"));
        }

Im swagger UI , the example and description is missing at all. Any help please...

enter image description here

enter image description here

enter image description here

please suggest

0

There are 0 best solutions below