JSON parse error: Can not construct instance of class

2787184 picture 2787184 · Aug 9, 2017 · Viewed 12.1k times · Source

Unable to map json string with java object, getting error JSON parse error: Can not construct instance of com.test.CPInput$Evc$Uni

error:

{
"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;
    }

}

Answer

Rami Sharaiyri picture Rami Sharaiyri · Aug 9, 2017

Try to use static inner classes for: CustomerSegment, Evc, PreviousValues and Uni.

I always use static inner classes, and I don't have any problem like that.