如何以编程方式配置Jersey以使用Jackson进行JSON

如何以编程方式配置Jersey以使用Jackson进行JSON

本文介绍了如何以编程方式配置Jersey以使用Jackson进行JSON(反)序列化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我和Grizzly一起使用Jersey来创建一个JSON Web服务。我将其初始化如下:

I'm using Jersey together with Grizzly to create a JSON web service. I initialize it as follows:

    final ResourceConfig rc = new PackagesResourceConfig("amplify.api.resources");
    rc.getProperties().put(JSONConfiguration.FEATURE_POJO_MAPPING, true);
    return GrizzlyServerFactory.createHttpServer(BASE_URI, rc);

我没有web.xml文件。我注意到Jersey正在将单个元素列表映射为元素,而不是像我期望的那样[元素]。看来显然这是因为Jersey使用了除Jackson之外的POJO-> JSON映射器,并且出于我的原因,他们认为自动解包单元素列表是一个好主意。

I have no web.xml file. I noticed that Jersey was mapping single element lists to be "element" rather than "[element]" as I would expect. It seems that apparently this is because Jersey uses a POJO->JSON mapper other than Jackson, and for reasons that elude me, they thought it would be a great idea to automatically unwrap single-element lists.

我找到了如何通过配置web.xml文件让Jersey使用Jackson的解释,但由于我没有web.xml,我不确定如何做到这一点。

I found explanations of how to make Jersey use Jackson by configuring the web.xml file, but since I don't have a web.xml, I'm unsure how to do this.

任何人都可以解释我如何以编程方式配置Jersey以使用Jackson吗?

Can anyone explain how I can programmatically configure Jersey to use Jackson?

推荐答案

我有来自

想法是将jersey-json库添加到您的应用程序中。

The idea is to add jersey-json library to your application.

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

这篇关于如何以编程方式配置Jersey以使用Jackson进行JSON(反)序列化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 19:33