Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 667 Vote(s) - 3.55 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Can not deserialize instance of java.util.ArrayList out of START_OBJECT token

#11
@JsonFormat(with = JsonFormat.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY)
private List< COrder > orders;
Reply

#12
After struggling with this thing for WAY too long, here is the super easy solution.

My controller was looking for

@RequestBody List<String> ids

and I had the request body as

{
"ids": [
"1234",
"5678"
]
}

and the solution was to change the body simply to

["1234", "5678"]

Yup. Just that easy.



Reply

#13
I was working with the same issue, I wanted to store multiple records at once.

After a lot of research, **I found an easy solution that worked immediately**.

**Before solving the problem code:**

**Controller method**

@PostMapping("/addList")
public ResponseEntity<List<Alarm>> saveAlarmList(@RequestBody List<AlarmDTO> dtos) {
return ResponseEntity.ok(alarmService.saveAlarmList(dtos));
}

**Service method**

public List<Alarm> saveAlarmList(List<AlarmDTO> dtos) {

List<Alarm> alarmList = new ArrayList<>();
for (AlarmDTO dto: dtos) {
Alarm newAlarm = AlarmDtoMapper.map(dto);
alarmList.add(newAlarm);
}

return alarmRepository.saveAll(alarmList);
}

**Entity class**

public class Alarm{

@Id
@Basic(optional = false)
@NotNull
@Column(name = "alarm_id")
private Long alarmId;

@Basic(optional = false)
@NotNull
@Size(min = 1, max = 100)
@Column(name = "alarm_name")
private String alarmName;
}


**postman object**


[
{
"alarmId":"55",
"alarmName":"fgdffg",
},
{
"alarmId":"77788",
"alarmName":"hjjjjfk",
}
]

**and another format**


{
"list": [
{
"alarmId":"55",
"alarmName":"fgdffg",
},
{
"alarmId":"77788",
"alarmName":"hjjjjfk",
}
]
}


In both methods, it was giving an error of serialization/deserialization.

then I just **implemented the Serializable interface in the Entity class** as follows.


**Entity class**



public class Alarm implements Serializable
{
@Id
@Basic(optional = false)
@NotNull
@Column(name = "alarm_id")
private Long alarmId;

@Basic(optional = false)
@NotNull
@Size(min = 1, max = 100)
@Column(name = "alarm_name")
private String alarmName;
}

After this modification the first type of request object from postman worked.
Reply

#14
The problem is the JSON - this cannot, by default, be deserialized into a `Collection` because it's not actually a JSON Array - that would look like this:

[
{
"name": "Test order1",
"detail": "ahk ks"
},
{
"name": "Test order2",
"detail": "Fisteku"
}
]

Since you're not controlling the exact process of deserialization (RestEasy does) - **a first option** would be to simply inject the JSON as a `String` and then take control of the deserialization process:

Collection<COrder> readValues = new ObjectMapper().readValue(
jsonAsString, new TypeReference<Collection<COrder>>() { }
);

You would loose a bit of the convenience of not having to do that yourself, but you would easily sort out the problem.

**Another option** - if you cannot change the JSON - would be to construct a wrapper to fit the structure of your JSON input - and use that instead of `Collection<COrder>`.
Reply

#15
The error message "Can not Deserialize Instance of java.util.ArrayList Out of start_object Token" typically occurs when you are trying to deserialize a JSON object to an ArrayList in Java, but the JSON data is not in the expected format.

This error message indicates that the JSON data that you are trying to deserialize is starting with a JSON object token {...}, but your code is expecting an array token [...]

Reference: [

[To see links please register here]

][1]


[1]:

[To see links please register here]

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through