Spring JPA - Data fetching selected columns - java

Am using Spring JPA repository
public String getNextULAID();
// Selecting particular columns from a table
#Query(value="select dbts,firstName from user_details
where ULA_ID= :ulaID and DEL_FLG = 'N'" ,nativeQuery=true)
public UserDetails fetchUser(#Param("ulaID")String ulaId);
}
Throws error
{
"timestamp": 1499336001602,
"status": 500,
"error": "Internal Server Error",
"exception": "org.springframework.dao.InvalidDataAccessResourceUsageException",
"message": "could not extract ResultSet; SQL [n/a];
nested exception is org.hibernate.exception.SQLGrammarException:
could not extract ResultSet",
"path": "/viewProfile/1"
}
But if I select all data from the database,then its working perfectly
public String getNextULAID();
// selecting all records
#Query(value="select * from user_details
where ULA_ID= :ulaID and DEL_FLG = 'N'" ,nativeQuery=true)
public UserDetails fetchUser(#Param("ulaID")String ulaId);
}
Here is my entity...
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
// End of user code
#Entity
#Table(name="user_details")
public class UserDetails {
/**
* Description of the property dbts.
*/
private Integer dbts;
/**
* Description of the property ulaId.
*/
#Id
private String ulaId = "";
/**
* Description of the property firstName.
*/
private String firstName = "";
/**
* Description of the property lastName.
*/
private String lastName = "";
/**
* Description of the property emailId.
*/
private String emailId = "";
/**
* Description of the property mobileNo.
*/
private String mobileNo = "";
/**
* Description of the property gender.
*/
private String gender = "";
/**
* Description of the property dateOfBirth.
*/
private String dateOfBirth = "";
/**
* Description of the property address1.
*/
private String address1 = "";
/**
* Description of the property address2.
*/
private String address2 = "";
/**
* Description of the property city.
*/
private String city = "";
/**
* Description of the property state.
*/
private String state = "";
/**
* Description of the property country.
*/
private String country = "";
/**
* Description of the property pincode.
*/
private String pincode = "";
/**
* Description of the property profileGroup.
*/
private String profileGroup = "";
/**
* Description of the property delFlg.
*/
private String delFlg = "";
/**
* Description of the property remarks.
*/
private String remarks = "";
/**
* Description of the property rCreTime.
*/
private String rCreTime = "";
/**
* Description of the property rModTime.
*/
private String rModTime = "";
// Start of user code (user defined attributes for UserDetails)
// End of user code
/**
* The constructor.
*/
public UserDetails() {
// Start of user code constructor for UserDetails)
super();
// End of user code
}
// Start of user code (user defined methods for UserDetails)
// End of user code
/**
* Returns dbts.
* #return dbts
*/
public Integer getDbts() {
return this.dbts;
}
/**
* Sets a value to attribute dbts.
* #param newDbts
*/
public void setDbts(Integer newDbts) {
this.dbts = newDbts;
}
/**
* Returns ulaId.
* #return ulaId
*/
public String getUlaId() {
return this.ulaId;
}
/**
* Sets a value to attribute ulaId.
* #param newUlaId
*/
public void setUlaId(String newUlaId) {
this.ulaId = newUlaId;
}
/**
* Returns firstName.
* #return firstName
*/
public String getFirstName() {
return this.firstName;
}
/**
* Sets a value to attribute firstName.
* #param newFirstName
*/
public void setFirstName(String newFirstName) {
this.firstName = newFirstName;
}
/**
* Returns lastName.
* #return lastName
*/
public String getLastName() {
return this.lastName;
}
/**
* Sets a value to attribute lastName.
* #param newLastName
*/
public void setLastName(String newLastName) {
this.lastName = newLastName;
}
/**
* Returns emailId.
* #return emailId
*/
public String getEmailId() {
return this.emailId;
}
/**
* Sets a value to attribute emailId.
* #param newEmailId
*/
public void setEmailId(String newEmailId) {
this.emailId = newEmailId;
}
/**
* Returns mobileNo.
* #return mobileNo
*/
public String getMobileNo() {
return this.mobileNo;
}
/**
* Sets a value to attribute mobileNo.
* #param newMobileNo
*/
public void setMobileNo(String newMobileNo) {
this.mobileNo = newMobileNo;
}
/**
* Returns gender.
* #return gender
*/
public String getGender() {
return this.gender;
}
/**
* Sets a value to attribute gender.
* #param newGender
*/
public void setGender(String newGender) {
this.gender = newGender;
}
/**
* Returns dateOfBirth.
* #return dateOfBirth
*/
public String getDateOfBirth() {
return this.dateOfBirth;
}
/**
* Sets a value to attribute dateOfBirth.
* #param newDateOfBirth
*/
public void setDateOfBirth(String newDateOfBirth) {
this.dateOfBirth = newDateOfBirth;
}
/**
* Returns address1.
* #return address1
*/
public String getAddress1() {
return this.address1;
}
/**
* Sets a value to attribute address1.
* #param newAddress1
*/
public void setAddress1(String newAddress1) {
this.address1 = newAddress1;
}
/**
* Returns address2.
* #return address2
*/
public String getAddress2() {
return this.address2;
}
/**
* Sets a value to attribute address2.
* #param newAddress2
*/
public void setAddress2(String newAddress2) {
this.address2 = newAddress2;
}
/**
* Returns city.
* #return city
*/
public String getCity() {
return this.city;
}
/**
* Sets a value to attribute city.
* #param newCity
*/
public void setCity(String newCity) {
this.city = newCity;
}
/**
* Returns state.
* #return state
*/
public String getState() {
return this.state;
}
/**
* Sets a value to attribute state.
* #param newState
*/
public void setState(String newState) {
this.state = newState;
}
/**
* Returns country.
* #return country
*/
public String getCountry() {
return this.country;
}
/**
* Sets a value to attribute country.
* #param newCountry
*/
public void setCountry(String newCountry) {
this.country = newCountry;
}
/**
* Returns pincode.
* #return pincode
*/
public String getPincode() {
return this.pincode;
}
/**
* Sets a value to attribute pincode.
* #param newPincode
*/
public void setPincode(String newPincode) {
this.pincode = newPincode;
}
/**
* Returns profileGroup.
* #return profileGroup
*/
public String getProfileGroup() {
return this.profileGroup;
}
/**
* Sets a value to attribute profileGroup.
* #param newProfileGroup
*/
public void setProfileGroup(String newProfileGroup) {
this.profileGroup = newProfileGroup;
}
/**
* Returns delFlg.
* #return delFlg
*/
public String getDelFlg() {
return this.delFlg;
}
/**
* Sets a value to attribute delFlg.
* #param newDelFlg
*/
public void setDelFlg(String newDelFlg) {
this.delFlg = newDelFlg;
}
/**
* Returns remarks.
* #return remarks
*/
public String getRemarks() {
return this.remarks;
}
/**
* Sets a value to attribute remarks.
* #param newRemarks
*/
public void setRemarks(String newRemarks) {
this.remarks = newRemarks;
}
/**
* Returns rCreTime.
* #return rCreTime
*/
public String getRCreTime() {
return this.rCreTime;
}
/**
* Sets a value to attribute rCreTime.
* #param newRCreTime
*/
public void setRCreTime(String newRCreTime) {
this.rCreTime = newRCreTime;
}
/**
* Returns rModTime.
* #return rModTime
*/
public String getRModTime() {
return this.rModTime;
}
/**
* Sets a value to attribute rModTime.
* #param newRModTime
*/
public void setRModTime(String newRModTime) {
this.rModTime = newRModTime;
}
}
What changes i have to do, to get specific columns from multiple data.
Thanks in advance.

You should return List<Object[]> instead of UserDetails in case of native query.
Another way is to create projection. You can use projections from Spring Data JPA http://docs.spring.io/spring-data/jpa/docs/current/reference/html/#projections.

Related

XML to Object mapping

Having hard time to map..
Calling rest and rest return response in XML format..
Help with class mapping in Java Object (method)
<aname>
<bname>
<c id="2">
<cname call="yes" text="no" email="yes"/>
<acc>
<accinfo name="ax" addr="USA"/>
</acc>
</c>
</bname>
</aname>
how can i convert above xml in Java class?
Thank You!
I share solution:
If you use need dependency
<!-- Jaxb -->
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
</dependency>
First class.
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlSchemaType;
import javax.xml.bind.annotation.XmlType;
#XmlAccessorType(XmlAccessType.FIELD)
#XmlType(name = "", propOrder = { "bname" })
#XmlRootElement(name = "aname")
public class Aname {
#XmlElement(required = true)
protected Aname.Bname bname;
/**
* Gets the value of the bname property.
*
* #return possible object is {#link Aname.Bname }
*
*/
public Aname.Bname getBname() {
return bname;
}
/**
* Sets the value of the bname property.
*
* #param value allowed object is {#link Aname.Bname }
*
*/
public void setBname(Aname.Bname value) {
this.bname = value;
}
#XmlAccessorType(XmlAccessType.FIELD)
#XmlType(name = "", propOrder = { "c" })
public static class Bname {
#XmlElement(required = true)
protected Aname.Bname.C c;
/**
* Gets the value of the c property.
*
* #return possible object is {#link Aname.Bname.C }
*
*/
public Aname.Bname.C getC() {
return c;
}
/**
* Sets the value of the c property.
*
* #param value allowed object is {#link Aname.Bname.C }
*
*/
public void setC(Aname.Bname.C value) {
this.c = value;
}
#XmlAccessorType(XmlAccessType.FIELD)
#XmlType(name = "", propOrder = { "cname", "acc" })
public static class C {
#XmlElement(required = true)
protected Aname.Bname.C.Cname cname;
#XmlElement(required = true)
protected Aname.Bname.C.Acc acc;
#XmlAttribute(name = "id", required = true)
#XmlSchemaType(name = "unsignedByte")
protected short id;
/**
* Gets the value of the cname property.
*
* #return possible object is {#link Aname.Bname.C.Cname }
*
*/
public Aname.Bname.C.Cname getCname() {
return cname;
}
/**
* Sets the value of the cname property.
*
* #param value allowed object is {#link Aname.Bname.C.Cname }
*
*/
public void setCname(Aname.Bname.C.Cname value) {
this.cname = value;
}
/**
* Gets the value of the acc property.
*
* #return possible object is {#link Aname.Bname.C.Acc }
*
*/
public Aname.Bname.C.Acc getAcc() {
return acc;
}
/**
* Sets the value of the acc property.
*
* #param value allowed object is {#link Aname.Bname.C.Acc }
*
*/
public void setAcc(Aname.Bname.C.Acc value) {
this.acc = value;
}
/**
* Gets the value of the id property.
*
*/
public short getId() {
return id;
}
/**
* Sets the value of the id property.
*
*/
public void setId(short value) {
this.id = value;
}
#XmlAccessorType(XmlAccessType.FIELD)
#XmlType(name = "", propOrder = { "accinfo" })
public static class Acc {
#XmlElement(required = true)
protected Aname.Bname.C.Acc.Accinfo accinfo;
/**
* Gets the value of the accinfo property.
*
* #return possible object is {#link Aname.Bname.C.Acc.Accinfo }
*
*/
public Aname.Bname.C.Acc.Accinfo getAccinfo() {
return accinfo;
}
/**
* Sets the value of the accinfo property.
*
* #param value allowed object is {#link Aname.Bname.C.Acc.Accinfo }
*
*/
public void setAccinfo(Aname.Bname.C.Acc.Accinfo value) {
this.accinfo = value;
}
#XmlAccessorType(XmlAccessType.FIELD)
#XmlType(name = "")
public static class Accinfo {
#XmlAttribute(name = "name", required = true)
protected String name;
#XmlAttribute(name = "addr", required = true)
protected String addr;
/**
* Gets the value of the name property.
*
* #return possible object is {#link String }
*
*/
public String getName() {
return name;
}
/**
* Sets the value of the name property.
*
* #param value allowed object is {#link String }
*
*/
public void setName(String value) {
this.name = value;
}
/**
* Gets the value of the addr property.
*
* #return possible object is {#link String }
*
*/
public String getAddr() {
return addr;
}
/**
* Sets the value of the addr property.
*
* #param value allowed object is {#link String }
*
*/
public void setAddr(String value) {
this.addr = value;
}
}
}
#XmlAccessorType(XmlAccessType.FIELD)
#XmlType(name = "")
public static class Cname {
#XmlAttribute(name = "call", required = true)
protected String call;
#XmlAttribute(name = "text", required = true)
protected String text;
#XmlAttribute(name = "email", required = true)
protected String email;
/**
* Gets the value of the call property.
*
* #return possible object is {#link String }
*
*/
public String getCall() {
return call;
}
/**
* Sets the value of the call property.
*
* #param value allowed object is {#link String }
*
*/
public void setCall(String value) {
this.call = value;
}
/**
* Gets the value of the text property.
*
* #return possible object is {#link String }
*
*/
public String getText() {
return text;
}
/**
* Sets the value of the text property.
*
* #param value allowed object is {#link String }
*
*/
public void setText(String value) {
this.text = value;
}
/**
* Gets the value of the email property.
*
* #return possible object is {#link String }
*
*/
public String getEmail() {
return email;
}
/**
* Sets the value of the email property.
*
* #param value allowed object is {#link String }
*
*/
public void setEmail(String value) {
this.email = value;
}
}
}
}
}
Second class.
import javax.xml.bind.annotation.XmlRegistry;
#XmlRegistry
public class ObjectFactory {
/**
* Create a new ObjectFactory that can be used to create new instances of schema
* derived classes for package: generated
*
*/
public ObjectFactory() {
}
/**
* Create an instance of {#link Aname }
*
*/
public Aname createAname() {
return new Aname();
}
/**
* Create an instance of {#link Aname.Bname }
*
*/
public Aname.Bname createAnameBname() {
return new Aname.Bname();
}
/**
* Create an instance of {#link Aname.Bname.C }
*
*/
public Aname.Bname.C createAnameBnameC() {
return new Aname.Bname.C();
}
/**
* Create an instance of {#link Aname.Bname.C.Acc }
*
*/
public Aname.Bname.C.Acc createAnameBnameCAcc() {
return new Aname.Bname.C.Acc();
}
/**
* Create an instance of {#link Aname.Bname.C.Cname }
*
*/
public Aname.Bname.C.Cname createAnameBnameCCname() {
return new Aname.Bname.C.Cname();
}
/**
* Create an instance of {#link Aname.Bname.C.Acc.Accinfo }
*
*/
public Aname.Bname.C.Acc.Accinfo createAnameBnameCAccAccinfo() {
return new Aname.Bname.C.Acc.Accinfo();
}
}
And Main class.
import java.io.StringReader;
import java.text.ParseException;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class Test {
static ObjectMapper mapper = new ObjectMapper();
public static void main(String... strings) throws ParseException {
String xml = "<aname> <bname> <c id=\"2\"> <cname call=\"yes\" text=\"no\" email=\"yes\"/> <acc> <accinfo name=\"ax\" addr=\"USA\"/> </acc> </c> </bname> </aname>";
Aname clazz = convertXMLToObject(Aname.class, xml);
try {
System.out.println(mapper.writeValueAsString(clazz));
} catch (JsonProcessingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
#SuppressWarnings("unchecked")
public static <T> T convertXMLToObject(Class<T> clazz, String xml)
{
try {
JAXBContext context = JAXBContext.newInstance(clazz);
Unmarshaller um = context.createUnmarshaller();
return (T) um.unmarshal(new StringReader(xml));
}
catch (JAXBException je){
throw new RuntimeException(String.format("Exception while Unmarshaller: %s", je.getMessage()));
}
}
}
result this.
{"bname":{"c":{"cname":{"call":"yes","text":"no","email":"yes"},"acc":{"accinfo":{"name":"ax","addr":"USA"}},"id":2}}}

"No suitable constructor found for type" and the type exists and has a default constructor

I am receiving this message in a new jersey 1.19.4 app that I am building:
No suitable constructor found for type [simple type, class javax.xml.bind.JAXBElement<com.objex.idms.idd.types.IDDModule>]: can not instantiate from JSON object (need to add/enable type information?)
at [Source: org.apache.catalina.connector.CoyoteInputStream#5a5e4ec1; line: 1, column: 2]
After much searching I have seen many posts that indicate this can be caused by a missing default constructor. However that doesn't seem to the the case here. Note I have tried this implementing "Serializable" with the same result. I am sure it's something simple but am just missing it.
Here is the class in question, any thoughts you might have would be appreciated.
package com.objex.idms.idd.types;
// import java.io.Serializable;
public class IDDModule {
/**
*
*/
//private static final long serialVersionUID = 1897236478938746827L;
private String DictName;
private String DictNode;
private String Name;
private String Version;
private String Language;
private String ModuleMetadata;
private String ModuleSource;
private String Message;
public IDDModule() {
}
/**
* #param dictName the dictName to set
*/
public void setDictName(String DictName) {
this.DictName = DictName;
}
/**
* #return the dictName
*/
public String getDictName() {
return DictName;
}
/**
* #param DictNode the dictNode to set
*/
public void setDictNode(String DictNode) {
this.DictNode = DictNode;
}
/**
* #return the dictNode
*/
public String getDictNode() {
return DictNode;
}
/**
* #param language the language to set
*/
public void setLanguage(String language) {
this.Language = language;
}
/**
* #return the language
*/
public String getLanguage() {
return Language;
}
/**
* #param Message the message to set
*/
public void setMessage(String Message) {
this.Message = Message;
}
/**
* #return the message
*/
public String getMessage() {
return Message;
}
/**
* #param ModuleMetadata the moduleMetadata to set
*/
public void setModuleMetadata(String ModuleMetadata) {
this.ModuleMetadata = ModuleMetadata;
}
/**
* #return the ModuleMetadata
*/
public String getModuleMetadata() {
return ModuleMetadata;
}
/**
* #param ModuleSource the moduleSource to set
*/
public void setModuleSource(String ModuleSource) {
this.ModuleSource = ModuleSource;
}
/**
* #return the moduleSource
*/
public String getModuleSource() {
return ModuleSource;
}
/**
* #param name the name to set
*/
public void setName(String name) {
this.Name = name;
}
/**
* #return the name
*/
public String getName() {
return Name;
}
/**
* #param version the version to set
*/
public void setVersion(String version) {
this.Version = version;
}
/**
* #return the version
*/
public String getVersion() {
return Version;
}
}

Spring Form nested object Boolean field is not getting resolved

I have an Employee class which has PersonalDetails class object as a member
public class Employee {
private long empId;
private PersonalDetails personalDetails = new PersonalDetails();
// getters setters
PersonalDetails class is as follows
/**
* Class represents personal details of an employee
*
* <BR>
* <BR>
* <B>Supported API: </B>false <BR>
* <BR>
* <B>Extendable: </B>false
*/
public class PersonalDetails {
private String empfirstName, empMiddleName, empLastName, gender, birthPlace, marritalStatus,
longMedicalTreatmentDescription, suffix, ethnicity, veteranStatus, prefFirstName, preLastName, birthCountry,
citizenship, citizenshipCountry1, citizenshipCountry2, visaType, email;
private Address permanentAddress, presentAddress;
private OtherContactDetails emergencyContactDetails;
private Date dob, visaExp;
private int age, noOfChildrens, healthVision;
private Boolean hasPhysicalDisability, hadLongMedicalTreatment, hasFourWheelLiscence, isPresentAddressSame;
private Passport passportDetails;
private FatherOrHusband fatherOrHusbandDetails;
/**
* #return the empfirstName
*/
public String getEmpfirstName() {
return empfirstName;
}
/**
* #param empfirstName
* the empfirstName to set
*/
public void setEmpfirstName(String empfirstName) {
this.empfirstName = empfirstName;
}
/**
* #return the empMiddleName
*/
public String getEmpMiddleName() {
return empMiddleName;
}
/**
* #param empMiddleName
* the empMiddleName to set
*/
public void setEmpMiddleName(String empMiddleName) {
this.empMiddleName = empMiddleName;
}
/**
* #return the empLastName
*/
public String getEmpLastName() {
return empLastName;
}
/**
* #param empLastName
* the empLastName to set
*/
public void setEmpLastName(String empLastName) {
this.empLastName = empLastName;
}
/**
* #return the gender
*/
public String getGender() {
return gender;
}
/**
* #param gender
* the gender to set
*/
public void setGender(String gender) {
this.gender = gender;
}
/**
* #return the birthPlace
*/
public String getBirthPlace() {
return birthPlace;
}
/**
* #param birthPlace
* the birthPlace to set
*/
public void setBirthPlace(String birthPlace) {
this.birthPlace = birthPlace;
}
/**
* #return the marritalStatus
*/
public String getMarritalStatus() {
return marritalStatus;
}
/**
* #param marritalStatus
* the marritalStatus to set
*/
public void setMarritalStatus(String marritalStatus) {
this.marritalStatus = marritalStatus;
}
/**
* #return the longMedicalTreatmentDescription
*/
public String getLongMedicalTreatmentDescription() {
return longMedicalTreatmentDescription;
}
/**
* #param longMedicalTreatmentDescription
* the longMedicalTreatmentDescription to set
*/
public void setLongMedicalTreatmentDescription(String longMedicalTreatmentDescription) {
this.longMedicalTreatmentDescription = longMedicalTreatmentDescription;
}
/**
* #return the permanentAddress
*/
public Address getPermanentAddress() {
return permanentAddress;
}
/**
* #param permanentAddress
* the permanentAddress to set
*/
public void setPermanentAddress(Address permanentAddress) {
this.permanentAddress = permanentAddress;
}
/**
* #return the presentAddress
*/
public Address getPresentAddress() {
return presentAddress;
}
/**
* #param presentAddress
* the presentAddress to set
*/
public void setPresentAddress(Address presentAddress) {
this.presentAddress = presentAddress;
}
/**
* #return the dob
*/
public Date getDob() {
return dob;
}
/**
* #param dob
* the dob to set
*/
public void setDob(Date dob) {
this.dob = dob;
}
/**
* #return the age
*/
public int getAge() {
return age;
}
/**
* #param age
* the age to set
*/
public void setAge(int age) {
this.age = age;
}
/**
* #return the noOfChildrens
*/
public int getNoOfChildrens() {
return noOfChildrens;
}
/**
* #param noOfChildrens
* the noOfChildrens to set
*/
public void setNoOfChildrens(int noOfChildrens) {
this.noOfChildrens = noOfChildrens;
}
/**
* #return the healthVision
*/
public int getHealthVision() {
return healthVision;
}
/**
* #param healthVision
* the healthVision to set
*/
public void setHealthVision(int healthVision) {
this.healthVision = healthVision;
}
/**
* #return the passportDetails
*/
public Passport getPassportDetails() {
return passportDetails;
}
/**
* #param passportDetails
* the passportDetails to set
*/
public void setPassportDetails(Passport passportDetails) {
this.passportDetails = passportDetails;
}
/**
* #return the suffix
*/
public String getSuffix() {
return suffix;
}
/**
* #param suffix
* the suffix to set
*/
public void setSuffix(String suffix) {
this.suffix = suffix;
}
/**
* #return the fatherOrHusbandDetails
*/
public FatherOrHusband getFatherOrHusbandDetails() {
return fatherOrHusbandDetails;
}
/**
* #param fatherOrHusbandDetails
* the fatherOrHusbandDetails to set
*/
public void setFatherOrHusbandDetails(FatherOrHusband fatherOrHusbandDetails) {
this.fatherOrHusbandDetails = fatherOrHusbandDetails;
}
/**
* #return the hasPhysicalDisability
*/
public Boolean isHasPhysicalDisability() {
return hasPhysicalDisability;
}
/**
* #param hasPhysicalDisability
* the hasPhysicalDisability to set
*/
public void setHasPhysicalDisability(Boolean hasPhysicalDisability) {
this.hasPhysicalDisability = hasPhysicalDisability;
}
/**
* #return the hadLongMedicalTreatment
*/
public Boolean isHadLongMedicalTreatment() {
return hadLongMedicalTreatment;
}
/**
* #param hadLongMedicalTreatment
* the hadLongMedicalTreatment to set
*/
public void setHadLongMedicalTreatment(Boolean hadLongMedicalTreatment) {
this.hadLongMedicalTreatment = hadLongMedicalTreatment;
}
/**
* #return the hasFourWheelLiscence
*/
public Boolean isHasFourWheelLiscence() {
return hasFourWheelLiscence;
}
/**
* #param hasFourWheelLiscence
* the hasFourWheelLiscence to set
*/
public void setHasFourWheelLiscence(Boolean hasFourWheelLiscence) {
this.hasFourWheelLiscence = hasFourWheelLiscence;
}
/**
* #return the isPresentAddressSame
*/
public Boolean isPresentAddressSame() {
return isPresentAddressSame;
}
/**
* #param isPresentAddressSame
* the isPresentAddressSame to set
*/
public void setPresentAddressSame(Boolean isPresentAddressSame) {
if (isPresentAddressSame) {
presentAddress = permanentAddress;
}
this.isPresentAddressSame = isPresentAddressSame;
}
/**
* #return the ethnicity
*/
public String getEthnicity() {
return ethnicity;
}
/**
* #param ethnicity
* the ethnicity to set
*/
public void setEthnicity(String ethnicity) {
this.ethnicity = ethnicity;
}
/**
* #return the veteranStatus
*/
public String getVeteranStatus() {
return veteranStatus;
}
/**
* #param veteranStatus
* the veteranStatus to set
*/
public void setVeteranStatus(String veteranStatus) {
this.veteranStatus = veteranStatus;
}
/**
* #return the prefFirstName
*/
public String getPrefFirstName() {
return prefFirstName;
}
/**
* #param prefFirstName
* the prefFirstName to set
*/
public void setPrefFirstName(String prefFirstName) {
this.prefFirstName = prefFirstName;
}
/**
* #return the preLastName
*/
public String getPreLastName() {
return preLastName;
}
/**
* #param preLastName
* the preLastName to set
*/
public void setPreLastName(String preLastName) {
this.preLastName = preLastName;
}
/**
* #return the birthCountry
*/
public String getBirthCountry() {
return birthCountry;
}
/**
* #param birthCountry
* the birthCountry to set
*/
public void setBirthCountry(String birthCountry) {
this.birthCountry = birthCountry;
}
/**
* #return the citizenship
*/
public String getCitizenship() {
return citizenship;
}
/**
* #param citizenship
* the citizenship to set
*/
public void setCitizenship(String citizenship) {
this.citizenship = citizenship;
}
/**
* #return the citizenshipCountry1
*/
public String getCitizenshipCountry1() {
return citizenshipCountry1;
}
/**
* #param citizenshipCountry1
* the citizenshipCountry1 to set
*/
public void setCitizenshipCountry1(String citizenshipCountry1) {
this.citizenshipCountry1 = citizenshipCountry1;
}
/**
* #return the citizenshipCountry2
*/
public String getCitizenshipCountry2() {
return citizenshipCountry2;
}
/**
* #param citizenshipCountry2
* the citizenshipCountry2 to set
*/
public void setCitizenshipCountry2(String citizenshipCountry2) {
this.citizenshipCountry2 = citizenshipCountry2;
}
/**
* #return the visaType
*/
public String getVisaType() {
return visaType;
}
/**
* #param visaType
* the visaType to set
*/
public void setVisaType(String visaType) {
this.visaType = visaType;
}
/**
* #return the emergencyContactDetails
*/
public OtherContactDetails getEmergencyContactDetails() {
return emergencyContactDetails;
}
/**
* #param emergencyContactDetails
* the emergencyContactDetails to set
*/
public void setEmergencyContactDetails(OtherContactDetails emergencyContactDetails) {
this.emergencyContactDetails = emergencyContactDetails;
}
/**
* #return the visaExp
*/
public Date getVisaExp() {
return visaExp;
}
/**
* #param visaExp
* the visaExp to set
*/
public void setVisaExp(Date visaExp) {
this.visaExp = visaExp;
}
/**
* #return the email
*/
public String getEmail() {
return email;
}
/**
* #param email
* the email to set
*/
public void setEmail(String email) {
this.email = email;
}
/*
* (non-Javadoc)
*
* #see java.lang.Object#toString()
*/
#Override
public String toString() {
return "PersonalDetails [empfirstName=" + empfirstName + ", empMiddleName=" + empMiddleName + ", empLastName="
+ empLastName + ", gender=" + gender + ", birthPlace=" + birthPlace + ", marritalStatus="
+ marritalStatus + ", longMedicalTreatmentDescription=" + longMedicalTreatmentDescription + ", suffix="
+ suffix + ", ethnicity=" + ethnicity + ", veteranStatus=" + veteranStatus + ", prefFirstName="
+ prefFirstName + ", preLastName=" + preLastName + ", birthCountry=" + birthCountry + ", citizenship="
+ citizenship + ", citizenshipCountry1=" + citizenshipCountry1 + ", citizenshipCountry2="
+ citizenshipCountry2 + ", visaType=" + visaType + ", email=" + email + ", permanentAddress="
+ permanentAddress + ", presentAddress=" + presentAddress + ", emergencyContactDetails="
+ emergencyContactDetails + ", dob=" + dob + ", visaExp=" + visaExp + ", age=" + age
+ ", noOfChildrens=" + noOfChildrens + ", healthVision=" + healthVision + ", hasPhysicalDisability="
+ hasPhysicalDisability + ", hadLongMedicalTreatment=" + hadLongMedicalTreatment
+ ", hasFourWheelLiscence=" + hasFourWheelLiscence + ", isPresentAddressSame=" + isPresentAddressSame
+ ", passportDetails=" + passportDetails + ", fatherOrHusbandDetails=" + fatherOrHusbandDetails + "]";
}
}
I am using spring form tags to create registration form and added code as follows
<label>
<form:checkbox path="personalDetails.isPresentAddressSame" />
Present address same as above
</label>
As I have a variable personalDetails and it has a field isPresentAddressSame it should match the getter setter but I am getting following error
There was an unexpected error (type=Internal Server Error, status=500).
Invalid property 'personalDetails.isPresentAddressSame' of bean class [com.hr.foundation.models.doc.models.employee.Employee]: Bean property 'personalDetails.isPresentAddressSame' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?
Other fields with type String are matching with the path like personalDetails.firstName but not this Boolean field
You did not include the getter/setters of the PersonalDetails class, but Spring says that's where the error is.
Make sure you have these:
boolean isPresentAddressSame() { ... }
and
void setPresentAddressSame(boolean value) { ... }
Your mistake is likely that either:
You used a Boolean in either the parameter or return type (note the capital B)
You used getPresentAddressame which is only allowed for Boolean (note capital B again) instead of isPresentAddressSame which you should use for boolean (small B).
Clarification
Change:
public Boolean isPresentAddressSame() {
return isPresentAddressSame;
}
public void setPresentAddressSame(Boolean isPresentAddressSame) {
if (isPresentAddressSame) {
presentAddress = permanentAddress;
}
this.isPresentAddressSame = isPresentAddressSame;
}
to:
public boolean isPresentAddressSame() {
return isPresentAddressSame;
}
public void setPresentAddressSame(boolean isPresentAddressSame) {
if (isPresentAddressSame) {
presentAddress = permanentAddress;
}
this.isPresentAddressSame = isPresentAddressSame;
}
Note again that the boolean here uses a small b.
Try to change the signature of the method:
public Boolean isPresentAddressSame() {
return isPresentAddressSame;
}
to
public Boolean getPresentAddressSame() {
return isPresentAddressSame;
}
if you are using spring, spring alwais looking for get word to resolve the beans properties.

Volley parsing Json response is null Android

I am trying to parse JSON Object using volley but it is giving all the values as null
I am using GsonRequest custom class to parse using GSON library
public class GsonRequest < T > extends JsonRequest < T > {
/** Charset for request. */
private static final String PROTOCOL_CHARSET = "utf-8";
/** Content type for request. */
private static final String PROTOCOL_CONTENT_TYPE = String.format("application/json; charset=%s", PROTOCOL_CHARSET);
private final Gson mGson;
//private final String mBody;
private final Class < T > clazz;
private final Listener < T > listener;
private final Map < String,
String > headers;
private final String mBody;
public GsonRequest(int method, String url, String body, Class < T > clazz, Map < String, String > headers, Response.Listener < T > listener, Response.ErrorListener errorListener) {
super(method, url, (body == null) ? null: body, listener, errorListener);
this.clazz = clazz;
this.mBody = body;
this.headers = headers;
this.listener = listener;
mGson = new Gson();
}#Override
public Map < String,
String > getHeaders() throws AuthFailureError {
return headers != null ? headers: super.getHeaders();
}
#Override
protected void deliverResponse(T response) {
listener.onResponse(response);
}
#Override
protected Response < T > parseNetworkResponse(NetworkResponse response) {
try {
String json = new String(response.data, HttpHeaderParser.parseCharset(response.headers));
return Response.success(mGson.fromJson(json, clazz), HttpHeaderParser.parseCacheHeaders(response));
} catch(UnsupportedEncodingException e) {
return Response.error(new ParseError(e));
} catch(JsonSyntaxException e) {
Log.e("JsonSyntaxException", " " + e);
return Response.error(new ParseError(e));
}
}
}
I have created model class using http://www.jsonschema2pojo.org/ below is my model class
public class ModelConnection {
private List < Datum > data = new ArrayList < Datum > ();
private Integer code;
private Object message;
private Map < String,
Object > additionalProperties = new HashMap < String,
Object > ();
/**
*
* #return
* The data
*/
public List < Datum > getData() {
return data;
}
/**
*
* #param data
* The Data
*/
public void setData(List < Datum > data) {
this.data = data;
}
/**
*
* #return
* The code
*/
public Integer getCode() {
return code;
}
/**
*
* #param code
* The Code
*/
public void setCode(Integer code) {
this.code = code;
}
/**
*
* #return
* The message
*/
public Object getMessage() {
return message;
}
/**
*
* #param message
* The Message
*/
public void setMessage(Object message) {
this.message = message;
}
public Map < String,
Object > getAdditionalProperties() {
return this.additionalProperties;
}
public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}
public class Datum {
private Integer userID;
private String firstName;
private String lastName;
private String title;
private String organization;
private String industry;
private String location;
private String profileSummary;
private String imagePath;
private List < Object > interests = new ArrayList < Object > ();
private Map < String,
Object > additionalProperties = new HashMap < String,
Object > ();
/**
*
* #return
* The userID
*/
public Integer getUserID() {
return userID;
}
/**
*
* #param userID
* The UserID
*/
public void setUserID(Integer userID) {
this.userID = userID;
}
/**
*
* #return
* The firstName
*/
public String getFirstName() {
return firstName;
}
/**
*
* #param firstName
* The FirstName
*/
public void setFirstName(String firstName) {
this.firstName = firstName;
}
/**
*
* #return
* The lastName
*/
public String getLastName() {
return lastName;
}
/**
*
* #param lastName
* The LastName
*/
public void setLastName(String lastName) {
this.lastName = lastName;
}
/**
*
* #return
* The title
*/
public String getTitle() {
return title;
}
/**
*
* #param title
* The Title
*/
public void setTitle(String title) {
this.title = title;
}
/**
*
* #return
* The organization
*/
public String getOrganization() {
return organization;
}
/**
*
* #param organization
* The Organization
*/
public void setOrganization(String organization) {
this.organization = organization;
}
/**
*
* #return
* The industry
*/
public String getIndustry() {
return industry;
}
/**
*
* #param industry
* The Industry
*/
public void setIndustry(String industry) {
this.industry = industry;
}
/**
*
* #return
* The location
*/
public String getLocation() {
return location;
}
/**
*
* #param location
* The Location
*/
public void setLocation(String location) {
this.location = location;
}
/**
*
* #return
* The profileSummary
*/
public String getProfileSummary() {
return profileSummary;
}
/**
*
* #param profileSummary
* The ProfileSummary
*/
public void setProfileSummary(String profileSummary) {
this.profileSummary = profileSummary;
}
/**
*
* #return
* The imagePath
*/
public String getImagePath() {
return imagePath;
}
/**
*
* #param imagePath
* The ImagePath
*/
public void setImagePath(String imagePath) {
this.imagePath = imagePath;
}
/**
*
* #return
* The interests
*/
public List < Object > getInterests() {
return interests;
}
/**
*
* #param interests
* The Interests
*/
public void setInterests(List < Object > interests) {
this.interests = interests;
}
public Map < String,
Object > getAdditionalProperties() {
return this.additionalProperties;
}
public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}
}
}
This is the response from rest client
{
-"Data": [2]
-0: {
"UserID": 124
"FirstName": "Mixer"
"LastName": "Development"
"Title": "Flipdex Architect "
"Organization": "CoStrategix"
"Industry": "Software "
"Location": "Bengaluru Area, India"
"ProfileSummary": "Mixer#^*+~|Software engineer?????????? 123456789"#&$)(;:/-.,?!ASDFZgZhXjZkZlZXCVZbNZmQWERTYUIOZp[]{}#%^*+¥£€><~|\_.,?! "
"ImagePath": "https://media.licdn.com/mpr/mprx/0_tiUBxkxpFtDhAIKrczfJBnzj6FMhlWiAOiiJJAUpF8YTlJayP3DBA3Mp5NrycIrrczfJ48nymk-3-DwljKYwBKBKIk-8-DErYKYNylCgh5F24Rlu-3HVpLuuwAHKUDj3c1VURiTsxsU"
"Interests": [0]
}
-1: {
"UserID": 153
"FirstName": "Mixer"
"LastName": "Android"
"Title": "Assistant Manager at CoStrategix"
"Organization": "CoStrategix"
"Industry": "Software"
"Location": "Bengaluru Area, India"
"ProfileSummary": "We have worked with over 35+ product companies and bring the critical thinking and technology expertise to launching your product. We have l"
"ImagePath": "https://media.licdn.com/mpr/mprx/0_0EwKKqh9nkV0X1t3pmPYj6B9ncH0T_TTJy0K9h29KnTYT1u8zElOR_C9q3zGb1gDJyjY4_SnOQUOGFf8zJmuZhhsUQUxGFHTzJmPP3zBKbm1HA1jMe4j1vRQR1T7wFxKysoyq1W3CaQ"
"Interests": [0]
}
"Code": 1
"Message": null
}
finally this is the way I am calling the api
GsonRequest request = new GsonRequest(Request.Method.GET, newConnectionAPI,null, ModelConnection.class, getHeaders(),
new Response.Listener<ModelConnection>() {
#Override
public void onResponse(ModelConnection response) {
if (listener != null) {
listener.networkResponseSuccessful(response);
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.d("volley error", "" + error);
}
});
request.setTag(tag);
request.setRetryPolicy(new DefaultRetryPolicy(15000, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
requestQueue.add(request);
when I debug I get all the values as null
Can anyone please help me out
Thought of answering my own question, may be someone can get help through this...
I created my Model class using jsonschema2pojo where I had to select JSON along with GSON(Which I forgot to select).
you can see the difference if you compare the Model classes posted above and below
public class ModelConnection {
#SerializedName("Data")#Expose
private List < Datum > data = new ArrayList < Datum > ();#SerializedName("Code")#Expose
private Integer code;#SerializedName("Message")#Expose
private Object message;
private Map < String,
Object > additionalProperties = new HashMap < String,
Object > ();
/**
*
* #return
* The data
*/
public List < Datum > getData() {
return data;
}
/**
*
* #param data
* The Data
*/
public void setData(List < Datum > data) {
this.data = data;
}
/**
*
* #return
* The code
*/
public Integer getCode() {
return code;
}
/**
*
* #param code
* The Code
*/
public void setCode(Integer code) {
this.code = code;
}
/**
*
* #return
* The message
*/
public Object getMessage() {
return message;
}
/**
*
* #param message
* The Message
*/
public void setMessage(Object message) {
this.message = message;
}
public Map < String,
Object > getAdditionalProperties() {
return this.additionalProperties;
}
public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}
public class Datum implements Serializable {
#SerializedName("UserId")#Expose
private Integer userID;#SerializedName("FirstName")#Expose
private String firstName;#SerializedName("LastName")#Expose
private String lastName;#SerializedName("Title")#Expose
private String title;#SerializedName("Organization")#Expose
private String organization;#SerializedName("Industry")#Expose
private String industry;#SerializedName("Location")#Expose
private String location;#SerializedName("ProfileSummary")#Expose
private String profileSummary;#SerializedName("ImagePath")#Expose
private String imagePath;#SerializedName("Interests")#Expose
private ArrayList < Interest > interest = new ArrayList < Interest > ();
private Map < String,
Object > additionalProperties = new HashMap < String,
Object > ();
/**
*
* #return
* The userID
*/
public Integer getUserID() {
return userID;
}
/**
*
* #param userID
* The UserID
*/
public void setUserID(Integer userID) {
this.userID = userID;
}
/**
*
* #return
* The firstName
*/
public String getFirstName() {
return firstName;
}
/**
*
* #param firstName
* The FirstName
*/
public void setFirstName(String firstName) {
this.firstName = firstName;
}
/**
*
* #return
* The lastName
*/
public String getLastName() {
return lastName;
}
/**
*
* #param lastName
* The LastName
*/
public void setLastName(String lastName) {
this.lastName = lastName;
}
/**
*
* #return
* The title
*/
public String getTitle() {
return title;
}
/**
*
* #param title
* The Title
*/
public void setTitle(String title) {
this.title = title;
}
/**
*
* #return
* The organization
*/
public String getOrganization() {
return organization;
}
/**
*
* #param organization
* The Organization
*/
public void setOrganization(String organization) {
this.organization = organization;
}
/**
*
* #return
* The industry
*/
public String getIndustry() {
return industry;
}
/**
*
* #param industry
* The Industry
*/
public void setIndustry(String industry) {
this.industry = industry;
}
/**
*
* #return
* The location
*/
public String getLocation() {
return location;
}
/**
*
* #param location
* The Location
*/
public void setLocation(String location) {
this.location = location;
}
/**
*
* #return
* The profileSummary
*/
public String getProfileSummary() {
return profileSummary;
}
/**
*
* #param profileSummary
* The ProfileSummary
*/
public void setProfileSummary(String profileSummary) {
this.profileSummary = profileSummary;
}
/**
*
* #return
* The imagePath
*/
public String getImagePath() {
return imagePath;
}
/**
*
* #param imagePath
* The ImagePath
*/
public void setImagePath(String imagePath) {
this.imagePath = imagePath;
}
/**
*
* #return
* The interests
*/
public ArrayList < Interest > getInterest() {
return interest;
}
/**
*
* #param interests
* The Interests
*/
public void setInterest(ArrayList < Interest > interests) {
this.interest = interests;
}
public Map < String,
Object > getAdditionalProperties() {
return this.additionalProperties;
}
public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}
}
public class Interest {
#SerializedName("Name")#Expose
private String name;
/**
*
* #return
* The name
*/
public String getName() {
return name;
}
/**
*
* #param name
* The Name
*/
public void setName(String name) {
this.name = name;
}
}
}
May be because the objects were not properly serialized so GSON was unable to parse the JSON String properly.
Others advice over the above assumption is most welcome.

Java How create a set of object with two classes?

I have a logical problem to understand, how create a private set of inhabitant object. Here my two classes:
package main;
import java.util.Set;
/**
* #author
* #version 0.1
*
*/
public class City {
private String name;
/**
*HERE is my Problem*/
private Set<Inhabitants> Inhabitants {
}
/**
* standard cunstructor
*/
public City(String n, ) {
setName(n);
}
/**
* Search a particular name of inhabitant
* #return object of inhabitant
*/
static Set<Inhabitants> Search(){
return inhabitants;
}
/**
* Creates a inhabitant object and
* add to the set of inhabitants of the city
*
* #param name
* #param datebirth
* #param maritalStatus
* #
*/
public void Add(String name,String datebirth,String maritalStatus) {
}
/**
* Return all inhabitants objects of the city
* #return
*/
static Set<Inhabitants> ReturnAll(){
return inhabitants;
}
/**
* Return city name
* #return
*/
static Set<Inhabitants> ReturnCityName(){
return inhabitants;
}
/**
* #return the name
*/
public String getName() {
return name;
}
/**
* #param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* #return the inhabitants
*/
public Set<Inhabitants> getInhabitants() {
return inhabitants;
}
/**
* #param inhabitants the inhabitants to set
*/
public void setInhabitants(Set<Inhabitants> inhabitants) {
this.inhabitants = inhabitants;
}
}
package main;
/**
* #version 0.1
*/
public class Inhabitants {
private String name;
private String datebirth;
private Boolean maritalStatus;
/**
* standard constructor
*/
public Inhabitants() {
}
/**
* #return the name
*/
public String getName() {
return name;
}
/**
* #param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* #return the datebirth
*/
public String getDatebirth() {
return datebirth;
}
/**
* #param datebirth the datebirth to set
*/
public void setDatebirth(String datebirth) {
this.datebirth = datebirth;
}
/**
* #return the maritalStatus
*/
public Boolean getMaritalStatus() {
return maritalStatus;
}
/**
* #param maritalStatus the maritalStatus to set
*/
public void setMaritalStatus(Boolean maritalStatus) {
this.maritalStatus = maritalStatus;
}
}
It sounds like a simple problem. You didn't need so much code:
package collections;
/**
* Person
* #author Michael
* #link https://stackoverflow.com/questions/30958832/java-how-create-a-set-of-object-with-two-classes
* #since 6/20/2015 5:06 PM
*/
public class Person {
private final String name;
public Person(String name) {
if (name == null || name.trim().length() == 0) throw new IllegalArgumentException("name cannot be blank or null");
this.name = name;
}
public String getName() {
return name;
}
#Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Person person = (Person) o;
return name.equals(person.name);
}
#Override
public int hashCode() {
return name.hashCode();
}
#Override
public String toString() {
final StringBuilder sb = new StringBuilder("Person{");
sb.append("name='").append(name).append('\'');
sb.append('}');
return sb.toString();
}
}
package collections;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
/**
* City
* #author Michael
* #link https://stackoverflow.com/questions/30958832/java-how-create-a-set-of-object-with-two-classes
* #since 6/20/2015 5:06 PM
*/
public class City {
private Set<Person> inhabitants;
public City(Collection<Person> inhabitants) {
this.inhabitants = (inhabitants == null) ? new HashSet<Person>() : new HashSet<Person>(inhabitants);
}
public void addInhabitant(Person p) {
if (p != null) {
this.inhabitants.add(p);
}
}
#Override
public String toString() {
final StringBuilder sb = new StringBuilder("City{");
sb.append("inhabitants=").append(inhabitants);
sb.append('}');
return sb.toString();
}
}

Categories