Domanda

Not able to generate client library after adding the following method to the endpoint:

@ApiMethod(name = "checkNumberPresent")
public List<Entity> checkNumberPresent(@Named("phoneNumber") String phoneNumber){
    List<Entity> result;
    result=ContactSearchHelper.ifNumberExists(phoneNumber);
    return result;

}

The method is to query the datastore if a number exists.However, while trying to generate client library,following error message is displayed. enter image description here

Where am I getting wrong?Thanks.

My Entity Class code:

@Entity
public class UserInfo {

@Id
private String userRegistrationID;
private String userPhoneNumber;
private long timestamp;

public String getUserRegistrationID() {
  return userRegistrationID;
}

public String getUserPhoneNumber() {
  return this.userPhoneNumber;
}

public void setUserRegistrationID(String userRegistrationID) {
  this.userRegistrationID = userRegistrationID;
}

public void setUserPhoneNumber(String userPhoneNumber) {
 this.userPhoneNumber = userPhoneNumber;
}
public void setRegisteredAppVersion(String registeredAppVersion) {
    this.registeredAppVersion = registeredAppVersion;
}
public String getRegisteredAppVersion() {
    return registeredAppVersion;
}

public long getTimestamp() {
  return timestamp;
}

public void setTimestamp(long timestamp) {
 this.timestamp = timestamp;
}
}

And the code for querying datastore to retrieve data is:

  public class ContactSearchHelper {

  //Query datastore to retrieve entity with the phoneNumber field equal to the passed phoneNumber
  public static List<Entity> ifNumberExists(String phoneNumber){

    DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
    Filter numberFilter =new FilterPredicate("userPhoneNumber",FilterOperator.EQUAL,phoneNumber);

    // Use class Query to assemble a query
            Query q = new Query("UserInfo").setFilter(numberFilter);
            // Use PreparedQuery interface to retrieve results
            PreparedQuery pq = datastore.prepare(q);
            int numberOfEntities=pq.countEntities(null);

            if(numberOfEntities>0){
                List<Entity> retrievedList=pq.asList(null);
                return retrievedList;
            }else{
                return null;
            }


}

   }
  }

I hope I am able to provide sufficient codes to explain the issue.Thanks.

È stato utile?

Soluzione

I had exactly the same error. In my case the problem was that I have added custom method getEntity to my class that is wrapped into API. This method was intended to cast my class to Entity objects (as I did not use JDO/JPA). Something like:

public class MyUser {


    private String userCookie;
    public String getUserCookie() {
        return userCookie;
    }

    public void setUserCookie(String userCookie) {
        this.userCookie = userCookie;
    }

    ...

    public Entity getEntity(){
        Entity e = new Entity("MyUser");
        e.setProperty("userCookie", this.getUserCookie());

        ...

        return e;
    }

}

(Now I understand that it was not good architecture). And in my API get method I wrote something like:

@Api(name = "user")
public class UserApi {

    @ApiMethod(name = "users.insert")
    public void insert(MyUser user) {
        DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
        e = user.getEntity();
        datastore.put(e);

    }
}

It was a mistake to start method name with getXYZ prefix when one do not have XYZ property in the class which is expected to be POJO (and that is a case in cloud endpoints). I just renamed getEntity method to convertToEntity and everything started working. So, check if you classes do not have custom getters. Getters and setters should be only for properties declared in the class.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top