簡體   English   中英

JSON解析錯誤:無法構造類的實例

[英]JSON parse error: Can not construct instance of class

無法將json字符串映射到java對象,得到錯誤JSON解析錯誤:無法構造com.test.CPInput的實例$ Evc $ Uni

錯誤:

{
"timestamp": 1502270576300,
"status": 400,
"error": "Bad Request",
"exception": "org.springframework.http.converter.HttpMessageNotReadableException",
"message": "**JSON parse error: Can not construct instance of com.test.CPInput$Evc$Uni: can only instantiate non-static inner class by using default, no-argument constructor; nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of com.test.CPInput$Evc$Uni: can only instantiate non-static inner class by using default, no-argument constructor at [Source: java.io.PushbackInputStream@edc246; line: 20, column: 9] (through reference chain: com.test.CPInput["evc"]->com.test.CPInput$Evc["uni"]->java.util.ArrayList[0]**)",
"path": "/demo/addCustomer"
}

JSON

{
  "customerId": "abcdef",
  "customerSegment": {
    "customerType": "customer type",
    "customerSubtype": "subtype",
    "industry": "industry",
    "subIndustry": "subindustry",
    "specialPopulation": " spl population"
  },
  "evc": {
    "evcId": "evcid",
    "action": "PR",
    "serviceId": "assigned prod id",
    "previousValues": {
        "previousName":"evcId",
        "previousValue":"EVC id - OLD"
    },
    "uni": [
      {
        "type": "uniA",
        "uniId": "uni id",
        "siteId": "sidt id",
        "tspCode": "tsp code",
        "elocId": "e loc id",
        "siteClli": "site clli",
        "uniAction": "PR",
        "previousValues": {
            "previousName":"evcId",
            "previousValue":"EVC id - OLD"
        }
      },
      {
        "type": "uniZ",
        "siteId": "sidt id",
        "tspCode": "tsp code",
        "elocId": "e loc id",
        "siteClli": "site clli",
        "uniAction": "PR",
        "previousValues": {
            "previousName":"evcId",
            "previousValue":"EVC id - OLD"
        }
      }
    ]
  },
  "solutionType": "EPL",
  "source": "Orion"
}

CPInput.java

public class CPInput {


    private String customerId;
    private CustomerSegment customerSegment;
    private Evc evc;
    private String solutionType;
    private String source;

    public CPInput() {
        super();
    }

    public class CustomerSegment{
        private String customerType;
        private String customerSubtype;
        private String industry;
        private String subIndustry;
        private String specialPopulation;
        //getter setter

    }

    public class Evc{
        private String evcId;
        private String action;
        private String serviceId;
        private PreviousValues previousValues;
        private List<CPInput.Evc.Uni> uni=new ArrayList<CPInput.Evc.Uni>();
        //getter setter

        public class PreviousValues{
            private String previousName;
            private String previousValue;
            //getter setter

        }


        public class Uni{
            private String type;
            private String uniId;
            private String siteId;
            private String tspCode;
            private String elocId;
            private String siteClli;
            private String uniAction;
            private PreviousValues previousValues;
            //getter setter

            public class PreviousValues{
                private String previousName;
                private String previousValue;
                //getter setter

    }

//getter setter

}

DemoController.java

@RestController
@SpringBootApplication(scanBasePackages = {"com.test"})
@RequestMapping(value = "/demo")
public class DemoController {

    @RequestMapping(method = RequestMethod.POST, value = "/addCustomer")
    public CPOutput addCustomer(@RequestBody CPInput input) {
        System.out.println(input);      
        return null;
    }

}

嘗試使用靜態內部類:CustomerSegment,Evc,PreviousValues和Uni。

我總是使用靜態內部類,我沒有這樣的問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM