10

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

}
1
  • 3
    I made inner classes to static and problem resolved. Commented Aug 9, 2017 at 9:43

1 Answer 1

20

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.

Sign up to request clarification or add additional context in comments.

3 Comments

And here is some explanation of this problem: cowtowncoder.com/blog/archives/2010/08/entry_411.html
Does using a static class cause race-conditions?
Perhaps this is what you are asking ?stackoverflow.com/questions/42407057/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.