【发布时间】:2019-03-12 03:25:41
【问题描述】:
该类返回JSON字符串,但包含List的Map值返回NULL指针异常。
尝试了提到的各种选项,仍然得到相同的空指针异常。
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.SerializerProvider;
public class JacksonFoo
{
public static void main(String[] args) throws Exception
{
Map<String, Foo> foos = new HashMap<String, Foo>();
foos.put("foo1", new Foo("foo1"));
foos.put("foo2", new Foo(List<DetailMessage>));
foos.put("foo3", null);
foos.put(null, new Foo("foo4"));
ObjectMapper mapper = new ObjectMapper();
mapper.configure(SerializationFeature.WRITE_NULL_MAP_VALUES, false);
mapper.setSerializationInclusion(Include.NON_NULL);
// Since the DetailMessage field is null , Its throwing Null pointer //exception.
}
}
【问题讨论】: