を認識しないGsonは、私は私のサーブレットに次のPOSTリクエストを作ってみるネストされたJSONオブジェクト
public static boolean validateUserForm(HttpServletRequest req)
{
// get all the parameters here
try {
Type type = new TypeToken<ReimbursementRequest>() {}.getType();
ReimbursementRequest rr = gson.fromJson(req.getReader(), type);
System.out.println(rr.toString());
} catch (JsonSyntaxException e) {
e.printStackTrace();
return false;
} catch (JsonIOException e) {
e.printStackTrace();
return false;
} catch (IOException e) {
e.printStackTrace();
return false;
}
catch (NumberFormatException ex)
{
ex.printStackTrace();
return false;
}
return true;
}
マイReimbursementRequest
POJOは、次のようになります。
package p1.revature.beans;
public class ReimbursementRequest extends Bean {
private int id, requesterID;
private double amount;
private String status;
private int reimbursableEventID;
private ReimbursableEvent reimbursableEvent;
/**
* Default constructor
*/
public ReimbursementRequest() {
super();
}
/**
* Constructor for everything but the id. This is used to insert into the reimbursement request table
* @param requesterID : the ID of the Employee requesting reimbursement
* @param amount : the amount requested
* @param status : the status of the request
* @param reimbursableEventID : the id of the ReimbursableEvent the requester is trying to have covered
*/
public ReimbursementRequest(int requesterID, double amount, String status, int reimbursableEventID) {
super();
this.requesterID = requesterID;
this.amount = amount;
this.status = status;
this.reimbursableEventID = reimbursableEventID;
}
/**
* Constructor for every field but the reimbursable event id. Used for rendering material client-side
* @param id
* @param requesterID
* @param amount
* @param status
* @param reimbursableEvent
*/
public ReimbursementRequest(int id, int requesterID, double amount, String status,
ReimbursableEvent reimbursableEvent) {
super();
this.id = id;
this.requesterID = requesterID;
this.amount = amount;
this.status = status;
this.reimbursableEvent = reimbursableEvent;
}
/**
* Constructor for every field. Used for select queries.
* @param id : the primary key of this request
* @param requesterID : the ID of the Employee requesting reimbursement
* @param amount : the amount requested
* @param status : the status of the request
* @param reimbursableEventID : the id of the ReimbursableEvent the requester is trying to have covered
*/
public ReimbursementRequest(int id, int requesterID, double amount, String status, int reimbursableEventID) {
super();
this.id = id;
this.requesterID = requesterID;
this.amount = amount;
this.status = status;
this.reimbursableEventID = reimbursableEventID;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getRequesterID() {
return requesterID;
}
public void setRequesterID(int requesterID) {
this.requesterID = requesterID;
}
public double getAmount() {
return amount;
}
public void setAmount(double amount) {
this.amount = amount;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public int getReimbursableEventID() {
return reimbursableEventID;
}
public void setReimbursableEventID(int reimbursableEventID) {
this.reimbursableEventID = reimbursableEventID;
}
public ReimbursableEvent getReimbursableEvent() {
return reimbursableEvent;
}
public void setReimbursableEvent(ReimbursableEvent reimbursableEvent) {
this.reimbursableEvent = reimbursableEvent;
}
@Override
public String toString() {
return "ReimbursementRequest [id=" + id + ", requesterID=" + requesterID + ", amount=" + amount + ", status="
+ status + ", reimbursableEventID=" + reimbursableEventID + ", reimbursableEvent=" + reimbursableEvent
+ "]";
}
}
Gsonは何らかの理由で私が実際にPAであるときにそれを普通の文字列にしていると思っていますそれをJSON文字列で表します。このエラーを返します:
com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:224)
at com.google.gson.Gson.fromJson(Gson.java:888)
at com.google.gson.Gson.fromJson(Gson.java:853)
at p1.revature.services.HandleDashboardData.validateUserForm(HandleDashboardData.java:34)
at p1.revature.servlets.SubmitDashboardInfo.doPost(SubmitDashboardInfo.java:32)
私はこのプロジェクトを手に入れようとしています。これは2日以内に完了します。私はあなたのための例を見つけてみなければなら Stringify.json
...あなたはJSONオブジェクトとしてデータを渡すことができ
デバッグモードで 'req.getReaderを()'を提供してください。 – amir110
'req.getReader()'が出力した内容をデバッグしてそれを質問に添付できますか? – pirho
そして、POJOは、あなたがそれを作成するために渡しているJSONのように全く見えません。あなたは正しいコードを追加しましたか? – pirho