I'm new in Spring 4 and I have a problem. Mabye someone knows how to help me.
This JSON does not work and I get
exception is com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.util.ArrayList out of START_OBJECT token
{
"placeAnOrder": {
"customer": {
"name": "adas",
"surname": "asdasd",
"street": "asd",
"homeNr": "ada",
"phone": "123123"
},
"orders": {
"order": {
{
"orderId": "1231",
"dateOrder": "2015-09-09 12:12:12",
"products": {
"product": [
{
"productId": "34234",
"price": "23423",
"quantity": "2"
}
]
}
}
}
}
}
}
This JSON works well.
{
"placeAnOrder": {
"customer": {
"name": "adas",
"surname": "asdasd",
"street": "asd",
"homeNr": "ada",
"phone": "123123"
},
"orders": {
"order": [
{
"orderId": "1231",
"dateOrder": "2015-09-09 12:12:12",
"products": {
"product": [
{
"productId": "34234",
"price": "23423",
"quantity": "2"
}
]
}
}
]
}
}
}
In first JSON I have one list element of Order but represented as {} instead of []. In second I have []. I found that I can handle this first JSON when I add this
<bean id="jacksonObjectMapper" class="com.fasterxml.jackson.databind.ObjectMapper" />
<bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="targetObject" ref="jacksonObjectMapper" />
<property name="targetMethod" value="configure" />
<property name="arguments">
<list>
<value type="com.fasterxml.jackson.databind.DeserializationFeature">ACCEPT_SINGLE_VALUE_AS_ARRAY</value>
<value>true</value>
</list>
</property>
</bean>
<mvc:annotation-driven>
<mvc:message-converters>
<!-- Jackson converter for HTTP messages -->
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="objectMapper" ref="jacksonObjectMapper" />
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
I added this to my servlet.xml (besides this I have web.xml and applicationContext.xml) but it does not work. I use fasterxml.jackson v.2.6.2.
JSON syntax requires arrays to be contained inside square brackets and not curly braces. As per www.json.org :
An array is an ordered collection of values. An array begins with [ (left bracket) and ends with ] (right bracket). Values are separated by , (comma).
By default, Jackson uses strict parsing and hence throws JsonMappingException since array is given as braces and not square brackets.
When you add the following to servlet.xml, it tells Jackson to treat single objects as arrays to work with Java collections:
<value type="com.fasterxml.jackson.databind.DeserializationFeature">ACCEPT_SINGLE_VALUE_AS_ARRAY</value>
As per Jackson documentation:
public static final DeserializationFeature ACCEPT_SINGLE_VALUE_AS_ARRAY
Feature that determines whether it is acceptable to coerce non-array (in JSON) values to work with Java collection (arrays, java.util.Collection) types. If enabled, collection deserializers will try to handle non-array values as if they had "implicit" surrounding JSON array. This feature is meant to be used for compatibility/interoperability reasons, to work with packages (such as XML-to-JSON converters) that leave out JSON array in cases where there is just a single element in array.
Feature is disabled by default.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With