Wildfly中的JSON序列化循环(无限递归) [英] JSON Serialization loop (infinite recursion) in Wildfly

查看:198
本文介绍了Wildfly中的JSON序列化循环(无限递归)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用IntelliJ IDEA开发一个简单的基于maven的JavaEE应用程序,显然我想将Wildfly 8用于开发和生产。我只需要通过一些RESTful Web服务公开一些实体。这些实体具有双向关系,当它们被序列化为JSON时会导致循环。

I am developing a straightforward maven-based JavaEE application in IntelliJ IDEA, and obviously I would like to use Wildfly 8 for both development and production. I simply need to expose some entities through some RESTful web services. Those entities have bidirectional relationships, which leads to a loop when they are going to be serialized into JSON.

新版本的Jackson能够处理这种情况特殊注释。为了实现这一点,我需要排除Wildfly内置的JSON序列化器/ jackson提供程序/它是什么,并使用我的应用程序捆绑的新版本。我已经按照我在网上找到的说明进行了操作,并提出了这个jboss-deployment-structure.xml文件:

Newer versions of Jackson are able to handle this kind of situation with a special annotation. To get that to work, I need to exclude Wildfly's built-in JSON serializer / jackson provider / whatever it is and use the newer version that comes bundled with my application. I have followed the instructions I have found on the web and came up with this jboss-deployment-structure.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<jboss-deployment-structure>
    <deployment>
        <exclusions>
            <module name="org.codehaus.jackson.jackson-jaxrs" />
            <module name="org.codehaus.jackson.jackson-core-asl" />
            <module name="org.codehaus.jackson.jackson-mapper-asl" />
            <module name="org.codehaus.jackson.jackson-xc" />
        </exclusions>
    </deployment>
</jboss-deployment-structure>

问题是,它不起作用。即使我将我的pom.xml设置为这样的东西:

The problem is, it doesn't work. Even when I set my pom.xml to something like this:

<dependencies>
    <dependency>
        <groupId>javax</groupId>
        <artifactId>javaee-api</artifactId>
        <version>7.0</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>2.3.1</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-annotations</artifactId>
        <version>2.3.0</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.jboss.resteasy</groupId>
        <artifactId>resteasy-jackson2-provider</artifactId>
        <version>3.0.6.Final</version>
        <scope>provided</scope>
    </dependency>
</dependencies>

这清楚地表明应该没有任何东西与我的应用程序捆绑在一起,我仍然得到这个StackOverflowError(由无限循环)源于org.codehaus.jackson包。这意味着杰克逊的内置版本仍在开发中,并不排除在外。我该如何解决这个问题?

which clearly indicates that nothing should come bundled with my application, I still get this StackOverflowError (caused by the infinite loop) which roots in org.codehaus.jackson package. This is turn means that built-in version of Jackson is still in the works and is not excluded. How can I fix this?

提前致谢。

我更改了标题,因为我觉得问题更大了。要么我做了一些非常错误的事情,要么Wildfly存在严重问题。

I changed the title because I guess the problem is even bigger. Either I am doing something terribly wrong or there is a serious problem with Wildfly.

我创建了一个包含所有jackson 2库的pom.xml(com.fasterxml.jackson) *)具有编译范围。这些库包含在WEB-INF / lib文件夹中。我根据 Jackson JAX-RS常见问题解答编写了@Provider,我可以验证它实际上是由JAX-RS实现(RestEasy),因为如果我在WEB-INF / lib中没有包含带有ClassNotFoundException的jackson库,则部署失败。但是,我仍然使用org.codehaus.X(杰克逊1)获得无限递归错误。

I have created a pom.xml with all the jackson 2 libraries (com.fasterxml.jackson*) with a "compile" scope. The libraries are included in the WEB-INF/lib folder. I wrote a @Provider according to Jackson JAX-RS FAQ and I can verify that it is actually read by the JAX-RS implementation (RestEasy), simply because the deployment fails if I do not include jackson libraries in WEB-INF/lib with a ClassNotFoundException. However, I am still getting infinite recursion errors with org.codehaus.X (Jackson 1).

我不在乎如何,我只需要一个解决方案来解决这个问题Wildfly中的无限递归。

I don't care how, I just need a solution to fix this infinite recursion in Wildfly.

推荐答案

解决方案是创建一个实现 MessageBodyWriter< Object> <的类/ code>使用Jackson的 ObjectMapper

The solution is to create a class which implement MessageBodyWriter<Object> using Jackson's ObjectMapper:

@Provider
@Produces("application/json")
public class JacksonMapper implements MessageBodyWriter<Object> {

    @Override
    public boolean isWriteable(Class<?> aClass, Type type, Annotation[] annotations, MediaType mediaType) {
        return true;
    }

    @Override
    public long getSize(Object object, Class<?> aClass, Type type, Annotation[] annotations, MediaType mediaType) {
        return 0;
    }

    @Override
    public void writeTo(Object object, Class<?> aClass, Type type, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> stringObjectMultivaluedMap, OutputStream outputStream)
            throws IOException, WebApplicationException {
        outputStream.write(new ObjectMapper().writeValueAsBytes(object));
    }

}

无需任何排除或Wildfly特定的描述符。

There's no need for any exclusion or Wildfly-specific descriptors.

如果包含依赖项无关紧要( compile 提供范围都可以正常工作,因为Jackson 2包含在Wildfly中。然而,由于一个未知的原因,几乎不可能停用杰克逊1。

It doesn't matter if you include the dependencies or not (compile or provided scopes both work fine) as Jackson 2 is included in Wildfly. However, for an unknown reason, it is near to impossible to deactivate Jackson 1.

这个解决方案带来了杰克逊2的工作。现在,您可以使用 @JsonIdentityInfo 注释轻松避免序列化循环。 此处提供更多信息

This solution brings Jackson 2 into the works. Now you can easily avoid serialization loops using the @JsonIdentityInfo annotation. More info here.

这篇关于Wildfly中的JSON序列化循环(无限递归)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆