RestEasy 和 JSON - 如何避免数字周围的引号?

发布于 2024-09-25 07:04:26 字数 561 浏览 0 评论 0原文

我正在使用 RestEasy 将实体编入 JSON。这工作得很好,但不知何故,所有的东西都被表示为一个字符串。例如,

@XmlRootElement(name="TestObject")
public class TestObject {
    private Long value;
    public Long getValue(){
        return value;
    }
}

它不是创建类似: {TestObject:{value:1234}} 的内容,而是

创建 {TestObject:{value:"1234"}} (请注意周围的“”数字)

因此 long 值被转换为字符串。我怎样才能避免这种情况?

我在 Jackson 论坛上询问过 RestEasy 正在使用哪个 JSON 行进,但他们说这可能是由 Java->XML->JSON 引起的。 似乎没有 RestEasy 论坛,并且在 Seam 论坛上没有人可以回答我的问题。

其他人也有同样的问题吗?

问候

I am using RestEasy to marchal entities to JSON. That works okay but somehow every thing is represented as a String. e.g.

@XmlRootElement(name="TestObject")
public class TestObject {
    private Long value;
    public Long getValue(){
        return value;
    }
}

Instead of creating something like: {TestObject:{value:1234}}

It creates {TestObject:{value:"1234"}} (Please note the " " around the number)

So the long value is converted into a String. How can I avoid that?

I've asked on the Jackson forum which RestEasy is using for the JSON marchaling but they said it is probably caused by going Java->XML->JSON.
There doesn't seem to be a RestEasy forum and on the Seam forum no one could answer my question.

Does anyone else have the same problem?

Regards

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

自由如风 2024-10-02 07:04:26

好吧,问题是 RestEasy+Seam 默认使用 Jetison(而不是 Jackson)。
Jettison 通过 Java->XML->JSON 进行编组。

Jackson jars实际上并不包含在Seam发行版中,所以你必须下载RestEasy并将所有提到jackson的jars复制到你的lib目录中。当RestEasy在类路径中找到resteasy-jackson-provider.jar时,将使用Jackson而不是Jettison。

当我从杰蒂森搬到杰克逊时遇到的一个问题是自行车参考。使用 Jettison,您只需使用以下方法注释方法(例如 @ManyToOne 关系)
@XmlTransient。对于 Jackson,您必须使用 @JsonIgnore 对其进行注释

Okay the problem is that RestEasy+Seam uses Jettison by default (and not Jackson).
Jettison does the marchaling via Java->XML->JSON.

The Jackson jars aren't actually included in the Seam distribution so you have to download RestEasy and copy all jars which mention jackson to your lib directory. When RestEasy finds the resteasy-jackson-provider.jar in the classpath, Jackson will be used instead of Jettison.

One problem I had when moving to Jackson from Jettison were cycling references. With Jettison you just annotate the method (e.g. a @ManyToOne relationship) with
@XmlTransient. For Jackson you have to annotate it with @JsonIgnore

牵你的手,一向走下去 2024-10-02 07:04:26

我只使用

<dependency>
    <groupId>org.jboss.resteasy</groupId>
    <artifactId>resteasy-jettison-provider</artifactId>
    <version>2.0.1.GA</version>
    <exclusions>
        <exclusion>
            <groupId>javax.xml.bind</groupId>
            <artifactId>jaxb-api</artifactId>
        </exclusion>
        <exclusion>
            <groupId>com.sun.xml.bind</groupId>
            <artifactId>jaxb-impl</artifactId>
        </exclusion>
        <exclusion>
            <groupId>javax.xml.stream</groupId>
            <artifactId>stax-api</artifactId>
        </exclusion>
    </exclusions>
</dependency>

resteasy-jettison-provider,但是当我尝试获取JSON输出时,服务器挂起,但在我排除jaxb-api和jaxb-impl之后。 Web 服务与 JSON 输出完美配合

use

<dependency>
    <groupId>org.jboss.resteasy</groupId>
    <artifactId>resteasy-jettison-provider</artifactId>
    <version>2.0.1.GA</version>
    <exclusions>
        <exclusion>
            <groupId>javax.xml.bind</groupId>
            <artifactId>jaxb-api</artifactId>
        </exclusion>
        <exclusion>
            <groupId>com.sun.xml.bind</groupId>
            <artifactId>jaxb-impl</artifactId>
        </exclusion>
        <exclusion>
            <groupId>javax.xml.stream</groupId>
            <artifactId>stax-api</artifactId>
        </exclusion>
    </exclusions>
</dependency>

I use only resteasy-jettison-provider, but server hangs when I try to get JSON output, but after I excludes the jaxb-api and jaxb-impl. webservice works perfectly fine with JSON output

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文