私はPOSTリクエストからデータを渡し、JSONがPOTDクラスにバインドするためのAPIをヒットしたいと考えています。 POTD内では、POTDResources型のArrayListがあります。これはデータのバインドに問題があります。JSONをJava SpringのArraylistにバインドするには?
私は最終的にProblem
オブジェクト全体を使用するつもりですが、今はクラス内の2つのフィールドでテストしています。
私はタイプのArrayList
に特異的に結合することができるようにJSON入力を必要とする、私は次のようにデータを渡していPOTDResources
:
{
"problemTitle": "Balancing Binary Tree",
"resources": [{"name": "youtube", "link": "http://yotube.com"}]
}
API呼び出し最初のヒット:
コントローラ
@RequestMapping(value = "/generatepotd", method = RequestMethod.POST)
@ResponseBody
public void generatePOTD(@RequestBody POTD problem) throws IOException {
POTD prob = new POTD();
prob.setProblemTitle(problem.getProblemTitle());
prob.setResources(problem.getResources());
tempGen.generateProblemOfTheDay(prob);
}
POTDクラス
package com.algoq.algoq.models;
import org.springframework.context.annotation.Description;
import java.util.ArrayList;
@Description("Handles the fields required for processing problem of the day")
public class POTD {
private String subject;
private String problemTitle;
private String problemDescription;
private ArrayList<POTDResources> resources;
// POTD(String subject, String problemTitle, String problemDescription, ArrayList<POTDResources> resources) {
POTD(String problemTitle, ArrayList<POTDResources> resources) {
// this.subject = subject;
this.problemTitle = problemTitle;
// this.problemDescription = problemDescription;
this.resources = resources;
}
public POTD() {
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
public String getProblemTitle() {
return problemTitle;
}
public void setProblemTitle(String problemTitle) {
this.problemTitle = problemTitle;
}
public String getProblemDescription() {
return problemDescription;
}
public void setProblemDescription(String problemDescription) {
this.problemDescription = problemDescription;
}
public ArrayList<POTDResources> getResources() {
return resources;
}
public void setResources(ArrayList<POTDResources> resources) {
this.resources = resources;
}
}
POTDリソースクラス
package com.algoq.algoq.models;
public class POTDResources {
private String name;
private String link;
public POTDResources(String name, String link) {
this.name = name;
this.link = link;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getLink() {
return link;
}
public void setLink(String link) {
this.link = link;
}
}
エラーメッセージ
{
"timestamp": 1513192593064,
"status": 400,
"error": "Bad Request",
"exception": "org.springframework.http.converter.HttpMessageNotReadableException",
"message": "JSON parse error: Can not construct instance of com.algoq.algoq.models.POTDResources: no suitable constructor found, can not deserialize from Object value (missing default constructor or creator, or perhaps need to add/enable type information?); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of com.algoq.algoq.models.POTDResources: no suitable constructor found, can not deserialize from Object value (missing default constructor or creator, or perhaps need to add/enable type information?)\n at [Source: [email protected]; line: 3, column: 17] (through reference chain: com.algoq.algoq.models.POTD[\"resources\"]->java.util.ArrayList[0])",
"path": "/generatepotd"
}
実装は正常です。エラーメッセージとは何ですか? ArrayListの代わりに小さなヒントが、インターフェイスによって使用されます。 private List resources; また、モデルのデフォルトのコンストラクタを追加しようとします。 –
JSONStatham
@JSONStatham素敵なユーザー名。私はOPのエラーを –
@ JSONStathamで更新しました。デフォルトのコンストラクタが必要だったようです。まだテストしています。どのようにSpringが実際にこれを処理するか、特定のメソッド名を探しますか? –