Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Customise example value of request body and execute it on swagger-ui with springdoc-open-api

I have created rest webservice using springboot and added springdoc-open-api for ducumentation of webservice, Now i have 2 question

1-how to add custom test value into request which displying on swagger-ui document page?

2-how to execute the request on click of TRY IT OUT button on swagger-ui document page?

Please refer below code snippet for rest webservice:

@PostMapping(value="/result", consumes={ "application/json"},produces={ "application/json" } )
@Parameter(description  = "Student object need to calculate the score" ,name="InputObject", required = true )
public ResponseEntity<Result> displayResult(@Valid  @RequestBody Student request);

Public class Student{
  String name;
  String birthDate;
  String motherName;
  int rollNo;
  int seatNo;
}

Public class Result{
  int marks;
  String grade;
  double percentage;
}

I have tried to add value of request using @Schema(name = "name", example= "Rubeena", description = "Name of student"), is it right way to add the value in example request ?
Even after adding this schema when i click on TRY IT OUT button i dont get the outcome.

Is there any way to solve this problem?
like image 554
Rubeena Avatar asked Oct 23 '20 13:10

Rubeena


People also ask

How can I define multiple OpenAPI definitions in one spring boot project?

How can I define multiple OpenAPI definitions in one Spring Boot project? You can define your own groups of API based on the combination of: API paths and packages to scan. Each group should have a unique groupName .

What is Springdoc OpenAPI?

springdoc-openapi java library helps to automate the generation of API documentation using spring boot projects. springdoc-openapi works by examining an application at runtime to infer API semantics based on spring configurations, class structure and various annotations.

How does spring boot integrate with OpenAPI?

Below are the steps to configure the Spring boot application with the Open API documentation library. Adding the springdoc-openapi-ui maven library. Defining an OpenAPI spring configuration bean. Finally, use the open API annotations to document the APIs.


Video Answer


2 Answers

Using @Schema annotation, I can provide the value in example request

Public class Student{
@Schema(example= "XXX", description = "Name of student")
 String name;

@Schema(example= "10-10-2020", description = "Birth date of student") String birthDate; ...... }

like image 64
Rubeena Avatar answered Oct 21 '22 10:10

Rubeena


use @ApiModelProperty annotation in your dto class.

Example -

Public class Student{
   @ApiModelProperty(value = "name", name = "name", dataType = "String" example = "Rube")
  String name;
   @ApiModelProperty(value = "birthDate", name = "birthDate", dataType = "birthDate" example = "12/12/1995")
  String birthDate;
 ........................
}

//should work with following dependencies 
     <!-- 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>
like image 1
Ayush Vipul Avatar answered Oct 21 '22 12:10

Ayush Vipul