Can Not Deserialize Instance of Java.Util.Arraylist Out of Start_Object Token

Can not deserialize instance of java.util.ArrayList out of START_OBJECT token

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>.

Hope this helps.

Cannot deserialize instance of `java.util.ArrayList<java.lang.Object>` out of START_OBJECT token

data in the JSON is not an ArrayList<String> instead it is a Map<String,List<String>>

So change you DTO as follows.

@Data
@Builder
@EqualsAndHashCode(exclude = "success")
@NoArgsConstructor
@AllArgsConstructor
@JsonIgnoreProperties(ignoreUnknown = true)
public class AparsersResponceDto {
private Integer success;
private Map<String,List<String>> data;
}

Exception: Can not deserialize instance of java.util.ArrayList out of START_OBJECT token at

You're trying to deserialize a list of Commune, but in HTTP response you're getting an object, containing such a list, but not a list itself. So, you need another wrapper object for deserialisation:

Wrapper

public class Towns implements Serializable {
private List<Commune> towns;

public Towns() {}

public List<Commune> getTowns() {
return towns;
}

public void setTowns(List<Commune> towns) {
this.towns = towns;
}

@Override
public String toString() {
return "Towns: " + towns.toString();
}
}

main app

    TypeReference<Towns> mapType = new TypeReference<Towns>() {};
Towns towns = mapper.readValue(output, mapType);
System.out.println(towns);

Jakson can not deserialize instance of java.util.ArrayList out of START_OBJECT token

you have problem in your json

"payments":{["id":"1"]}

since you are trying to deserialize list of type PaymentView this should be, i suppose PaymentView contains id attribute

 "payments":[{"id":"1"}]

Can not deserialize instance of java.util.ArrayList out of VALUE_STRING

This is the solution for my old question:

I implemented my own ContextResolver in order to enable the DeserializationConfig.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY feature.

package org.lig.hadas.services.mapper;

import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.ext.ContextResolver;
import javax.ws.rs.ext.Provider;

import org.codehaus.jackson.map.DeserializationConfig;
import org.codehaus.jackson.map.ObjectMapper;

@Produces(MediaType.APPLICATION_JSON)
@Provider
public class ObjectMapperProvider implements ContextResolver<ObjectMapper>
{
ObjectMapper mapper;

public ObjectMapperProvider(){
mapper = new ObjectMapper();
mapper.configure(DeserializationConfig.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
}
@Override
public ObjectMapper getContext(Class<?> type) {
return mapper;
}
}

And in the web.xml I registered my package into the servlet definition...

<servlet>
<servlet-name>...</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>...;org.lig.hadas.services.mapper</param-value>
</init-param>
...
</servlet>

... all the rest is transparently done by jersey/jackson.



Related Topics



Leave a reply



Submit