首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何使用springdoc-open-api定制请求体的示例值并在swagger-ui上执行

如何使用springdoc-open-api定制请求体的示例值并在swagger-ui上执行
EN

Stack Overflow用户
提问于 2020-10-23 21:37:11
回答 2查看 5.2K关注 0票数 4

我已经使用springboot创建了rest webservice,并添加了springdoc-open-api作为webservice的文档,现在我有两个问题。

1-如何将自定义测试值添加到swagger-ui文档页面上显示的请求中?

2-如何在swagger-ui文档页面上单击TRY IT OUT按钮执行请求?

请参考以下rest rest服务的代码片段:

代码语言:javascript
运行
复制
@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?
EN

回答 2

Stack Overflow用户

发布于 2020-11-26 18:56:22

使用@Schema注释,我可以在示例请求中提供值

代码语言:javascript
运行
复制
Public class Student{
@Schema(example= "XXX", description = "Name of student")
 String name;

@Schema(example= "10-10-2020",description =“学生出生日期”)字符串birthDate;.}

票数 4
EN

Stack Overflow用户

发布于 2020-10-23 23:24:55

在dto类中使用@ApiModelProperty注释。

示例:

代码语言:javascript
运行
复制
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>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64501089

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档