我正在发送以下JSON的AJAX请求:
{"name":"somebody","vehicles":["car","trunk"],"_vehicles":"on"}
注意:( _vehicles属性来自表单:复选框)
它绑定的bean就像这样简单:
public class Person{
private String name;
private List<String> vehicles;
在访问Spring Controller之前,抛出以下异常:
org.springframework.http.converter.HttpMessageNotReadableException: Could not read document: Can not deserialize instance of java.lang.String out of START_ARRAY token
at [Source: java.io.PushbackInputStream@e36df534; line: 1, column: 117] (through reference chain: java.util.LinkedHashMap["vehicles"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.lang.String out of START_ARRAY token
at [Source: java.io.PushbackInputStream@e36df534; line: 1, column: 117] (through reference chain: java.util.LinkedHashMap["vehicles"])
控制器:
@RequestMapping(value="/new", method=RequestMethod.POST, consumes="application/json", produces="application/json")
@ResponseBody
public Person saveNewPerson(@RequestBody Person person){
依赖关系:
web-mvc-config摘录:
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="messageConverters">
<list>
<!-- Default converters -->
<bean class="org.springframework.http.converter.StringHttpMessageConverter" />
<bean class="org.springframework.http.converter.FormHttpMessageConverter" />
<bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter" />
<bean class="org.springframework.http.converter.xml.SourceHttpMessageConverter" />
<!-- Jackson 2 -->
<bean class="org.springframework.http.converter.ResourceHttpMessageConverter" />
<ref bean="mappingJackson2HttpMessageConverter"/>
</list>
</property>
</bean>
<bean id="mappingJackson2HttpMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" />
为什么不能反序列化?
注意:如果我没有发送车辆部件,则Person对象的反序列化工作正常。