当前位置: 首页 > 知识库问答 >
问题:

如何在Swagger(OpenApi)中添加JSON请求和响应示例?

曾承弼
2023-03-14

我的Symfony 4应用程序中有一个APIendpoint,我想用NelmioApiDocBundle和Swagger记录它。endpoint将JSON作为请求数据,并返回一些自定义JSON作为响应。如何使用注释将其示例添加到文档中?我在文档页面上看不到任何示例,只有描述。

/**
 * @Route("/order/import", methods={"POST"}, name="order_import")
 * @OA\RequestBody (
 *     request="order",
 *     description="Order data in JSON format",
 *     @OA\Schema(
 *        type="object",
 *        example={"hello": "world"}
 *     )
 * )
 * @OA\Response(
 *     response=200,
 *     description="Returns the JSON data after import",
 *     @OA\Schema(
 *        type="object",
 *        example={"foo": "bar"}
 *     )
 * )
 * @OA\Tag(name="import")

共有1个答案

邬宜然
2023-03-14

对于NelmioApiDocBundle v4,您可以这样做

use OpenApi\Annotations as OA;

/**
 * @OA\Parameter(
 *     name="body",
 *     in="path",
 *     required=true,
 *     @OA\JsonContent(
 *        type="object",
 *        @OA\Property(property="property1", type="number"),
 *        @OA\Property(property="property2", type="number"),
 *     ),
 * )
 *
 * @OA\Response(
 *     response=200,
 *     description="",
 *     @OA\JsonContent(
 *        type="object",
 *        @OA\Property(property="property1", type="number"),
 *        @OA\Property(property="property2", type="number"),
 *     )
 * )
 */

对于v3

use Swagger\Annotations as SWG;

/**
 * @SWG\Parameter(
 *     name="body",
 *     in="body",
 *     required=true,
 *     @SWG\Schema(
 *         @SWG\Property(property="test1", type="string"),
 *         @SWG\Property(property="test2", type="string"),
 *     ),
 * )
 *
 * @SWG\Response(
 *     description="",
 *     response=200,
 *     @SWG\Schema(
 *         @SWG\Property(property="test1", type="string"),
 *         @SWG\Property(property="test2", type="string"),
 *     ),
 * )
 */

但如果您有DTO或实体,最好通过@Model注释来实现,如文档中所述。

 类似资料:
  • 我正试图将示例响应值添加到我的springdoc-openapi swagger文档中。 比如用“马克吐温”代替“字符串”等。 我尝试使用这个解决方案-springdoc openapi:如何添加POST请求示例? 我已经在课堂上使用了。 如果我使用这个- @io.swagger.v3.oas.annotations.parameters.RequestBody(内容=@Content(示例={@

  • 我正在用Swagger创建一个API文档。我直接尝试了openapi 3.0。不知何故,我无法得到我的请求机构工作的描述。 但这些描述不会出现: 我想得到像《大摇大摆2》那样的东西。下面是如何将相同的代码转换为Swagger 2

  • 问题内容: 我有一个包含表单(帖子)的html。单击某些提交按钮时,我会收到JSON响应。 如何获取此JSON? 如果我不截取请求,则json将显示在Web视图上,所以我想应该使用(我正在使用API​​ 12),但是我不知道如何在其中获取json。 还是有更好的方法,例如拦截响应而不是请求? 谢谢 问题答案: 您应该重写的方法

  • 在我的views.py中,我有一个函数,它每次使用不同的响应来调用各种requests.get() 在我的测试类中,我想做这样的事情,但无法计算出确切的方法调用 步骤1: 验证响应包含“a response”、“b response”、“c response” 如何完成步骤1(模拟请求模块)?

  • 问题内容: 我正在使用RestEasy并hibernate以返回Jackson的响应。我有一个bean播放器,具有以下字段:名称,id,年龄,位置。 现在,我正在实现两种重现json的rest方法。 ,这将返回一名玩家:姓名,身份证,年龄,位置。 ,这将返回玩家列表,但是有了这个玩家列表,我不想返回位置。 我的意思是,我该如何为一个响应添加一个字段,而对另一个响应忽略它。 请提出建议。 谢谢 问题

  • 我已经从其中一个服务复制了swagger.json文件,并想将其导入到Postman collection中