Jersey 没有看到我的 MessageBodyReader

发布于 2024-11-09 13:48:05 字数 1023 浏览 0 评论 0原文

我尝试将球衣与我自己的 json MessageBodyReader/MessageBodyWriter 一起使用(因为我没有在我的域类上使用 @XmlRootElement... 注释)。

@Provider
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public final class MyGsonMessageBodyHandler implements MessageBodyWriter<Object>, MessageBodyReader<Object> {
...
}

Jersey 使用此类作为消息正文编写器(因为它在实现的方法 writeTo 中停在断点处)。然而,它并没有将此类视为 messagebodyreader (即使当我将此类分解为 messagebodyreader/messagebodywriter 的单独实现时,它仍然拒绝使用我的 messagebodyreader)。

测试代码如下所示(jersey-grizzly):

final Greeting greeting = resource.path("/greeting")
            .queryParam("name", name)
            .accept(MediaType.APPLICATION_JSON)
            .type(MediaType.APPLICATION_JSON)
            .get(Greeting.class);

我得到的错误如下所示:

A message body reader for Java class test.Greeting, and Java type class test.Greeting, and MIME media type application/json was not found

我想知道编写自己的 MessageBodyReader 需要什么样的魔法?

I'm trying to use jersey with my own json MessageBodyReader/MessageBodyWriter (as I am not use @XmlRootElement... annotations on my domain classes).

@Provider
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public final class MyGsonMessageBodyHandler implements MessageBodyWriter<Object>, MessageBodyReader<Object> {
...
}

Jersey uses this class as messagebodywriter (as it stops at breakpoint in the implemented method writeTo). Hovewer it does not see this class as messagebodyreader (and even when I break up this class to the separate implementations of the messagebodyreader/messagebodywriter it still refuses to use my messagebodyreader).

The testing code looks as follows (jersey-grizzly):

final Greeting greeting = resource.path("/greeting")
            .queryParam("name", name)
            .accept(MediaType.APPLICATION_JSON)
            .type(MediaType.APPLICATION_JSON)
            .get(Greeting.class);

The error I got looks as follows:

A message body reader for Java class test.Greeting, and Java type class test.Greeting, and MIME media type application/json was not found

I'm wondering what kind of magic is required for writing own MessageBodyReader?

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

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

发布评论

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

评论(4

拥抱影子 2024-11-16 13:48:05

一段时间后,我找到了问题的根本原因。我的 MessageBodyReader/Writer 实现没问题(而且我可以与 RESTlet 配合使用),但是如果您使用 JerseyTest,请不要忘记将您的 MessageBodyReader/Writer 添加到它的 ClientConfig 中:

/**
 * Creates custom REST client config which is mandatory since we don't use any JSON providers.
 * @return Jersey Client Config with the required classes to read/write in(out)coming data.
 */
private static ClientConfig createClientConfig() {
    final ClientConfig config = new DefaultClientConfig();
    config.getClasses().add(GsonMessageBodyHandler.class);
    config.getClasses().add(GsonAwareContextResolver.class);
    return config;
}

/**
 * Public ctor
 * @throws com.sun.jersey.test.framework.spi.container.TestContainerException On error
 */
public MyRestExposureTest() throws TestContainerException {
    super(new WebAppDescriptor.Builder("my.rest.package")
            .clientConfig(createClientConfig())
            .contextPath("/")
            .build());
}

否则您的客户端代码将是无法读/写您的 POJO。

After a while I found a root cause of the issue. My implementation of MessageBodyReader/Writer is OK (and I it works fine with RESTlet), but IF YOU USE JerseyTest, DO NOT FORGET TO ADD YOUR MessageBodyReader/Writer to it's ClientConfig:

/**
 * Creates custom REST client config which is mandatory since we don't use any JSON providers.
 * @return Jersey Client Config with the required classes to read/write in(out)coming data.
 */
private static ClientConfig createClientConfig() {
    final ClientConfig config = new DefaultClientConfig();
    config.getClasses().add(GsonMessageBodyHandler.class);
    config.getClasses().add(GsonAwareContextResolver.class);
    return config;
}

/**
 * Public ctor
 * @throws com.sun.jersey.test.framework.spi.container.TestContainerException On error
 */
public MyRestExposureTest() throws TestContainerException {
    super(new WebAppDescriptor.Builder("my.rest.package")
            .clientConfig(createClientConfig())
            .contextPath("/")
            .build());
}

Otherwise your client code would be unable to read/write your POJOs.

友谊不毕业 2024-11-16 13:48:05

这就是我正在使用的并且它正在工作(目前使用 Jersey 1.8)。

public static Client createMyClient() {
    ClientConfig cc = new DefaultClientConfig();
    cc.getClasses().add(MyProviderClass1.class);
    cc.getClasses().add(MyProviderClass2.class);
    cc.getClasses().add(MyProviderClass3.class);
    return Client.create(cc);
}

This is what I'm using and it's working (currently with Jersey 1.8).

public static Client createMyClient() {
    ClientConfig cc = new DefaultClientConfig();
    cc.getClasses().add(MyProviderClass1.class);
    cc.getClasses().add(MyProviderClass2.class);
    cc.getClasses().add(MyProviderClass3.class);
    return Client.create(cc);
}
当梦初醒 2024-11-16 13:48:05

我不确定这是否是您的情况,但常见的错误是 isReadable 方法的实现不正确。

你实现了吗?
调试时你会停在那里吗?
它返回 true 吗?

I'm not sure if it's your case, but the common mistake is incorrect implementation of isReadable method.

Did you implemented it?
Do you stop there, when debugging?
Does it return true?

孤独岁月 2024-11-16 13:48:05

在尝试了亚历克斯的解决方案后,这终于对我有用:

public IntegrationTest() throws TestContainerException {
    super(new LowLevelAppDescriptor.Builder(createResourceConfig())
                  .build());
}

private static ResourceConfig createResourceConfig() {
    ResourceConfig rc = new PackagesResourceConfig("com.github.joschi.jersey.security.smime");
    rc.getSingletons().add(new EnvelopedWriter());
    rc.getSingletons().add(new SignedWriter());
    return rc;
}

After trying Alex' solution, this finally worked for me:

public IntegrationTest() throws TestContainerException {
    super(new LowLevelAppDescriptor.Builder(createResourceConfig())
                  .build());
}

private static ResourceConfig createResourceConfig() {
    ResourceConfig rc = new PackagesResourceConfig("com.github.joschi.jersey.security.smime");
    rc.getSingletons().add(new EnvelopedWriter());
    rc.getSingletons().add(new SignedWriter());
    return rc;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文