【问题标题】:GET with query parameter produces "not found"带有查询参数的 GET 产生“未找到”
【发布时间】:2021-03-03 08:28:20
【问题描述】:

我刚刚开始使用 Google Cloud Endpoints。我正在尝试向 Echo 示例添加一个新方法,以简单地使用 GET 而不是 POST。部署后,好像找不到新的API方法。对于我的生活,我无法弄清楚。

这是名为 echo2 的新方法的 java 代码。 HttpdMethod 从 POST 更改为 GET,msg 参数从 Message 更改为 String。

@ApiMethod(name = "echo2",httpMethod = ApiMethod.HttpMethod.GET)
public Message echo2( @Named("msg") String msg, @Named("n") @Nullable Integer n )
{
    logger.info( "echo2(): msg=" + msg + ", and n=" + n );  
    
    return doEcho( msg, n );
}

这是添加到 openapi.json 的代码。我做了两处更改:i) 第一个参数更改为查询参数 ii) 第一个参数从 Message 对象(仅包含一个字符串)更改为 String。

"/echo/v1/echo2": {
    "get": {
        "operationId": "Echo2",
        "parameters": [
            {
                "name": "msg",
                "in": "query",
                "required": true,
                "type": "string"
            },
            {
                "name": "n",
                "in": "query",
                "required": false,
                "type": "integer",
                "format": "int32"
            }
        ],
        "responses": {
            "200": {
                "description": "A successful response",
                "schema": {
                    "$ref": "#/definitions/Message"
                }
            }
        }
    }
},

我部署这个(到 Google App Engine 标准),重新部署 openapi.json 并运行下面的 curl,响应是 404。

curl --request GET 'https://mytestapi.appspot.com/_ah/api/echo/v1/echo2?msg=hey' --header '接受:application/json' --header 'Content-Type : 应用程序/json'

我做错了什么?

【问题讨论】:

    标签: google-app-engine swagger google-cloud-endpoints openapi


    【解决方案1】:

    查看sample codedoEcho() 接受 Message 和 Integer 作为参数。如果要将参数更改为 String,可以像这样重载方法:

    private Message doEcho(String request, Integer n) {
        Message response = new Message();
        if (n != null && n >= 0) {
          StringBuilder sb = new StringBuilder();
          for (int i = 0; i < n; i++) {
            if (i > 0) {
              sb.append(' ');
            }
            sb.append(request);
          }
          response.setMessage(sb.toString());
        }
        return response;
      }
    

    从您的 ApiMethod 中,通过使用此命令 mvn endpoints-framework:openApiDocs 生成 openapi.json,您的 json 文件应如下所示:

    "/echo/v1/echo2/{msg}": {
       "get": {
        "operationId": "EchoEcho2",
        "parameters": [
         {
          "name": "msg",
          "in": "path",
          "required": true,
          "type": "string"
         },
         {
          "name": "n",
          "in": "query",
          "required": false,
          "type": "integer",
          "format": "int32"
         }
        ],
        "responses": {
         "200": {
          "description": "A successful response",
          "schema": {
           "$ref": "#/definitions/Message"
          }
         }
        }
       }
      }
    

    然后像这样传递您的请求:

    curl \
    --request GET \
    --header 'Accept: application/json' \
    --header 'Content-Type: application/json'\
    'https://mytestapi.appspot.com/_ah/api/echo/v1/echo2/hey' 
    

    但是,我也尝试从头开始遵循快速入门,但我在 App Engine 日志中得到了一个空结果 {},其中包含 404 错误,即使请求返回 200。假设您也在遵循上述示例,那么我建议你通过他们的 GitHub issues 链接打开一个问题。

    【讨论】:

    • 您强调了我遇到的几个问题。首先,我不想要一个“post”方法,这是 mvn 端点似乎总是生成的。所以,我是用 openapi.json 手工编码的。请注意,您的 curl 语句使用 --request GET。其次,如果我使用路径参数和 get 方法,我的示例将起作用。但不适用于查询参数和 GET。玩了几天GCE和OpenAPI,感觉挺脆弱的,不知道有没有很多人在用。
    • 我错过了,我对我的答案进行了小幅更新以应用您的更正
    【解决方案2】:

    我发现了如何区分 GET 和 PATH 参数。

    对于 GET 参数,请确保使用 @Nullable 注释 Java 方法。

    这样做之后,上面的例子就成功了。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多