2017-12-13 2 views
1

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" 
} 
+0

구현이 잘된 것 같습니다. 오류 메시지가 무엇입니까? 작은 힌트, ArrayList 대신 인터페이스를 사용하십시오 : private List resources; 또한 모델의 기본 생성자를 추가하려고합니다. – JSONStatham

+0

@JSONStatham 멋진 사용자 이름입니다. 나는 OP를 오류 –

+0

@ JSONStatham으로 업데이트했습니다. 기본 생성자가 필요한 것처럼 보입니다. 여전히 테스트 중입니다. Spring이 실제로 이것을 어떻게 처리하는지 알기 원하십니까? 특정 메소드 이름을 찾습니까? –

답변

2

귀하의 문제는 오류 메시지와 함께 자리 잡고 있습니다.

는 POTDResources의 기본 생성자를 추가하려고 :

public POTDResources() { 

    } 

JSON 및 XML 처리를 위해 Spring에 의해 사용되는 잭슨, 오브젝트 2 가지 방법을 사용할 수 있습니다. 생성자 및 설정자를 기반으로합니다. 동일한 필드 이름과 유형을 가진 생성자를 찾을 수 있다면 해당 생성자를 사용하려고 시도합니다. 적합한 생성자가 없으면 객체의 인스턴스를 만들고 setter를 사용하려고 시도합니다. 적절한 생성자가 없으므로 인스턴스를 만들지 못했습니다.

2

당신의 POTDResources 클래스에 빈 생성자를 추가

public POTDResources() {} 

이유는 JSON 매퍼가 먼저 시도 할 것 인 요 초기화

public POTDResources() {} 

잭슨은 인수 없음의 생성자가 필요합니다에만 다음 UR 클래스, 그리고

1

귀하의 문제는 POTDResources는 기본 생성자가없는 것입니다 그것에 값을 적용한다.

더 적합한 생성자는 기본 생성자를 재정의

발견하고 ObjectMapper는 모델 클래스의 인스턴스를 만들 수 없습니다 :