JSON解析错误:无法构造类的实例

Posted

技术标签:

【中文标题】JSON解析错误:无法构造类的实例【英文标题】:JSON parse error: Can not construct instance of class 【发布时间】:2018-01-17 02:44:58 【问题描述】:

无法用java对象映射json字符串,报错 JSON parse error: Can not construction instance of 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;
    


【问题讨论】:

我将内部类设为静态并解决了问题。 【参考方案1】:

尝试使用静态内部类:CustomerSegment、Evc、PreviousValues 和 Uni。

我总是使用静态内部类,我没有任何问题。

【讨论】:

这里是对这个问题的一些解释:cowtowncoder.com/blog/archives/2010/08/entry_411.html 使用静态类会导致竞争条件吗? 也许这就是你要问的?***.com/questions/42407057/…

以上是关于JSON解析错误:无法构造类的实例的主要内容,如果未能解决你的问题,请参考以下文章

JSON 解析错误:无法从 START_OBJECT 令牌中反序列化 `byte[]` 的实例

JSON 解析错误:无法反序列化 START_ARRAY 令牌中的实例

JSON 解析错误:无法从 START_OBJECT 令牌中反序列化 java.util.ArrayList 的实例

AWS DynamoDB - 转换器类 - “错误请求,无法解析JSON”

无法解析构造函数 GoogleSignInOptions

找不到模块:错误:无法解析“流”或 TypeError:stream.Readable 不是构造函数