如何自定义请求体的示例值,并在使用springdoc-open-api时在swagger-ui上执行它。

huangapple go评论109阅读模式
英文:

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

问题

我已经使用Spring Boot创建了REST Web服务,并添加了springdoc-open-api来对Web服务进行文档化。现在我有两个问题:

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

2- 如何在swagger-ui文档页面上点击“尝试”按钮后执行请求?

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

  1. @PostMapping(value="/result", consumes={ "application/json"}, produces={ "application/json" } )
  2. @Parameter(description = "需要计算分数的学生对象" ,name="InputObject", required = true )
  3. public ResponseEntity<Result> displayResult(@Valid @RequestBody Student request);
  4. public class Student{
  5. String name;
  6. String birthDate;
  7. String motherName;
  8. int rollNo;
  9. int seatNo;
  10. }
  11. public class Result{
  12. int marks;
  13. String grade;
  14. double percentage;
  15. }

我尝试使用@Schema(name = "name", example= "Rubeena", description = "学生的姓名")来添加请求的值,这样做的方式对吗?即使在添加了此模式后,当我点击“尝试”按钮时,我也无法获得结果。

有没有解决这个问题的方法?

英文:

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:

  1. @PostMapping(value=&quot;/result&quot;, consumes={ &quot;application/json&quot;},produces={ &quot;application/json&quot; } )
  2. @Parameter(description = &quot;Student object need to calculate the score&quot; ,name=&quot;InputObject&quot;, required = true )
  3. public ResponseEntity&lt;Result&gt; displayResult(@Valid @RequestBody Student request);
  4. Public class Student{
  5. String name;
  6. String birthDate;
  7. String motherName;
  8. int rollNo;
  9. int seatNo;
  10. }
  11. Public class Result{
  12. int marks;
  13. String grade;
  14. double percentage;
  15. }
  16. I have tried to add value of request using @Schema(name = &quot;name&quot;, example= &quot;Rubeena&quot;, description = &quot;Name of student&quot;), is it right way to add the value in example request ?
  17. Even after adding this schema when i click on TRY IT OUT button i dont get the outcome.
  18. Is there any way to solve this problem?

答案1

得分: 8

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

  1. public class Student {
  2. @Schema(example = "XXX", description = "学生姓名")
  3. String name;
  4. @Schema(example = "10-10-2020", description = "学生出生日期")
  5. String birthDate;
  6. // ...
  7. }
英文:

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

  1. Public class Student{
  2. @Schema(example= &quot;XXX&quot;, description = &quot;Name of student&quot;)
  3. String name;

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

答案2

得分: 1

使用 @ApiModelProperty 注解在您的 DTO 类中。

示例 -

  1. Public class Student{
  2. @ApiModelProperty(value = "name", name = "name", dataType = "String" example = "Rube")
  3. String name;
  4. @ApiModelProperty(value = "birthDate", name = "birthDate", dataType = "birthDate" example = "12/12/1995")
  5. String birthDate;
  6. // ........................
  7. }

// 应该与以下依赖项一起工作



io.springfox
springfox-swagger2
2.9.2



io.springfox
springfox-swagger-ui
2.9.2

```

英文:

use @ApiModelProperty annotation in your dto class.

Example -

  1. Public class Student{
  2. @ApiModelProperty(value = &quot;name&quot;, name = &quot;name&quot;, dataType = &quot;String&quot; example = &quot;Rube&quot;)
  3. String name;
  4. @ApiModelProperty(value = &quot;birthDate&quot;, name = &quot;birthDate&quot;, dataType = &quot;birthDate&quot; example = &quot;12/12/1995&quot;)
  5. String birthDate;
  6. ........................
  7. }
  8. //should work with following dependencies
  9. &lt;!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 --&gt;
  10. &lt;dependency&gt;
  11. &lt;groupId&gt;io.springfox&lt;/groupId&gt;
  12. &lt;artifactId&gt;springfox-swagger2&lt;/artifactId&gt;
  13. &lt;version&gt;2.9.2&lt;/version&gt;
  14. &lt;/dependency&gt;
  15. &lt;!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui --&gt;
  16. &lt;dependency&gt;
  17. &lt;groupId&gt;io.springfox&lt;/groupId&gt;
  18. &lt;artifactId&gt;springfox-swagger-ui&lt;/artifactId&gt;
  19. &lt;version&gt;2.9.2&lt;/version&gt;
  20. &lt;/dependency&gt;

huangapple
  • 本文由 发表于 2020年10月23日 21:37:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/64501089.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定