copolii
copolii

Reputation: 14506

Custom JSON keys for appengine endpoint entities

I've been searching for this for a while and am pretty sure I'm using the wrong terminology in my keywords. So here's an example to demonstrate what I want to do:

I have an Entity defined as such

@Entity 
public class DeviceRecord {
    @Id private    String id;
    private        String apikey;
    @Index private String description;
}

When I use this in an @ApiMethod such as this

@ApiMethod
public DeviceRecord login (final HttpServletRequest request, final String id) {
    return doSomethingThatGeneratesADeviceRecord(request, id);
}

I get back the following JSON on the client:

{
  "id" : "did1234567",
  "apikey" : "apikey",
  "description" : "Bob"
}

I want to use shorter keys to reduce the overhead and get something like below

{
  "id" : "did1234567",
  "ak" : "apikey",
  "dn" : "Bob"
}

How do I go about annotating these fields so that they are deserialized with the custom keys and not the default field names?

Thanks.

Update (Answer):

@Entity 
public class DeviceRecord {
    @Id @APIResourceProperty (name = "id" )    private String id;
    @APIResourceProperty (name = "ak" )        private String apikey;
    @Index @APIResourceProperty (name = "dn" ) private String description;
}

Upvotes: 0

Views: 228

Answers (1)

Romin
Romin

Reputation: 8816

Please take a look at @APIResourceProperty annotation. This lets you annotate properties of an Entity with your own customized property names.

Documentation is here : https://developers.google.com/appengine/docs/java/endpoints/annotations#apiresourceproperty

For e.g. on the getAPIKey() method in your Entity, you could add the following annotation : @ApiResourceProperty(name = "ak") and so on.

Upvotes: 1

Related Questions