I am using Spring to develop project and want to parse JSON data coming in string format to the controller. To show case the issue, i have written below small program.
Did goggling enough but no luck. Hoping to get answer on this site. Issue: Unable to parse the inner object i.e. A3PatientRecordStatusBean. Output of the program: MedicPatientRecordDataStatusBean [a3PatientRecordStatusBean=null]
Main program that tries to perform JSON parsing:
public static void main(String[] args) {
String jsonString = "{\"a3PatientRecordStatusBean\":{\"patientRecordId\":\"1\",\"messageCode\":\"2000\"}}";
ObjectMapper mapper = new ObjectMapper();
try {
MedicPatientRecordDataStatusBean medicPatientRecordDataStatusBean = mapper.readValue(jsonString, MedicPatientRecordDataStatusBean.class);
System.out.println(medicPatientRecordDataStatusBean);
} catch (IOException e) {
e.printStackTrace();
}
}
Outer class/Object:
@JsonAutoDetect
public class MedicPatientRecordDataStatusBean implements Serializable {
private static final long serialVersionUID = -4917476398283528449L;
private A3PatientRecordStatusBean a3PatientRecordStatusBean;
/**
* @return the a3PatientRecordStatusBean
*/
public A3PatientRecordStatusBean getA3PatientRecordStatusBean() {
return a3PatientRecordStatusBean;
}
/**
* @param a3PatientRecordStatusBean
* the a3PatientRecordStatusBean to set
*/
public void setA3PatientRecordStatusBean(
A3PatientRecordStatusBean a3PatientRecordStatusBean) {
a3PatientRecordStatusBean = a3PatientRecordStatusBean;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "MedicPatientRecordDataStatusBean [a3PatientRecordStatusBean="
+ a3PatientRecordStatusBean + "]";
}
}
Inner object class:
@JsonAutoDetect
public class A3PatientRecordStatusBean implements Serializable {
private static final long serialVersionUID = -4585669896170562832L;
private String patientRecordId = "";
private String messageCode = "";
/**
* @return the patientRecordId
*/
public String getPatientRecordId() {
return patientRecordId;
}
/**
* @param patientRecordId
* the patientRecordId to set
*/
public void setPatientRecordId(String patientRecordId) {
this.patientRecordId = patientRecordId;
}
/**
* @return the messageCode
*/
public String getMessageCode() {
return messageCode;
}
/**
* @param messageCode
* the messageCode to set
*/
public void setMessageCode(String messageCode) {
this.messageCode = messageCode;
}
@Override
public String toString() {
return "A3PatientRecordStatusBean [patientRecordId=" + patientRecordId
+ ", messageCode=" + messageCode + "]";
}
}