我有以下方法addAppearance(BasicInfo basicInfo)
private static Boolean addAppearance(BasicInfo basicInfo){
Boolean inserted = false;
GsonBuilder builder = new GsonBuilder();
Gson gson = builder.create();
String jsonObj = gson.toJson(basicInfo);
Client client = Client.create();
WebResource webResource = client
.resource("http://localhost:8080/Services/webapi/appearance");
ClientResponse clientResponse = webResource
.accept(MediaType.TEXT_PLAIN)
.type(MediaType.APPLICATION_JSON)
.post(ClientResponse.class, jsonObj.toString());
basicInfo = clientResponse.getEntity(BasicInfo.class);
if(clientResponse.getStatus()==200)
inserted = true;
return inserted;
}
在通过client.resource()访问url之后,我得到了一个Json响应
{
"appearanceId": 24,
"bodyType": "SLIM",
"healthInfo": "LOW_BP",
"height_cm": 101,
"height_feet": 3,
"height_inch": 8,
"skinTone": "VERY_FAIR",
"weight": 34
}
但我无法将该响应转换为BasicInfo类。
BasicInfo.java
public class BasicInfo {
private int appearanceId;
private int height_feet;
private int height_inch;
private int height_cm;
private int weight;
private String fullName;
private String maritalStatus;
private String bodyType;
private String healthInfo;
private String skinTone;
private String disability;
private String bloodgroup;
public String getBodyType() {
return bodyType;
}
public void setBodyType(String bodyType) {
this.bodyType = bodyType;
}
public int getAppearanceId() {
return appearanceId;
}
public void setAppearanceId(int appearanceId) {
this.appearanceId = appearanceId;
}
public int getHeight_feet() {
return height_feet;
}
public void setHeight_feet(int height_feet) {
this.height_feet = height_feet;
}
public int getHeight_inch() {
return height_inch;
}
public void setHeight_inch(int height_inch) {
this.height_inch = height_inch;
}
public int getHeight_cm() {
return height_cm;
}
public void setHeight_cm(int height_cm) {
this.height_cm = height_cm;
}
public int getWeight() {
return weight;
}
public void setWeight(int weight) {
this.weight = weight;
}
public String getFullName() {
return fullName;
}
public void setFullName(String fullName) {
this.fullName = fullName;
}
public String getMaritalStatus() {
return maritalStatus;
}
public void setMaritalStatus(String maritalStatus) {
this.maritalStatus = maritalStatus;
}
public String getHealthInfo() {
return healthInfo;
}
public void setHealthInfo(String healthInfo) {
this.healthInfo = healthInfo;
}
public String getSkinTone() {
return skinTone;
}
public void setSkinTone(String skinTone) {
this.skinTone = skinTone;
}
public String getDisability() {
return disability;
}
public void setDisability(String disability) {
this.disability = disability;
}
public String getBloodgroup() {
return bloodgroup;
}
public void setBloodgroup(String bloodgroup) {
this.bloodgroup = bloodgroup;
}
}