无法在 Jersey 2.0 中为 Jackson 启用基于 POJO 的 JSON 绑定支持

Can#39;t enable POJO based JSON binding support for Jackson in Jersey 2.0(无法在 Jersey 2.0 中为 Jackson 启用基于 POJO 的 JSON 绑定支持)

本文介绍了无法在 Jersey 2.0 中为 Jackson 启用基于 POJO 的 JSON 绑定支持的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Jersey 来生成 JSON(通过 Jackson 进行 POJO 映射)和 Jetty(从 main 方法开始).
它非常适合 Jersey 1.x.:

I'm using Jersey to produce JSON (with POJO mapping through Jackson) and Jetty (start from main method).
It works perfect for Jersey 1.x.:

ServletHolder sh = new ServletHolder(ServletContainer.class);
sh.setInitParameter("com.sun.jersey.config.property.resourceConfigClass",
                        "com.sun.jersey.api.core.PackagesResourceConfig");
sh.setInitParameter("com.sun.jersey.config.property.packages", "service");
sh.setInitParameter("com.sun.jersey.api.json.POJOMappingFeature", "true");
...
context.addServlet(sh, "/rest/*");
Server server = new Server(8080);
server.setHandler(context);
server.start();

现在我将我的项目迁移到 Jersey 2.0 并且未能在其中启用基于 POJO 的 JSON 绑定,我得到以下信息:org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException: MessageBodyWriter not found for media type=application/json, type=class business.entity.ExampleEntity, genericType=class business.entity.ExampleEntity.

Now I migrated my project to Jersey 2.0 and failed to enable POJO based JSON binding in it, I get the following: org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException: MessageBodyWriter not found for media type=application/json, type=class business.entity.ExampleEntity, genericType=class business.entity.ExampleEntity.

显然,com.sun.jersey.api.json.POJOMappingFeature 不再有效,因为 Jersey 转到 org.glassfish

Obviously, com.sun.jersey.api.json.POJOMappingFeature no longer valid as Jersey goes to org.glassfish

文档说如下:

为了将 Jackson 用作您的 JSON (JAXB/POJO) 提供程序,您需要在 Configurable(客户端/服务器)中为 ObjectMapper(如果需要)注册 JacksonFeature 和 ContextResolver.

In order to use Jackson as your JSON (JAXB/POJO) provider you need to register JacksonFeature and a ContextResolver for ObjectMapper (if needed) in your Configurable (client/server).

但我不知道如何正确地做到这一点.

But I can't figure out how to do it correctly in my case.

我为这个问题创建了一个小项目:

I created a little project for this question:

branch master - Jersey 1.17.1 的工作示例;

branch master - worked example for Jersey 1.17.1;

分支 jersey-2.0-migration - 迁移到 Jersey 2.0 的尝试无效 - 测试失败

branch jersey-2.0-migration - not working attempt to migrate to Jersey 2.0 - test failed;

branch jersey-2.0-migrate-client-only - 尝试将 Jersey 客户端 2.0 与工作的 Jersey 服务器 1.17.1 一起使用 - 测试失败.

branch jersey-2.0-migrate-client-only - non working attempt to use Jersey client 2.0 with working Jersey server 1.17.1 - test failed.

问题是:如何在 Jersey 2.0 中启用基于 POJO 的 JSON 绑定

推荐答案

文档有点过时了.最新的 Jackson 构建提供了一个自动发现的提供程序.将以下 jars 添加到类路径中:

The documentation is a bit outdated. The latest Jackson build provides an auto-discoverable provider. Add the following jars to the class path:

1) jackson-annotations-2.2.2.jar

1) jackson-annotations-2.2.2.jar

2) jackson-core-2.2.2.jar

2) jackson-core-2.2.2.jar

3) jackson-databind-2.2.2.jar

3) jackson-databind-2.2.2.jar

4) 杰克逊-jaxrs-base-2.2.1.jar

4) jackson-jaxrs-base-2.2.1.jar

5) jackson-jaxrs-json-provider-2.2.1.jar

5) jackson-jaxrs-json-provider-2.2.1.jar

6) jackson-module-jaxb-annotations-2.2.2.jar

6) jackson-module-jaxb-annotations-2.2.2.jar

确保将com.fasterxml.jackson.jaxrs.json"添加到jersey.config.server.provider.packages"servlet 配置属性,以便自动发现 Jackson json 提供程序.

Make sure to add "com.fasterxml.jackson.jaxrs.json" to the "jersey.config.server.provider.packages" servlet config property, so the Jackson json provider can be auto-discovered.

这篇关于无法在 Jersey 2.0 中为 Jackson 启用基于 POJO 的 JSON 绑定支持的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:无法在 Jersey 2.0 中为 Jackson 启用基于 POJO 的 JSON 绑定支持

基础教程推荐