0

I'm just getting started with Google Cloud Endpoints. I'm trying to add a new method to the Echo example to simply use a GET instead of a POST. After deploying, it seems that the new API method can't be found. For the life of me, I can't figure it out.

Here is the java code for the new method called echo2. The HttpdMethod was changed from POST to GET and the msg argument was changed from a Message to a 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 );
}

And here is the code that was added to openapi.json. I made two changes: i) The first parameter was changed to a query parameter ii) The first parameter was changed from a Message object (which just contains a string) to a 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"
                }
            }
        }
    }
},

I deploy this (to Google App Engine Standard), redeploy the openapi.json and run the curl below, the response is 404.

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

What am I doing wrong?

Mike Dee
  • 558
  • 1
  • 5
  • 13

2 Answers2

0

Looking at the sample code, doEcho() accepts Message and Integer as arguments. If you want to change an argument to String, you could overload the method like this:

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;
  }

From your ApiMethod, by generating openapi.json using this command mvn endpoints-framework:openApiDocs, your json file should then look like this:

"/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"
      }
     }
    }
   }
  }

Then pass your request like this:

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

However I tried following the quickstart from scratch as well and I'm getting an empty result {} with 404 errors on App Engine logs, even though the request returned 200. Assuming that you're also following the sample mentioned above, then I suggest that you open an issue through their GitHub issues link.

Donnald Cucharo
  • 3,866
  • 1
  • 10
  • 17
  • You've highlighted several issues that I've been having. First, I don't want a "post" method, which is what the mvn endpoints always seems to generate. So, I hand-coded by openapi.json. Note that your curl statement uses --request GET. Second, my sample will work if I use path parameters and a get method. But won't work with query parameters and GET. From a few days of playing around with GCE and OpenAPI, it seems quite fragile, and I wonder if there are many people actually using it. – Mike Dee Nov 20 '20 at 06:56
  • I missed that out, I made a minor update to my answer to apply your correction – Donnald Cucharo Nov 20 '20 at 07:37
0

I discovered how to differentiate GET and PATH parameters.

For GET parameters make sure the Java method is annotated with @Nullable.

After doing this, the above example worked.

Mike Dee
  • 558
  • 1
  • 5
  • 13
  • Meant to add this link: https://cloud.google.com/endpoints/docs/frameworks/java/parameter-and-return-types – Mike Dee Dec 01 '20 at 19:38