我希望能够从 POST 请求传递数据并访问我的 API,其中我传递的 JSON 绑定(bind)到 POTD 类。在 POTD 中,我有一个 POTDResources 类型的 ArrayList,这是我在绑定(bind)数据时遇到问题的地方。
我最终只是打算使用整个 Problem
对象,但现在我只是测试类中的两个字段。
我需要 JSON 输入能够绑定(bind)到 POTDResources
类型的 ArrayList
我正在传递如下数据:
{
"problemTitle": "Balancing Binary Tree",
"resources": [{"name": "youtube", "link": "http://yotube.com"}]
}
API 调用首次命中:
Controller
@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 类(class)
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: java.io.PushbackInputStream@1710624f; line: 3, column: 17] (through reference chain: com.algoq.algoq.models.POTD[\"resources\"]->java.util.ArrayList[0])",
"path": "/generatepotd"
}
最佳答案
您的问题在于错误消息:
no suitable constructor found
您正在覆盖默认构造函数,并且 ObjectMapper 无法创建您的 Model 类的实例。
尝试为 POTDResources 添加默认构造函数:
public POTDResources() {
}
Spring 用于 JSON 和 XML 处理的 Jackson 可以通过 2 种方式与 Object 一起工作。 基于构造函数和 Setter。 如果它能找到具有相同字段名称和类型的构造函数,它将尝试使用该构造函数。如果没有合适的构造函数,那么它将尝试创建对象的实例,并使用setters。您没有合适的构造函数,它无法创建实例。
关于java - 在 Java Spring 中将 JSON 绑定(bind)到 Arraylist?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47800472/