spring mvc 接收List对象入参
pom 里引入
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.3.0</version>
</dependency>
spring-servlet.xml 配置RequestMappingHandlerAdapter 转换json对象
<mvc:annotation-driven>
<mvc:message-converters register-defaults="true">
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
</list>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
<bean
class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="ignoreDefaultModelOnRedirect" value="true" />
<property name="messageConverters">
<list>
<bean
class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" />
</list>
</property>
</bean>
方法参数加上@requestBody注解
public Object addStudent(@RequestBody List<Student> studentList, HttpServletRequest request)
js将list序列化成json,传入后台 contentType 标识为 application/json
var data = $("form").serializeJson();
console.log(data);
$.ajax({
type:"post",
url:"/addStudent",
contentType : 'application/json',
data:data,
success:function(result) {
alert(JSON.stringify(result));
},
error:function() {
alert("error");
}
});