查询参数注释在Swagger中以身体参数生成?

发布于 2025-02-12 20:40:19 字数 1179 浏览 2 评论 0原文

我有一个使用Restlet Swagger Extention的雷神API应用程序,通过Swagger2SpecificationRestlet生成Swagger JSON。

// route to generated swagger json
swagger2SpecificationRestlet.attach(router, "/docs");  

我的路线是这样定义的:

router.attach("/path/{pathParam}", MyResource.class);

我已经在本地实施了Swagger-UI,并设置了初始化器URL,以读取/docs的Swagger JSON。在包括所需路径参数输入的所有路由的所有路线上,UI的工作原理,但是查询参数的注释在没有其他定义的字段的情况下呈现为邮局(?)。参数输入:

"parameters": [
    {
        "name": "pathParam",
        "in": "path",
        "required": true,
        "type": "string"
    },
    {
        "in": "body",
        "name": "body",
        "required": false,
        "schema": {
        "type": "string"
    }
],

带注释的我的资源方法:

    @ApiOperation(
        value="test desc",
        httpMethod = "GET", 
        produces = "application/json", 
        notes="testing notes"
    )
    @Get("txt")
    public String represent(
        @ApiParam(name="queryParam", value = "testing desc") 
        @QueryParam("queryParam") String queryParam
        ) throws SQLException { ... }

我如何注释查询参数,以生成正确的JSON配置?

I have a Restlet API application using the Restlet Swagger extenstion to generate the Swagger json via Swagger2SpecificationRestlet.

// route to generated swagger json
swagger2SpecificationRestlet.attach(router, "/docs");  

My routes are defined like this:

router.attach("/path/{pathParam}", MyResource.class);

I have implemented swagger-ui locally and set the initializer url to read the swagger json from /docs. The UI works as expected for all of the routes including the required path parameter inputs however the annotations for the query params are rendering as post body(?) without any of the other defined fields. param inputs:

"parameters": [
    {
        "name": "pathParam",
        "in": "path",
        "required": true,
        "type": "string"
    },
    {
        "in": "body",
        "name": "body",
        "required": false,
        "schema": {
        "type": "string"
    }
],

My resource method with annotations:

    @ApiOperation(
        value="test desc",
        httpMethod = "GET", 
        produces = "application/json", 
        notes="testing notes"
    )
    @Get("txt")
    public String represent(
        @ApiParam(name="queryParam", value = "testing desc") 
        @QueryParam("queryParam") String queryParam
        ) throws SQLException { ... }

How can I annotate query params so that swagger generates the correct json configuration?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甜是你 2025-02-19 20:40:19

在进一步查看了文档之后,我遇到了这一点:

noreferrer“> https://docs.swagger.io/swagger-core/apidocs/com/wordnik/wordnik/swagger/annotations/apiimpliticparam.html#paramtype

public @interface apiimpliticparam代表一个单个参数
API操作。当Apiparam绑定到JAX-RS参数时,方法
或字段,这使您可以手动定义一个参数
微调的方式。 这是定义参数的唯一方法
使用servlet或其他非JAX-RS环境。

我用apiparam apiimpliticparam ,该有一个字段来声明param类型,并将注释移动上述注释该方法:

    @ApiOperation(
        value="get stuff",
        httpMethod = "GET", 
        produces = "application/json", 
        notes="test notes"
    )
    @Get("txt")
    @ApiImplicitParam(
        name="queryParam", 
        dataType = "String", 
        paramType = "query", 
        value = "testing query param desc", 
        defaultValue = "default val")
    public String represent() throws SQLException {
        return getMethod();
    }

它导致正确生成的JSON:

"parameters": [
    {
        "name": "pathParam",
        "in": "path",
        "required": true,
        "type": "string"
    },
    {
        "name": "queryParam",
        "in": "query",
        "description": "testing query param desc",
        "required": false,
        "type": "string",
        "default": "default val"
    }
]

After looking further into the documentation I came across this:

https://docs.swagger.io/swagger-core/apidocs/com/wordnik/swagger/annotations/ApiImplicitParam.html#paramType()

which states:

public @interface ApiImplicitParam Represents a single parameter in an
API Operation. While ApiParam is bound to a JAX-RS parameter, method
or field, this allows you to manually define a parameter in a
fine-tuned manner. This is the only way to define parameters when
using Servlets or other non-JAX-RS environments.

I replaced ApiParam with ApiImplicitParam which has a field to declare param type and moved the annotation above the method:

    @ApiOperation(
        value="get stuff",
        httpMethod = "GET", 
        produces = "application/json", 
        notes="test notes"
    )
    @Get("txt")
    @ApiImplicitParam(
        name="queryParam", 
        dataType = "String", 
        paramType = "query", 
        value = "testing query param desc", 
        defaultValue = "default val")
    public String represent() throws SQLException {
        return getMethod();
    }

Which results in the correctly generated json:

"parameters": [
    {
        "name": "pathParam",
        "in": "path",
        "required": true,
        "type": "string"
    },
    {
        "name": "queryParam",
        "in": "query",
        "description": "testing query param desc",
        "required": false,
        "type": "string",
        "default": "default val"
    }
]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文