使用Spring 3.2.0和Jackson 2.1.1反序列化对象数组时遇到问题。 当我点击一个带有对象数组的控制器时,它会抛出“无法实例化bean类。没有找到默认的构造函数”。我的Pojo也有默认构造函数。我在dispatcher-servlet中使用 MappingJackson2HttpMessageConverter 进行JSON处理。
POJO的:
public class Student{
private String rollNum;
private String firstName;
private String lastName;
public Student(){
}
/**
* @return the firstName
*/
public String getFirstName() {
return firstName;
}
/**
* @param firstName the firstName to set
*/
public void setFirstName(String firstName) {
this.firstName = firstName;
}
/**
* @return the lastName
*/
public String getLastName() {
return lastName;
}
/**
* @param lastName the lastName to set
*/
public void setLastName(String lastName) {
this.lastName = lastName;
}
/**
* @return the rollNum
*/
public String getRollNum() {
return rollNum;
}
/**
* @param rollNum the rollNum to set
*/
public void setRollNum(String rollNum) {
this.rollNum = rollNum;
}
}
控制器类:
public @ResponseBody
String saveStudentName(
final @ModelAttribute Student[] student){
//code goes here
}
这项服务受到iOS应用程序的攻击。当我用单个对象(@ModelAttribute学生)打同一个服务时,它工作正常。但是使用对象数组,它会抛出异常
org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [[Lcom.student.pojo.Student;]: No default constructor found;
客户端输入
[{"rollNum":"12","firstName":"qweqw","lastName":"adsf"},{"rollNum":"23","firstName":"rty","lastName":"fghj"}]
并且客户端获得以下错误作为响应
Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (Invalid value around character 0.) UserInfo=0x7522680 {NSDebugDescription=Invalid value around character 0.}
请有人告诉我哪里错了。如果控制器参数正确,请告诉我可以发送给控制器的输入格式。
提前致谢。
答案 0 :(得分:0)
首先,您必须围绕Student列表创建一个包装器,如下所示:
class Students extends ArrayList<Student> {
public Students() {
super();
}
}
同样更改服务方法:
public @ResponseBody String saveStudentName(final @ModelAttribute Students students){
//code goes here
}
我想这会解决您的问题。
~Ragesh AK
答案 1 :(得分:0)
Below are the complete configs:
**My pom.xml:**
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.springframework.rest</groupId>
<artifactId>RestWebService</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>RestWebService Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<spring.version>3.0.5.RELEASE</spring.version>
<junit.version>4.11</junit.version>
<jdk.version>1.6</jdk.version>
</properties>
<dependencies>
<!-- Spring 3 dependencies -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.7</version>
<scope>compile</scope>
</dependency>
<!-- dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-stream</artifactId>
<version>2.1.3.RELEASE</version>
</dependency -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>RestWebService</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.0</version>
<configuration>
<source>${jdk.version}</source>
<target>${jdk.version}</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
**dispatcher-servlet.xml:**
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="com.springframework.rest" />
<!-- To enable @RequestMapping process on type level and method level -->
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="jsonConverter" />
</list>
</property>
</bean>
<bean id="jsonConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
<property name="supportedMediaTypes" value="application/json" />
</bean>
</beans>
**Student:**
public class Student {
private String name;
private int id;
private String address;
public Student() {
}
public Student(int id, String name, String address) {
this.id = id;
this.name = name;
this.address = address;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
**Students:**
public class Students extends ArrayList<Student> {
public Students() {
super();
}
}
**Controller:**
@Controller
public class BaseController{
@RequestMapping(value="/student", method = RequestMethod.POST)
@ResponseBody
public String saveStudent(@RequestBody Students students) {
System.out.println("Size:"+students.size());
return "Success";
}
}
如果这有助于你,请告诉我....