我不得不承认,我需要一些帮助。 我只有3周的Rest经验,2周的Grails经验和1周的Android Studio经验,所以我希望你不会对我太过刻苦。 我从grails服务器获取这个Json:
{"class":"medicserver.Medication",
"id":1,"dateOfPrescription":"2015-09-19T08:12:34Z",
"description":"Meds",
"medicationUnit":[{"class":"medicserver.MedicationUnit","id":1},{"class":"medicserver.MedicationUnit","id":2},{"class":"medicserver.MedicationUnit","id":3}],
"patient":null}
你看,属性“drugUnit”是一个MedicationUnit列表,这是我的ResponseObject创建时唯一没有的东西。我根本得不到它。 这是Rest-Code:
RestTemplate restTemplate = new RestTemplate();
MappingJackson2HttpMessageConverter mapping = new MappingJackson2HttpMessageConverter();
mapping.setObjectMapper(new ObjectMapper());
restTemplate.getMessageConverters().add(mapping);
Medication medication = restTemplate.getForObject(url , Medication.class);
return medication;
My Classes - plaese忽略静态类型。这是为了实现内部类:
@JsonIgnoreProperties(ignoreUnknown = true)
static class Medication{
String description;
MedicationUnit[] medicationUnit;
Date dateOfPrescription;
public Medication(){
}
public void setDescription(String description){
this.description = description;
}
public String getDescription(){
return this.description;
}
public void setMedicationUnit(MedicationUnit[] medicationUnit){
this.medicationUnit = medicationUnit;
}
public MedicationUnit[] getMedicationUnit(){
return this.medicationUnit;
}
public void setDateOfPrescription(Date dateOfPrescription){
this.dateOfPrescription = dateOfPrescription;
}
public Date getDateOfPrescription(){
return this.dateOfPrescription;
}
public String postAll(){
String result = this.description + "\n" + String.valueOf(this.dateOfPrescription) + "\n";
for(MedicationUnit med: this.medicationUnit){
result = result + med.postAll();
}
return result;
}
}
MedicationUnit:
@JsonIgnoreProperties(ignoreUnknown = true)
static class MedicationUnit {
String name="Hans";
Date intakeTime=new Date(0);
int dosage=3;
public MedicationUnit(){
}
public void setName(String name){
this.name=name;
}
public String getName(){
return this.name;
}
public void setIntakeTime(Date intakeTime){
this.intakeTime=intakeTime;
}
public Date getIntakeTime(){
return this.intakeTime;
}
public void setDosage(int dosage){
this.dosage=dosage;
}
public int getDosage(){
return this.dosage;
}
private String postAll(){
String result;
result = this.name + " " + String.valueOf(this.intakeTime) + " " + String.valueOf(dosage) + "\n";
return result;
}
}