Codevalley
Codevalley

Reputation: 4641

Passing array with in GAE Endpoints

I am just trying out the first example of GAE Endpoints, I modified the sample API Method to resemble this.

@ApiMethod(name = "sayHi")
public MyBean sayHi(@Named("name") String[] names) {

My expectation is to receive a array of strings. Now when I use the Google API Explorer to test this, [https://apis-explorer.appspot.com/apis-explorer/] it generates API like this

POST https://myprojectid.appspot.com/_ah/api/myApi/v1/sayHi/arg1/arg2/arg3?fields=data

API explorer screenshot

It eventually returns 404 error. Since the endpoint is not recognized.

What am I doing wrong here? In fact explorer shows name as String not String[]. Any help is appreciated!

Upvotes: 0

Views: 507

Answers (2)

Renaud Tarnec
Renaud Tarnec

Reputation: 83153

Instead of having an array as a parameter of the endpoint method, you should put an object (java bean) which contains an array as a property.

Then you get the object in your method and you just read the property and treat it as an array.

Edit after some more research, following your comment

Indeed when you try to pass an array as a Path parameter it doesn't work. The different elements of your array are added to the URL (as you show in your question) and it generates a 404 Not Found error. The trick is that you should pass this array as a Query parameter and not a Path Parameter. See this doc: https://cloud.google.com/appengine/docs/java/endpoints/parameter-and-return-types#path_parameters

And indeed, if you do something like that it works very well:

@ApiMethod(name = "sayHi",
        path = "sayHiWithName")
public MyBean sayHi(@Named("name") String[] names) {
    MyBean response = new MyBean();
    response.setData("Hi, " + names[0] + names[1]);

    return response;
}

Note that the parameter is NOT added to the path (i.e. we don't have a path like sayHiWithName/{name}).

Upvotes: 1

HammondSuisse
HammondSuisse

Reputation: 143

First things first: does this work when there is a single String parameter? There's some servlet mapping magic that needs to happen to expose endpoints, and if that is not present in the project, things won't work. See this link to make sure your web.xml is as it should be.

Looking at this link, it seems that if your method parameter is a basic type (not a real Java object), and if it is not specifically included in a @Path annotation, there's some uncertainty in what will happen in your Api:

Path parameters are the method parameters included in the path property of the @ApiMethod annotation. If path is unspecified, any parameters not annotated with @Nullable or @DefaultValue will be automatically added to the path (they will be path parameters).

So it seems that by not including "name" in a @Path annotation, the docs don't state what the format of the path will be. The generated descriptor that the Explorer is looking at seems to think the right answer is /names[0]/names[1]/names[2], kind of like C-style varargs. It might be this disconnect that causes your 404 to happen. Can you try by including "name" in a @Path annotation?

Upvotes: 1

Related Questions