1

在发送 JSON 以由 Jackson 反序列化时,似乎如果子元素的最后一个值为 ,则nullJackson 将中止反序列化。


坏的:

如果我将此 JSON 发送到我的接收器(如下),则映射Content对象将为null,因为反序列化停止于image

{"user":{"id":"1", "token":"ABC", "image":null},"content":{"id":"2"}}

好的:

如果我这样发送它,image仍将具有null所需的值,但Content将使用其 id 创建对象:

{"user":{"id":"1", "image":null, "token":"ABC"},"content":{"id":"2"}}

这对我来说就像一个错误..?!


简单的接收器

@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@Transactional
public Response create(UserContent source) {
        UserContent dbResult = manager.create(source);
        return Response.status(200)
                .entity(dbResult.getId().toString()).build();
}

串行器

 public static String objectToString(Object object) {
        ObjectMapper mapper = new ObjectMapper();
        mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"));

        Writer strWriter = new StringWriter();
        try {
            mapper.writeValue(strWriter, object);
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
        String userDataJSON = strWriter.toString();

        return userDataJSON;
    }

我可以将我的 ObjectMapper 设置为在发送端将所有对象序列null化为或空对象吗?""

如果我将UserContent具有属性的对象传递null给上述方法,则生成的 JSON 将具有null与上面示例类似的值,并且如果列表的最后一项具有值 null,反序列化将中止,导致我的后端出现 NullPointerExceptions。


编辑 - UserContent 类:

@Entity
@Table(name = "user_content")
@XmlRootElement
public class UserContent extends org.springframework.data.jpa.domain.AbstractPersistable<Long> {
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "user_id", nullable = false)
    @Fetch(FetchMode.JOIN)
    private User user;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "content_post_id", nullable = false)
    @Fetch(FetchMode.JOIN)
    private Contentcontent;

    // more properties...
}

我们的依赖:

<properties>
    <jersey-version>1.14</jersey-version>
</properties>
            <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-mapper-asl</artifactId>
        <version>1.9.5</version>
    </dependency>

    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-server</artifactId>
        <version>${jersey-version}</version>
    </dependency>

    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-client</artifactId>
        <version>${jersey-version}</version>
    </dependency>

    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-json</artifactId>
        <version>${jersey-version}</version>
    </dependency>

    <dependency>
        <groupId>com.sun.jersey.contribs</groupId>
        <artifactId>jersey-multipart</artifactId>
        <version>${jersey-version}</version>
    </dependency>


    <dependency>
        <groupId>com.sun.jersey.jersey-test-framework</groupId>
        <artifactId>jersey-test-framework-grizzly2</artifactId>
        <version>${jersey-version}</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-servlet</artifactId>
        <version>${jersey-version}</version>
        <scope>test</scope>
    </dependency>
4

2 回答 2

0

对于这种奇怪的行为,我们现在的解决方案是通过配置忽略序列化上的所有空值ObjectMapper

mapper.setSerializationInclusion(Inclusion.NON_NULL);

当我们控制双方时,我们可以做到这一点。如果我们将 REST 服务公开可用,我仍然对如何实际处理如上所述的传入空值星座感到好奇。

于 2012-11-26T05:29:18.923 回答
-2

假设您使用的是运动衫。您可以尝试在服务器端用Genson替换 jackson ,它应该可以解决您的问题。确实,它看起来像一个错误。请务必删除 jackson 和 jersey-json。您不需要任何配置来启用 Genson 的 json 支持,只需将 jar 放在您的类路径中。

于 2012-11-25T18:29:43.063 回答