简体   繁体   中英

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. I have added jackson libs in my pom as below :

<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) :

<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)

@Entity(name="Employee")
public class Employee {

private String name;
private List<Address> addressList;

}
}

I am using @ResponseBody in my rest controller. 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. But in this case, the address list will be NULL. I am getting NullPointer Exception due to this.

I have tried annotating the address field with @JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL) and @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. None of the other ways worked.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM