So I'm having a strange issue with Firebase, and I can't seem to find any info on it. I'm attempting to add a user object to the database. When I check Firebase, it's created an object with the correct username, but it doesn't add any of the attributes that are Strings, and the 2 double attributes are zero. I've turned off user authentication for now, until I get this working. Here's the relevant code:
public class User {
public String email;
public String password;
public String firstName;
public String lastName;
public String dob;
public double height;
public double weight;
public String gender;
public User()
{
}
public User(String email, String password, String firstName, String lastName, String DoB, double height, double weight, String gender) {
}
public String getEmail()
{
return email;
}
public String getPassword()
{
return password;
}
public String getFirstName()
{
return firstName;
}
public String getLastName()
{
return lastName;
}
public String getdob()
{
return dob;
}
public double getHeight()
{
return height;
}
public double getWeight()
{
return weight;
}
public String getGender()
{
return gender;
}
public void setEmail(String email)
{
this.email = email;
}
public void setPassword(String password)
{
this.password = password;
}
public void setFirstName(String firstName)
{
this.firstName = firstName;
}
public void setLastName(String lastName)
{
this.lastName = lastName;
}
public void setdob(String dob)
{
this.dob = dob;
}
public void setHeight(double height)
{
this.height = height;
}
public void setWeight(double weight)
{
this.weight = weight;
}
public void setGender(String gender)
{
this.gender = gender;
}
}
final FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference usersRef = database.getReference();
...
private void registerUser() {
usersRef.child("testuser").setValue(new User("test#test.com", "password", "First", "Last", "2000/01/01", 172, 75, "Male"));
After that completes, if I check Firebase, it shows "users", with an object called "testuser", but the testuser's only attributes are height and weight, both with value 0. If I change height and weight to Strings instead of doubles, nothing at all is added to the database. As far as I can tell, everything is pretty much identical to the Firebase docs, so I'm pretty well stumped. Anyone seen anything like this?
First, delete the static keyword from your User class declaration. Should only be:
public class User {}
Add the no-argument constructor needed for Firebase.
public User() {}
Add also the getters and setters for all of your fields.
This how your model class should look like:
public class User {
private String email;
private String password;
private String firstName;
private String lastName;
private String dob;
private double height;
private double weight;
private String gender;
public User() {}
public User(String email, String password, String firstName, String lastName, String dob, double height, double weight, String gender) {
this.email = email;
this.password = password;
this.firstName = firstName;
this.lastName = lastName;
this.dob = dob;
this.height = height;
this.weight = weight;
this.gender = gender;
}
public String getEmail() {return email;}
public String getPassword() {return password;}
public String getFirstName() {return firstName;}
public String getLastName() {return lastName;}
public String getDob() {return dob;}
public double getHeight() {return height;}
public double getWeight() {return weight;}
public String getGender() {return gender;}
}
Related
I want to create a POJO class with Constructor for the POST request body which has nested JSON, But I am not sure how to call JSONArray inside it?
PS: I do not want to set data using the setter method, I want to use Constructor for setting the data.
Here is the JSON:
{
"FirstName": "test",
"LastName": "account",
"PASSWORD": "Password123*",
"Email": [
{
"TYPE": "Primary",
"VALUE": "arpitay6#mail7.io"
}
]}
POJO I've created -
import java.util.List;
public class PostAccountCreateAPI {
private List <Email> email;
private String password;
private String firstname;
private String lastname;
public PostAccountCreateAPI(List<Email> email, String password, String firstname, String lastname) {
this.email = email;
this.password = password;
this.firstname = firstname;
this.lastname = lastname;
}
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;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public List<Email> getEmail() {
return email;
}
public void setEmail(List<Email> email) {
this.email = email;
}
}
package pojo;
public class Email {
private String type;
private String value;
public Email(String type, String value) {
this.type = type;
this.value = value;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
In the main method, I am calling POJO using -
PostAccountCreateAPI PostAccountCreateAPIPayLoad = new PostAccountCreateAPI("pri#mail7.io", "P#$$w0rd", "arpita", "garg");
But It is not working. Can anyone please suggest how to do this?
Ok....Here I am Giving a complete example:
The PostAccountCreateApi class:
import java.util.List;
public class PostAccountCreateAPI{
private String FirstName;
private String LastName;
private String PASSWORD;
private List<Email> Email;
public PostAccountCreateAPI(){}
public PostAccountCreateAPI(String FirstName, String LastName, String PASSWORD, List<Email> Email){
this.FirstName = FirstName;
this.LastName = LastName;
this.PASSWORD = PASSWORD;
this.Email = Email;
}
public void setFirstName(String FirstName){
this.FirstName = FirstName;
}
public String getFirstName(){
return this.FirstName;
}
public void setLastName(String LastName){
this.LastName = LastName;
}
public String getLastName(){
return this.LastName;
}
public void setPASSWORD(String PASSWORD){
this.PASSWORD = PASSWORD;
}
public String getPASSWORD(){
return this.PASSWORD;
}
public void setEmail(List<Email> Email){
this.Email = Email;
}
public List<Email> getEmail(){
return this.Email;
}
}
The Email Class:
public class Email {
String TYPE;
String VALUE;
public Email() {
}
public Email(String TYPE, String VALUE) {
this.TYPE = TYPE;
this.VALUE = VALUE;
}
public void setTYPE(String TYPE) {
this.TYPE = TYPE;
}
public String getTYPE() {
return this.TYPE;
}
public void setVALUE(String VALUE) {
this.VALUE = VALUE;
}
public String getVALUE() {
return this.VALUE;
}
}
The main class with a dummy main method:
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
List<Email> emailList = new ArrayList<>();
emailList.add(new Email("Primary", "pri#mail7.io"));
emailList.add(new Email("Primary", "amimulahsan7#gmail.com"));
//And list goes on......
PostAccountCreateAPI postAccountCreateAPI = new PostAccountCreateAPI("arpita", "garg",
"P#$$w0rd", emailList);
}
}
The first parameter to the constructor is List<Email>, but you are currently passing a String as the first argument to the constructor. Create a list of Email objects and then invoke the PostAccountCreateAPI constructor.
I am using spring security to login and logout, eveything works fine.
I can get username from logged user fine, however i need userID,
I would like to know how can i get user as an object from logged in user or how could i get userID
#RequestMapping("/contato")
public String contato(Model model, Principal principal ){
String userName = principal.getName();
model.addAttribute("userName",userName);
System.out.println(userName);
return "contato";
}
Bean
import java.sql.Date;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;
import org.hibernate.validator.constraints.Email;
import org.hibernate.validator.constraints.NotBlank;
public class Users {
private int user_id;
#NotBlank
#Size(min=1, max=100, message="Name must be between 1 and 100 characters")
private String firstname;
#NotBlank
private String surname;
#NotNull
private Date dob;
#NotBlank
#Email
private String username;
#NotBlank
private String telephone;
#NotBlank
private String address;
#NotBlank
private String city;
#NotBlank
private String country;
#NotBlank
private String postcode;
#NotBlank
#Size(min=6, message="Password must be have more than 6 characters")
private String password;
private boolean enabled = false;
private String authority;
public Users() {
}
public Users(int user_id, String firstname, String surname, Date dob, String username, String telephone,
String address, String city, String country, String postcode, String password, boolean enabled,
String authority) {
super();
this.user_id = user_id;
this.firstname = firstname;
this.surname = surname;
this.dob = dob;
this.username = username;
this.telephone = telephone;
this.address = address;
this.city = city;
this.country = country;
this.postcode = postcode;
this.password = password;
this.enabled = enabled;
this.authority = authority;
}
public int getUser_id() {
return user_id;
}
public void setUser_id(int user_id) {
this.user_id = user_id;
}
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;
}
public Date getDob() {
return dob;
}
public void setDob(Date dob) {
this.dob = dob;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getTelephone() {
return telephone;
}
public void setTelephone(String telephone) {
this.telephone = telephone;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public String getPostcode() {
return postcode;
}
public void setPostcode(String postcode) {
this.postcode = postcode;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public boolean isEnabled() {
return enabled;
}
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
public String getAuthority() {
return authority;
}
public void setAuthority(String authority) {
this.authority = authority;
}
}
Can anyone please help me to get user id from logged user
I have also tried using
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
Users user =(Users)authentication.getPrincipal();
but it still did not work
The simplest approach would be to leverage the UserDetails and UserDetailsService interfaces.
Write a simple UserDetailsService:
#Service
public class CustomUserDetailsService implements UserDetailsService {
#Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
return findUserByUsername(username); //load the user from somewhere (e.g. Database)
}
}
Have your Users class implement the UserDetails interface:
public class Users implements UserDetails {
private String username;
private String userId;
private String password;
private String role;
public Users(String username, String userId, String password, String role) {
this.username = username;
this.userId = userId;
this.password = password;
this.role = role;
}
//...
}
Finally, when you call this static method you'll receive the Users object from which you can extract the userId:
Users user = (Users) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
i am a newbie to java.I am trying to create a library system.
Which classes should be abstract? do i need more classes?
Yes you need many classes, Your classes should look like this :
class Person{
//attributes, getters and setters
}
class User extends Person{
//attributes, getters and setters
}
class Members extends Person{
}
class Librarian extends Person{
}
class Book{
//attributes, getters and setters
}
public class Person {
private String FirstName;
private String LastName;
private String Gender;
private String Contact;
private String Email;
public Person() {
}
public Person(String FirstName, String LastName, String Gender, String Contact, String Email) {
this.FirstName = FirstName;
this.LastName = LastName;
this.Gender = Gender;
this.Contact = Contact;
this.Email = Email;
}
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;
}
public String getGender() {
return Gender;
}
public void setGender(String Gender) {
this.Gender = Gender;
}
public String getContact() {
return Contact;
}
public void setContact(String Contact) {
this.Contact = Contact;
}
public String getEmail() {
return Email;
}
public void setEmail(String Email) {
this.Email = Email;
}
}
public class User extends Person {
private String Password;
private String Username;
boolean isEnabled;
public User() {
}
public User(String Password, String Username, boolean isEnabled) {
this.Password = Password;
this.Username = Username;
this.isEnabled = isEnabled;
}
public String getPassword() {
return Password;
}
public void setPassword(String Password) {
this.Password = Password;
}
public String getUsername() {
return Username;
}
public void setUsername(String Username) {
this.Username = Username;
}
public boolean isIsEnabled() {
return isEnabled;
}
public void setIsEnabled(boolean isEnabled) {
this.isEnabled = isEnabled;
}
}
public class Guest extends User {
public Guest() {
}
public Guest(String Password, String Username, boolean isEnabled) {
super(Password, Username, isEnabled);
}
public void App(){
}
}
public class Members extends User{
public Members() {
}
public Members(String Password, String Username, boolean isEnabled) {
super(Password, Username, isEnabled);
}
}
public class Libararian extends User {
public Libararian() {
}
public Libararian(String Password, String Username, boolean isEnabled) {
super(Password, Username, isEnabled);
}
}
public class Book {
private String Title;
private String Publisher;
private String Category;
public Book(String Title, String Publisher, String Category) {
this.Title = Title;
this.Publisher = Publisher;
this.Category = Category;
}
public Book() {
}
public String getTitle() {
return Title;
}
public void setTitle(String Title) {
this.Title = Title;
}
public String getPublisher() {
return Publisher;
}
public void setPublisher(String Publisher) {
this.Publisher = Publisher;
}
public String getCategory() {
return Category;
}
public void setCategory(String Category) {
this.Category = Category;
}
}
Summary:
New to Java, tried looking through other posts but didn't find an answer. I'm learning inheritance and have an AddressBook class extended by a Runner class. When I write a program to test the inheritance I create a Runner object. If I get the first String parameter it returns fine but when I attempt to get the second String parameter it returns null.
Question:
Why is the second parameter returning null?
package Assignment_1;
//Begin Class Definition
public class AddressBook {
// Member variables
private String businessPhone;
private String cellPhone;
private String facebookId;
private String firstName;
private String homeAddress;
private String homePhone;
private String lastName;
private String middleName;
private String personalWebSite;
private String skypeId;
//Constructors
public AddressBook (String firstName, String middleName, String lastName, String homeAddress, String businessPhone, String homePhone, String cellPhone, String skypeId, String facebookId, String personalWebSite) {
this.firstName = firstName;
this.middleName = middleName;
this.lastName = lastName;
this.homeAddress = homeAddress;
this.businessPhone = businessPhone;
this.homePhone = homePhone;
this.cellPhone = cellPhone;
this.skypeId = skypeId;
this.facebookId = facebookId;
this.personalWebSite = personalWebSite;
}
public AddressBook (String firstName) {
this.firstName = firstName;
}
public AddressBook(String firstName, String middleName) {
this.firstName = firstName;
this.middleName = middleName;
}
public AddressBook (String firstName, String middleName, String lastName) {
this.firstName = firstName;
this.middleName = middleName;
this.lastName = lastName;
}
// Getters and setters
public String getFirstName() {
return firstName;
}
public String getMiddleName() {
return middleName;
}
public String getLastName() {
return lastName;
}
public String getHomeAddress() {
return homeAddress;
}
public String getBusinessPhone() {
return businessPhone;
}
public String getHomePhone() {
return homePhone;
}
public String getCellPhone() {
return cellPhone;
}
public String getSkypeId() {
return skypeId;
}
public String getFacebookId() {
return facebookId;
}
public String getPersonalWebsite() {
return personalWebSite;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public void setMiddleName(String middleName) {
this.middleName = middleName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public void setHomeAddress(String homeAddress) {
this.homeAddress = homeAddress;
}
public void setBusinessPhone(String businessPhone) {
this.businessPhone = businessPhone;
}
public void setHomePhone(String homePhone) {
this.homePhone = homePhone;
}
public void setCellPhone(String cellPhone) {
this.cellPhone = cellPhone;
}
public void setSkypeId(String skypeId) {
this.skypeId = skypeId;
}
public void setFacebookId(String facebookId) {
this.facebookId = facebookId;
}
public void setPersonalWebSite(String personalWebSite) {
this.personalWebSite = personalWebSite;
}
// Public methods
public static void compareNames(String name1, String name2) {
if(name1.equals(name2)) {
System.out.println(name1);
System.out.println(name2);
System.out.println("The names are the same.");
} else {
System.out.println(name1);
System.out.println(name2);
System.out.println("The names appear to be different.");
}
}
************************************************************
package Assignment_1;
public class BanffMarathonRunner extends AddressBook {
// Member variables
private int time;
private int years;
// Constructors
public BanffMarathonRunner(String firstName, String lastName, int min, int yr) {
super(firstName, lastName);
time = min;
years = yr;
}
// Getters and Setters
public int getTime() {
return time;
}
public void setTime(int time) {
this.time = time;
}
public int getYears() {
return years;
}
public void setYears(int years) {
this.years = years;
}
}
************************************************************
package Assignment_1;
import Assignment_1.BanffMarathonRunner;
public class TestBanffMarathonRunner {
public static void main(String[] args) {
BanffMarathonRunner r1 = new BanffMarathonRunner("Elena", "Brandon", 341, 1);
System.out.print(r1.getLastName());
}
}
}
Because lastName is null.
You are calling AddressBook(String firstName, String middleName)
and setting the middleName, not the lastName.
BanffMarathonRunner r1 = new BanffMarathonRunner("Elena", "Brandon", 341, 1);
calls:
// firstName = "Elena"
// lastName = "Brandon"
// min = 341
// yr = 1
public BanffMarathonRunner(String firstName, String lastName, int min, int yr) {
super(firstName, lastName);
// ...
}
which calls via super(...):
// firstName = "Elena"
// middleName = "Brandon" <-- here is your issue
public AddressBook(String firstName, String middleName) {
this.firstName = firstName;
this.middleName = middleName;
}
Brandon is set in AddressBook#middleName instead of AddressBook#lastName.
Your problem is in the BanffMarathonRunner.java:
in the constructor when you are calling the
super(firstName, lastName);
Actually by the call above the super class constructor with two parameter is being called, and that constructor is the one which set the middleName not the lastName.
I think you are confused because of the lastName variable name, which is passed to the constructor with two argument and that constructor use the second argument to set the middleName.
Good Luck.
Recently I am doing a coding exercises I need to make my project , and so far I am practicing it with the code below what I want to ask is that, is this a has a relationship? am I doing the right practice? look at my code, sorry for my bad english
public class Personal {
private String firstName;
private String middleInitial;
private String lastName;
private int age;
public Personal(String firstName,String middleInitial , String lastName , int age){
setFirstName(firstName);
setMiddleInitial(middleInitial);
setLastName(lastName);
setAge(age);
}
public void setFirstName(String firstName){
this.firstName = firstName;
}
public String getFirstName(){
return firstName;
}
public void setMiddleInitial(String middleInitial){
this.middleInitial = middleInitial;
}
public String getMiddleInitial(){
return middleInitial;
}
public void setLastName(String lastName){
this.lastName = lastName;
}
public String getLastName(){
return lastName;
}
public void setAge(int age){
this.age = age;
}
public int getAge(){
return age;
}
public String toString(){
return String.format("First Name: "+getFirstName()+"\nMiddle Initial: "+getMiddleInitial()+
"\nLast Name: "+getLastName()+"\nAge: "+getAge());
}
}
Contact Class
public class Contact {
private String address;
private String email;
private String contactNumber;
public Contact(String address,String contactNumber, String email){
setAddress(address);
setContactNumber(contactNumber);
setEmail(email);
}
public void setAddress(String address){
this.address = address;
}
public String getAddress(){
return address;
}
public void setEmail(String email){
this.email = email;
}
public String getEmail(){
return email;
}
public void setContactNumber(String contactNumber){
this.contactNumber = contactNumber;
}
public String getContactNumber(){
return contactNumber;
}
public String toString(){
return String.format("Address: "+getAddress()+"\nContact Number: "+getContactNumber()+
"\nEmail Address: "+getEmail());
}
}
Employee Class
public class Employee {
private Personal personal;
private Contact contact;
public Employee(Personal personal, Contact contact){
this.personal = personal;
this.contact = contact;
}
public void setFirstName(String firstName){
this.personal.setFirstName(firstName);
}
public String toString(){
return String.format(personal.toString()+contact.toString());
}
}
And the Test class
public class TestClass {
public static void main(String[] args){
Personal personalHerp = new Personal("John","M","Doe",18);
Contact contactHerp = new Contact("88 Herp Derp St U mad New york","724-15-70","fido.com");
Employee employeeHerp = new Employee(personalHerp,contactHerp);
System.out.println(employeeHerp);
}
}
Well, since Employee doesn't extend Personal it has a Personal and a Contact.
I guess you'd rather like Employee to be a Personal and thus it should look like this:
public class Employee extends Personal {
private Contact contact;
...
}
So to summarize:
is-a means a class/object extends another class or implements an interface, i.e. A is-a B if A extends B or A implements B
has-a means that a class/object has a variable of that type, like Contact contact in your Employee class, which means Employee has-a contact.
Yes, this is a "has-a" relationship (exactly as we discussed in your other question).