Jersey 没有看到我的 MessageBodyReader
我尝试将球衣与我自己的 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 技术交流群。
发布评论
评论(4)
这就是我正在使用的并且它正在工作(目前使用 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);
}
在尝试了亚历克斯的解决方案后,这终于对我有用:
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;
}
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
一段时间后,我找到了问题的根本原因。我的 MessageBodyReader/Writer 实现没问题(而且我可以与 RESTlet 配合使用),但是如果您使用 JerseyTest,请不要忘记将您的 MessageBodyReader/Writer 添加到它的 ClientConfig 中:
否则您的客户端代码将是无法读/写您的 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:
Otherwise your client code would be unable to read/write your POJOs.