-1

I got this error. I tried add to properties this code: "spring.jackson.deserialization.accept-single-value-as-array=true" but i can't solve this problem?

org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Can not deserialize instance of java.util.ArrayList out of START_OBJECT token; nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.util.ArrayList out of START_OBJECT token at [Source: java.io.PushbackInputStream@2e64d73; line: 1, column: 145] (through reference chain: com.test.mobil.viewmodel.CompanyViewModel ["customerList"])

CustomerViewModel

public class CustomerViewModel {

private String name;
private String surname;
private int birthDate;

public CustomerViewModel(){}

public CustomerViewModel(String name, String surname, int birthDate) {
    this.name = name;
    this.surname = surname;
    this.birthDate = birthDate;
}
}

CompanyViewModel

public class CompanyViewModel {

private String company;
private List<CustomerViewModel> customerList;

public CompanyViewModel(){}

public CompanyViewModel(String company, List<CustomerViewModel> customerList) {
    this.company = company;
    this.customerList = customerList;
}
}

CustomerController

@Controller
public class CustomerController {

@PostMapping("/customer")
public void setCustomer(@RequestBody CompanyViewModel companyViewModel){
    System.out.println(companyViewModel);
}
}

JSON

page = {
    company: "Facebook",
    customerList = [
        {
            name: "Test1",
            surname: "Test2",
            birthDate: 1987
        },
        {
            name: "Test3",
            surname: "Test4",
            birthDate: 1988
        }

    ]
}
  • 4
    Your JSON seems wrong!! Replace `customerList =` with `customerList :` – Abdullah Khan Jul 12 '17 at 14:31
  • Unless you have configured appropriately the used `ObjectMapper`, you also have to add getters/setters to the private fields of your objects, which you want to make serialisable/deserialisable. – Dimos Jul 12 '17 at 14:38
  • You should change your JSON example to one where `customerList` really is just a JSON object (instead of a JSON array) to match your exception. – Sotirios Delimanolis Jul 12 '17 at 14:52
  • What JSON you are actually sending. You should send: `{ company: "Facebook", customerList = [ { name: "Test1", surname: "Test2", birthDate: 1987 }, { name: "Test3", surname: "Test4", birthDate: 1988 } ] }` **without `page=`** also, use `customerList:` instead `customerList=` – Serhii Povísenko Jul 12 '17 at 16:51

2 Answers2

-1

You misunderstood the problem: The array can't be deserialized because it's not valid JSON! You have an = at customerList = [, as said here :

JsonMappingException: Can not deserialize instance of java.util.ArrayList
                          out of START_OBJECT token
    at [Source: java.io.PushbackInputStream@2e64d73; line: 1, column: 145]
        (through reference chain: com.test.mobil.viewmodel.CompanyViewModel ["customerList"])
Abdullah Khan
  • 12,010
  • 6
  • 65
  • 78
Turtle
  • 1,626
  • 16
  • 26
  • @Abdullah Is there a rule that exceptions must be formatted as quoted text? I prefer when they have the language coloration in a `code` block. – Turtle Jul 12 '17 at 14:44
  • @Downvoter Why did someone just downvoted the 3 given answers? An explanation would be great. – Turtle Jul 12 '17 at 14:47
  • No there is no rule for that. Its just that the exception is more readable and the code formatting doesn't mess the exception. – Abdullah Khan Jul 12 '17 at 14:47
  • @AbdullahKhan Well, for me at least, the exception is actually easier to read in code format ^^ I guess that's a matter of taste. – Turtle Jul 12 '17 at 14:49
  • That's just the browser output of the json. If that was really the problem, the error would be completely different. – Sotirios Delimanolis Jul 12 '17 at 14:49
  • @SotiriosDelimanolis I actually got the same error for the same problem some times ago. Reading the stacktrace lead (led?) me to the conclusion that my JSON was wrong, which was the case. I'm positive this is actually the problem (at least **one** problem). – Turtle Jul 12 '17 at 14:55
  • No. The Jackson exception for incorrect JSON syntax looks like `Unexpected character ('t' (code 116)): was expecting double-quote to start field name` and `Unexpected character ('=' (code 61)): was expecting a colon to separate field name and value` – Sotirios Delimanolis Jul 12 '17 at 14:57
  • Their issue is they are trying to deserialize a single JSON object as an `ArrayList`. Their configuration of `spring.jackson.deserialization.accept-single-value-as-array=true` must have been done incorrectly. – Sotirios Delimanolis Jul 12 '17 at 14:58
-1

Thank you so much guys. I checked JSON value on front-side and I saw like this. My JSON not include Array because Angular misled me. I changed List to Map and problem SOLVED!

{  
"customer":{  
  "name":"name1",
  "surname":"sur1",
  "company":"firm",
  "address":"adres",
  "phone":"tel",
  "fax":"faxx",
  "type":"real",
  "taxNo":"2398"
},
"products":{  
  "0":{  
     "company":"es",
     "product":"a",
     "detail":"a",
     "value":2
  },
  "1":{  
     "company":"we",
     "product":"qwe",
     "detail":"qwe",
     "value":1
  }
},
}