Swagger API 具有查询字符串

2024-02-24

我想部署一个具有查询字符串的 API。这是 API

v1/products?q=circuit breaker&locale=en-GB&pageSize=8&pageNo=1&project=GLOBAL

这是我的实施方式

"/v1/products?q={searchText}&locale={ctrCode}&pageSize={pageSize}&pageNo={pageNo}&project={project}&country={country}":{  
         "get":{  
            "tags":[  
               "Search Text"
            ],
            "summary":"Get Products by searching text, countrycode, page number, pagesize, project and country(optional)",
            "description":"Get Products by searching text, countrycode, page number, pagesize, project and country(optional)",
            "operationId":"getProductName",
            "produces":[  
               "application/json",
               "application/xml"
            ],
            "parameters":[  
               {  
                  "name":"searchText",
                  "in":"path",
                  "description":"The Product that needs to be fetched",
                  "required":true,
                  "type":"string"
               },
               {  
                  "name":"ctrCode",
                  "in":"path",
                  "description":"The Product locale needs to be fetched. Example=en-GB, fr-FR, etc.",
                  "required":true,
                  "type":"string"
               },
               {  
                  "name":"pageSize",
                  "in":"path",
                  "description":"The Product PageSize that needs to be fetched. Example=10, 20 etc.",
                  "required":true,
                  "type":"number"
               },
               {  
                  "name":"pageNo",
                  "in":"path",
                  "description":"The Product pageNo that needs to be fetched. Example=1,2 etc.",
                  "required":true,
                  "type":"number"
               },
               {  
                  "name":"project",
                  "in":"path",
                  "description":"The Project that needs to be fetched. Example=Mypact, DSL etc.",
                  "required":true,
                  "type":"string"
               },
               {  
                  "name":"country",
                  "in":"header",
                  "description":"The Country that needs to be fetched. Example=France, India etc.",
                  "required":false,
                  "type":"string"
               }
            ],
            "responses":{  
               "200":{  
                  "description":"successful operation",
                  "schema":{  
                     "$ref":"#/definitions/Products"
                  }
               },
               "400":{  
                  "description":"Invalid Product_id supplied"
               },
               "404":{  
                  "description":"Product not found"
               }
            }
         }
      }

国家/地区是其中的可选参数。我希望 URL 仅在用户输入某些值时才显示国家/地区,否则不应在 URL 中显示。


您无法将查询参数描述为 Swagger 中路径的一部分。您必须将它们显式声明为查询参数。

"/v1/products":{  
         "get":{  
            "tags":[  
               "Search Text"
            ],
            "summary":"Get Products by searching text, countrycode, page number, pagesize, project and country(optional)",
            "description":"Get Products by searching text, countrycode, page number, pagesize, project and country(optional)",
            "operationId":"getProductName",
            "produces":[  
               "application/json",
               "application/xml"
            ],
            "parameters":[  
               {  
                  "name":"searchText",
                  "in":"query",
                  "description":"The Product that needs to be fetched",
                  "required":true,
                  "type":"string"
               },
               {  
                  "name":"ctrCode",
                  "in":"query",
                  "description":"The Product locale needs to be fetched. Example=en-GB, fr-FR, etc.",
                  "required":true,
                  "type":"string"
               },
               {  
                  "name":"pageSize",
                  "in":"query",
                  "description":"The Product PageSize that needs to be fetched. Example=10, 20 etc.",
                  "required":true,
                  "type":"number"
               },
               {  
                  "name":"pageNo",
                  "in":"query",
                  "description":"The Product pageNo that needs to be fetched. Example=1,2 etc.",
                  "required":true,
                  "type":"number"
               },
               {  
                  "name":"project",
                  "in":"query",
                  "description":"The Project that needs to be fetched. Example=Mypact, DSL etc.",
                  "required":true,
                  "type":"string"
               },
               {  
                  "name":"country",
                  "in":"query",
                  "description":"The Country that needs to be fetched. Example=France, India etc.",
                  "required":false,
                  "type":"string"
               }
            ],
            "responses":{  
               "200":{  
                  "description":"successful operation",
                  "schema":{  
                     "$ref":"#/definitions/Products"
                  }
               },
               "400":{  
                  "description":"Invalid Product_id supplied"
               },
               "404":{  
                  "description":"Product not found"
               }
            }
         }
      }
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Swagger API 具有查询字符串 的相关文章

随机推荐