Endpoints method not working, why? - java

When I test my google endpoints API in the api explorer, the saveProfile method throws the 503 Service Unavailable error, but all other methods work fine.
Here is the saveProfile method in my endpoint:
#ApiMethod(name = "saveProfile")
public Profile saveProfile(final ProfileForm profileForm) {
String firstName = profileForm.getFirstName();
String lastName = profileForm.getLastName();
String email = profileForm.getEmail();
Profile profile = new Profile("124234132", email, firstName, lastName);
ofy().save().entity(profile).now();
return profile;
}
Here is the Profile entity class:
#Entity
public class Profile {
#Id
private String userId;
private String email;
private String firstName;
private String lastName;
public Profile(String userId, String email, String firstName, String lastName) {
this.userId = userId;
this.email = email;
this.firstName = firstName;
this.lastName = lastName;
}
}
Here is the profileForm class:
public class ProfileForm {
private String firstName;
private String lastName;
private String email;
public ProfileForm() {}
public ProfileForm(String firstName, String lastName, String email) {
this.firstName = firstName;
this.lastName = lastName;
this.email = email;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public String getEmail() {
return email;
}
}
I have registered the entity and have set the API_EXPLORER_CLIENT_ID as well as the web and android client IDs.
Does somebody understand how to fix this so that the method just returns the profile object as it should?

In order for Profile to be serializable you need to define getters and setters for each of its fields (ex. getUserId(), setUserId(string Id)).
An Objectify entity must also include a no-arg constructor: https://github.com/objectify/objectify/wiki/Entities
Let me know if that fixes the error.

Related

Disaster management Class shows "cannot be applied" to User u2 error

I'm working on this lab where I'm creating a hypothetical disaster management system for people to put information about disasters with Java. Within the code, there are two classes with the same name User. The tutorial for the lab says that line 75 with user u2 is supposed to use an Emergency Contact class by using the following code:
User u2 = new User("Marlena", "Evans","6088861222", "me#daysofourlives.com");
However, with this, the console brings up this error:
'User(java.lang.String, java.lang.String, java.lang.String, java.lang.String)' in 'User' cannot be applied to '(java.lang.String, java.lang.String, boolean, int, java.lang.String, java.lang.String)'
so I changed it to:
User u2 = new User("Marlena", "Evans", false, 30, "6088861222","me#daysofourlives.com");
which removed the error.
I need a fresh set of eyes on this. Can someone look at this and tell me why the tutorial's code isn't working?
Here is what the code is supposed to look like:
and here is my code:
//import java.sql.Timestamp (package already active)
public class User {
private String firstName;
private String lastName;
private boolean gender; //true - male; false - female
private int age;
private BloodType blood;
private String telephone;
private String email;
private User emergencyContact;
public void setContact(User u){
this.emergencyContact = u;
}
public User (
String firstName,
String lastName,
boolean gender,
int age,
String blood,
String telephone,
String email
) {
this.firstName = firstName;
this.lastName = lastName;
this.gender = gender;
this.age = age;
this.blood = BloodType.fromString(blood);
this.telephone = telephone;
this.email = email;
}
public String getFirstName(){
return this.firstName;
}
public String getLastName(){
return this.lastName;
}
public boolean getGender(){
return this.gender;
}
public int getAge() {
return this.age;
}
public String getTelephone(){
return this.telephone;
}
public String getEmail() {
return this.email;
}
public BloodType getBlood() {
return this.blood;
}
public User getEmergencyContact() {
return this.emergencyContact;
}
/**
* #param args
*/
public static void main(String[] args) {
// TODO
//Here is an example of using the new user constructor
User u1 = new User("John","Black",true,25, "6085551234","jb#daysof" +
"ourlives.com");
//This example uses the Emergency Contact Constructor to create a new emergency contact
User u2 = new User("Marlena", "Evans", false, 30, "6088861222","me#daysofourlives.com");
u1.setContact(u2); //This means Marlena is the Emergency Contact for John
System.out.println("User: " + u1.firstName + " has an emergency contact of: " + u1.emergencyContact.getFirstName());
}
public User (
String firstName,
String lastName,
boolean gender,
int age,
String telephone,
String email
){
this.firstName = firstName;
this.lastName = lastName;
this.gender = gender;
this.age = age;
this.email = email;
}
}
Thank you and as always, let me know if you need more context to help me.
You are missing an additional constructor with the reduced number of arguments that constructor call in line 75 is using.
Add this to your User class:
public User(String firstName, String lastName, String telephone, String email) {
this.firstName = firstName;
this.lastName = lastName;
this.telephone = telephone;
this.email = email;
}
Also, in the main class, you are accessing firstName and emergencyContact fields, but these are private fields, so either set them to public or add a setter method.

How can I exclude a field from the schema when generating schemas in Jackon?

I'm using Jackson's JsonSchemaGenerator to generate schemas for my beans. One of these beans have a getter that I would like to exclude from the schema generation (or alternatively, mark is as an "object" - not any).
How would I go about generating a schema but forcing the address property to be either excluded or any object?
public static class Person {
private final String firstName;
private final String lastName;
private final Address address;
public Person(
#JsonProperty("first_name") String firstName,
#JsonProperty("last_name") String lastName,
#JsonProperty("address") Address address) {
this.firstName = firstName;
this.lastName = lastName;
this.address = address;
}
#JsonProperty("first_name")
public String getFirstName() {
return firstName;
}
#JsonProperty("last_name")
public String getLastName() {
return lastName;
}
#JsonProperty("address")
public Address getAddress() {
return address;
}
}
I haven't found a way to achieve this using Jackson.

SonarQube issue resolve using #NotNull and #JsonProperty together

I'm working on sonarQube issues in my company, on previously developed module I've to modify variable names as a part of fix. So I've applied #JsonProperty but I can't remove #NotNull as well. So my problem is #JsonProperty doing it's job but not null validation failing (THROWING EXCEPTION). Please help me with solution should I apply both annotation's. We are using spring mvc, and restful call to this dto. For now I don't want to make any validation in controller.
public class CustomerImpl extends Customer {
#NotNull(message = "should not be null")
#JsonProperty("customer_Id")
private int customerId;
#NotNull(message = "should not be null")
#JsonProperty("first_name")
private String firstName;
#NotNull(message = "should not be null")
#JsonProperty("last_name")
private String lastName;
public int getCustomerId() {
return customerId;
}
public void setCustomerId(int customerId) {
this.customerId = customerId;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}

How to update a value stored in an Object on "Update" button click - Android?

I have the following class for an android application.
public class AccountVO
{
private String id;
private String username;
private String password;
private String email;
private String firstName;
private String lastName;
public AccountVO(String tempID, String tempUserName, String tempPassword, String tempEmail, String tempFirstName, String tempLastName)
{
this.id = tempID;
this.username = tempUserName;
this.password = tempPassword;
this.email = tempEmail;
this.firstName = tempFirstName;
this.lastName = tempLastName;
}
public String getId()
{
return id;
}
public String getUserName()
{
return username;
}
public String getEmail()
{
return email;
}
public String getFirstName()
{
return firstName;
}
public String getLastName()
{
return lastName;
}
}
I create the following objects in an activity.
AccountVO userAccount = new AccountVO("1", "scott", "password", "scott#mail.com", "Scott", "James");
AccountVO userAccount2 = new AccountVO("2", "john", "password", "jsmith#mail.com", "John", "Smith");
I have another activity where I retrieve the values from the objects which I created above and display them in EditText fields. Suppose I change the data in the fields and click on the "Update" Button, can anyone please tell as to how to update the values in my old AccountVO object? For Example: If I change the email via the edittitext field in "userAccount" AccountVO object (to say scott#abc.com), How to update that value in the same object?
Write a setter for each of the fields you have a getter for, like so:
public void setLastName(String lastName) {
this.lastName = lastName;
}
Then, call the setter in the Update function:
public void Update() {
userAccount.setLastName(editText.getText().toString());
}

How to use or annotate a dummy field in a JPA entity bean which is not supposed to be persisted in database

I have this code for login validation using a Struts2 action class which calls an EJB for LDAP validation and then if (LDAP credentials) validated, querying the user database to get the rest of the user information using the JPA entity bean which also acts like a POJO. Unlike the username, userid and other user info, password is not stored in the database, but for the sake of the POJO getter and setter method I attempt to include a dummy password field - for serving the Struts2 action form.
The problem is after ldap authentication, an exception occurs stating that the column "password" does not exist in the database (which was never meant to be anyway!)
Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.0.1.v20100213-r6600): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'PASSWORD' in 'field list'
Error Code: 1054
Call: SELECT PERSON_ID, ACTIVE_USER, EMAIL, FIRSTNAME, SURNAME, PASSWORD, FULLNAME, EMPLOYEE_NUMBER FROM xxte_employees WHERE (ACTIVE_USER = ?)
bind => [john.doe]
Query: ReadAllQuery(name="XxteEmployees.validateLogin" referenceClass=XxteEmployees sql="SELECT PERSON_ID, ACTIVE_USER, EMAIL, FIRSTNAME, SURNAME, PASSWORD, FULLNAME, EMPLOYEE_NUMBER FROM xxte_employees WHERE (ACTIVE_USER = ?)")
at org.eclipse.persistence.exceptions.DatabaseException.sqlException(DatabaseException.java:333)
at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.basicExecuteCall(DatabaseAccessor.java:687)
at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.executeCall(DatabaseAccessor.java:530)
at org.eclipse.persistence.sessions.server.ServerSession.executeCall(ServerSession.java:529)
....
Here's the code from the entity bean:
public class XxteEmployees implements Serializable {
private static final long serialVersionUID = 1L;
#Column(name = "ACTIVE_USER")
private String activeUser;
#Column(name = "EMAIL")
private String email;
#Id
#Basic(optional = false)
#Column(name = "PERSON_ID")
private Double personId;
#Column(name = "EMPLOYEE_NUMBER")
private Double employeeNumber;
#Column(name = "FIRSTNAME")
private String firstname;
#Column(name = "SURNAME")
private String surname;
private String fullname;
private String password;
public XxteEmployees() {
}
public XxteEmployees(Double personId) {
this.personId = personId;
}
public String getActiveUser() {
return activeUser;
}
public void setActiveUser(String activeUser) {
this.activeUser = activeUser;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Double getPersonId() {
return personId;
}
public void setPersonId(Double personId) {
this.personId = personId;
}
public Double getEmployeeNumber() {
return employeeNumber;
}
public void setEmployeeNumber(Double employeeNumber) {
this.employeeNumber = employeeNumber;
}
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getSurname() {
return surname;
}
public void setSurname(String surname) {
this.surname = surname;
}
// BEGIN: objects not in db
public String getFullname() {
return firstname + ' ' + surname;
}
public void setFullname(String fullname) {
this.fullname = firstname + ' ' + surname;;
}
public String getPassword() {
return sifre;
}
public void setPassword(String password) {
this.password = password;
}
// END: objects not in db
Any workaround for this?
Annotate any fields that you don't want to be persisted as #Transient. It is in the javax.persistence package.

Categories