如何告诉 Jersey 使用我的 MessageBodyReader 而不是使用 JAXB?

发布于 2024-09-24 13:00:58 字数 419 浏览 1 评论 0原文

基本上,我有一些模型都使用 JAXB。但是,我有一些高度自定义的功能可以转换为 JSON 并返回,因此我想编写自己的 MessageBodyReader/Writer 来为我完成这项工作。

现在,写入部分已完成:如果我从 REST 资源返回我的模型之一,它将通过我的编写器。但是,当我尝试接受模型作为 FormParam 时,它不使用我的 MessageBodyReader,而是尝试使用 JAXB 对其进行解组(失败)。

那么我如何告诉 Jersey 使用我的 Reader 呢?

public TestModel testProvider(@FormParam("model") TestModel input){ //doesnt work
  return new TestModel(); //this part works!
}

Basically, I have some models which all use JAXB. However, I have some highly custom functionality to convert to JSON and back so I want to write my own MessageBodyReader/Writer to do the job for me.

Right now, the writing portion is done: if i return one of my models from a REST resource, it goes through my writer. But when I try and accept a model as a FormParam, it doesnt use my MessageBodyReader and instead attempts to unmarshal it using JAXB (which fails).

So how I can tell Jersey to use my Reader instead?

public TestModel testProvider(@FormParam("model") TestModel input){ //doesnt work
  return new TestModel(); //this part works!
}

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

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

发布评论

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

评论(2

幻想少年梦 2024-10-01 13:00:58
  1. 标记为 @Provider
  2. 将配置添加到您的 web.xml EX:

com.sun.jersey.config.property.packages; <参数值> your.package.that.contains.the.provider ;

  1. Mark as @Provider
  2. Add the configuration to your web.xml EX:

<init-param><param-name>com.sun.jersey.config.property.packages</param-name> <param-value> your.package.that.contains.the.provider </param-value> </init-param>

剪不断理还乱 2024-10-01 13:00:58

由于您的编写器可以工作,但您的阅读器不能工作,所以我猜您在配置中遗漏了某些内容。需要检查的一些事项:

  • 您的阅读器上有 @Provider 注释吗?
  • 您是否在阅读器上正确实现了 isReadable 方法?
  • 您在阅读器上是否有适当的 @Consumes 注释,其媒体类型是否与您的服务方法上指定的媒体类型匹配?

Since your writer works, but your reader does not, I'm guessing you have just missed something in your configuration. Some things to check:

  • Do you have the @Provider annotation on your reader?
  • Did you correctly implement the isReadable method on your reader?
  • Do you have the appropriate @Consumes annotation on the reader, does its media type match the media type specified on your service method?
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文