杰克逊的NPE试图序列化一个不存在的领域?

时间:2018-01-25 21:12:36

标签: spring-boot jackson2 jackson-databind

这是我的简单豆

@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class Foo {

    private String firstName;
    private String lastName;

    public String getFullName(){
     return firstName + lastName;
    }

}

当这个对象在Jackson的Spring-boot控制器中被序列化时, 我收到以下错误

j.l.NullPointerException: null
   com.example.foobar.foo.getFullName(Foo.java:28)
  s.r.NativeMethodAccessorImpl.invoke0(NativeMethodAccessorImpl.java)
  s.r.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
  s.r.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  j.l.reflect.Method.invoke(Method.java:498)
  c.f.j.d.s.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:653)
  c.f.j.d.s.s.BeanSerializerBase.serializeFields(BeanSerializerBase.java:690)..
  28 common frames omitted\nWrapped by: c.f.j.d.JsonMappingException: (was java.lang.NullPointerException) 
  (through reference chain: com.example.foobar.foo[\"fullName\"])
   c.f.j.d.JsonMappingException.wrapWithPath(JsonMappingException.java:379)
  c.f.j.d.JsonMappingException.wrapWithPath(JsonMappingException.java:339)
  c.f.j.d.s.s.StdSerializer.wrapAndThrow(StdSerializer.java:343)
  c.f.j.d.s.s.BeanSerializerBase.serializeFields(BeanSerializerBase.java:698)
  c.f.j.d.s.BeanSerializer.serialize(BeanSerializer.java:155)
  c.f.j.d.s.DefaultSerializerProvider.serializeValue(DefaultSerializerProvider.java:292)
  c.f.j.d.ObjectWriter$Prefetch.serialize(ObjectWriter.java:1419)
  c.f.j.d.ObjectWriter.writeValue(ObjectWriter.java:940)
  o.s.h.c.j.AbstractJackson2HttpMessageConverter.writeInternal(AbstractJackson2HttpMessageConverter.java:267)...
  23 common frames omitted\nWrapped by: o.s.h.c.HttpMessageNotWritableException: Could not write content: (was java.lang.NullPointerException) (through reference chain: com.example.foobar.foo[\"fullName\"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: (was java.lang.NullPointerException) (through reference chain: com.example.foobar.foo[\"fullName\"])
                                                                                                                                                o.s.h.c.j.AbstractJackson2HttpMessageConverter.writeInternal(AbstractJackson2HttpMessageConverter.java:274 ..."

这是我发送的requestBody

{"firstName": "foo",
"lastName: null
}

Jackson试图序列化fullName属性吗?但我没有宣布这样的领域。这是预期的行为吗?我无法找到支持此行为的文档。

由于

1 个答案:

答案 0 :(得分:2)

你的猜测是正确的,杰克逊试图通过其无法找到的getter方法找到字段名称。因此NPE。

不同的解决方案

  1. 在getFullName方法上使用@JsonIgnore
  2. 您可以通过在spring boot application.properties文件中设置此属性来禁用此功能

    spring.jackson.mapper.use-getters-as-setters=false

  3. 如果您希望使用java代码(如果不使用spring boot),可以使用像这样的bean声明

    @Bean
    public Jackson2ObjectMapperBuilder objectMapperBuilder() {
      Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
      builder.featuresToDisable(MapperFeature.USE_GETTERS_AS_SETTERS);
      return builder;
    }