[英]Spring MVC Jackson serialization throws NullPointer Exception
Note : I tried almost all the solution available, but still not able to get it working. 注意:我尝试了几乎所有可用的解决方案,但仍无法使其正常运行。
I am using Spring 4 in my application. 我在我的应用程序中使用Spring 4。 I have added jackson libs in my pom as below :
我在pom中添加了jackson libs,如下所示:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.2.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.2.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.2.1</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-core-asl</artifactId>
<version>1.9.9</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.9</version>
</dependency>
spring-config.xml looks like this(only part of it) : spring-config.xml看起来像这样(仅一部分):
<mvc:annotation-driven/>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="order" value="1"/>
<property name="messageConverters">
<list>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="supportedMediaTypes" value="application/json"/>
</bean>
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes" value="text/plain;charset=UTF-8"/>
</bean>
</list>
</property>
</bean>
Entity (Omitted the getters and setters) 实体 (省略了getter和setter)
@Entity(name="Employee")
public class Employee {
private String name;
private List<Address> addressList;
}
}
I am using @ResponseBody
in my rest controller. 我在我的rest控制器中使用
@ResponseBody
。 I am creating an employee with only the name (no address provided). 我正在创建仅具有姓名(未提供地址)的员工。 After the creation, I am trying to return the same object back to the UI.
创建之后,我试图将同一对象返回给UI。 But in this case, the address list will be NULL.
但是在这种情况下,地址列表将为NULL。 I am getting NullPointer Exception due to this.
由于这个原因,我收到了NullPointer异常。
I have tried annotating the address field with @JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
and @JsonInclude(JsonInclude.Include.NON_NULL)
. 我尝试使用
@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
和@JsonInclude(JsonInclude.Include.NON_NULL)
注释地址字段。 But in both of these cases also, I am getting the same exception. 但是在这两种情况下,我都会遇到相同的例外。
I tried different ways. 我尝试了不同的方法。 If I create a custom converter and use it in the controller, it works fine.
如果我创建了一个自定义转换器并在控制器中使用它,则可以正常工作。 But I do not want to write that code in all the controller methods.
但是我不想在所有控制器方法中都编写该代码。
Any solution ? 有什么办法吗?
Finally, I decided to go with instantiating the the collection with ArrayList
in the entities and it is working fine now. 最后,我决定继续使用实体中的
ArrayList
实例化集合,现在它可以正常工作。 None of the other ways worked. 其他方法均无效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.