How to add example, schema and media type in java model using swagger annotation

77 views Asked by At

I have to add the example, schema and media type (Application/JSON) in the API documentation using swagger annotation. I'm able to display the example and model in the swagger UI. however, in the example, values are not set which I set in the Model class. The field values are coming empty. Not sure how to add media type as well

Model class

public class AddTennatResponseData {

    @Schema(name = "Unique ID of tenant", example = "abc123xYz")
    @Expose
    Object id;

    @Schema(name = "Tennat URL of the customer", example = "abc.xyz.oneenterprise.com")
    @Expose
    Object tenantURL;

    @Schema(name = "Name of the region", example = "us-east-1")
    @Expose
    Object region;

    @Schema(name = "Timestamp when Tenant created", example = "2024-01-03T14:52:34.700497084Z")
    @Expose
    OffsetDateTime timestamp;

    public AddTennatResponseData(Object id, Object tenantURL,
            Object region, OffsetDateTime timestamp) {

        this.id = id;
        this.tenantURL = tenantURL;
    
        this.region = region;
        
        this.timestamp = timestamp;
    }

    public Object getId() {
        return id;
    }

    public Object getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(OffsetDateTime timestamp) {
        this.timestamp = timestamp;
    }

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

    public Object getTenantURL() {
        return tenantURL;
    }

    public void setTenantURL(Object tenantURL) {
        this.tenantURL = tenantURL;
    }

    public Object getRegion() {
        return region;
    }

    public void setRegion(Object region) {
        this.region = region;
    }

}

Controller class

public class WebController {

    @ApiOperation(value = "Adds a specific Tenant in cluster")
    

    @ApiResponses(value = { @ApiResponse(code = 200, message = "Successfully Added" ),
            @ApiResponse(code = 500, message = "API Validation failed"),
            @ApiResponse(code = 404, message = "Specific Resource Not found")
    
    })

    @GetMapping("/tenant/add")
    @ResponseBody
    public ResponseEntity<AddTennatResponseData> addTenant(@RequestParam(value = "Cluster name", required = true) String clusterName,
            @RequestParam(value = "Tenant URL", required = true) String tenantURL,
            @RequestParam(value = "Region", required = true) String region,
            @RequestParam(value = "Logical cluster name", required = true) String logicalClusterName) throws Exception {

    AddTennatResponseData res = new AddTennatResponseData(generatedString, tenantURL, region, OffsetDateTime.now());

  //// business logic 

return ResponseEntity.ok(res);

}

POM.xml:

<dependency>
    <groupId>io.swagger.core.v3</groupId>
    <artifactId>swagger-annotations</artifactId>
    <version>2.2.20</version>
</dependency>
<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>

the issue is @Schema used, but still the field values are not coming in the swagger UI.

Example value is coming like this (empty curly braces)

{
  "id": {},
  "region": {},
  "tenantURL": {},
  "timestamp": {}
}

Not able to understand what is missing in annotation. I just need proper example with fields value set in Model class and Media type.

I have referred below doc:

0

There are 0 answers