POST 요청에서 데이터를 전달하고 JSON을 통과하는 POTD 클래스에 바인딩하는 API를 사용할 수 있기를 원합니다. POTD 내에서 데이터 바인딩 문제가있는 POTDResources 유형의 ArrayList가 있습니다.Java Spring에서 JSR을 Arraylist에 바인딩 하시겠습니까?
결국 전체 Problem
개체를 사용할 예정이지만, 지금은 클래스 내에서 두 필드 만 테스트 중입니다.
나는 유형의 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이 실제로 이것을 어떻게 처리하는지 알기 원하십니까? 특정 메소드 이름을 찾습니까? –