JsonGenerationException:JSON 中不允许的映射的空键(使用转换 NullKeySerializ



我正在尝试将此类的实例添加到neo4j spring 4.0.0.RELEASE

@NodeEntity
public class User
{
    @GraphId
    private Long id;
    @Property
    private UUID uniqueId = UUID.randomUUID();
    @Property
    private String username;
    @Property
    private String password;
    // getters, setters, blah blah...
}

当我这样做时,我得到这个异常:

ERROR o.a.c.c.C.[.[.[.[dispatcherServlet] - Servlet.service() for servlet [dispatcherServlet] in context with path [/pegboard] threw exception [Request processing failed; nested exception is org.neo4j.ogm.metadata.MappingException: Could not create JSON due to Null key for a Map not allowed in JSON (use a converting NullKeySerializer?)] with root cause
com.fasterxml.jackson.core.JsonGenerationException: Null key for a Map not allowed in JSON (use a converting NullKeySerializer?)
    at com.fasterxml.jackson.databind.ser.impl.FailingSerializer.serialize(FailingSerializer.java:35) ~[jackson-databind-2.6.5.jar:2.6.5]
    at com.fasterxml.jackson.databind.ser.std.MapSerializer.serializeFields(MapSerializer.java:538) ~[jackson-databind-2.6.5.jar:2.6.5]
    at com.fasterxml.jackson.databind.ser.std.MapSerializer.serialize(MapSerializer.java:469) ~[jackson-databind-2.6.5.jar:2.6.5]
    at com.fasterxml.jackson.databind.ser.std.MapSerializer.serialize(MapSerializer.java:29) ~[jackson-databind-2.6.5.jar:2.6.5]
    at com.fasterxml.jackson.databind.ser.std.MapSerializer.serializeFields(MapSerializer.java:561) ~[jackson-databind-2.6.5.jar:2.6.5]
    at com.fasterxml.jackson.databind.ser.std.MapSerializer.serialize(MapSerializer.java:469) ~[jackson-databind-2.6.5.jar:2.6.5]
    at com.fasterxml.jackson.databind.ser.std.MapSerializer.serialize(MapSerializer.java:29) ~[jackson-databind-2.6.5.jar:2.6.5]
    at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:693) ~[jackson-databind-2.6.5.jar:2.6.5]
...more stack trace...

这是怎么回事?

UUID 属性导致此错误。因为 neo4j 只会保留基元类型的属性 + String所以在不使用转换器的情况下无法映射。要解决创建转换器的问题,请执行以下操作:

import org.neo4j.ogm.typeconversion.AttributeConverter;
public class UUIDConverter implements AttributeConverter<UUID, String> {
    @Override
    public String toGraphProperty(UUID uuid) {
        return uuid.toString();
    }
    @Override
    public UUID toEntityAttribute(String uuid) {
        return UUID.fromString(uuid);
    }
}

然后将转换注释添加到用户类中的 UUID 属性:

import org.neo4j.ogm.annotation.typeconversion.Convert;
...
@Property
@Convert(UUIDConverter.class)
private UUID uniqueId = UUID.randomUUID();

相关内容

  • 没有找到相关文章

最新更新