如何创建向后兼容的 JAX-RS 和 JAX-WS API?

发布于 2024-10-02 18:19:57 字数 667 浏览 5 评论 0原文

JAX-RS 和 JAX-WS 非常适合生成 API。然而,它们根本没有解决向后兼容性的问题。

为了避免在 API 引入新功能时破坏旧客户端,您本质上必须接受并提供与之前完全相同的输入和输出格式;如果许多 XML 和 JSON 解析器发现某个字段未映射到任何内容或类型错误,那么它们似乎都适合。

一些 JSON 库(例如 Jackson 和 Gson)提供了一项功能,您可以根据运行时设置为给定对象指定不同的输入/输出表示形式,这似乎是在许多情况下处理版本控制的合适方法。这使得可以通过注释添加和删除的字段来提供向后兼容性,以便它们仅根据客户端使用的 API 版本显示。

JAXB 和我迄今为止发现的任何其他 XML 数据绑定库都没有对这个概念提供适当的支持,更不用说能够为 JSON 和 XML 重用相同的注释了。将其添加到 JAXB-RI 或 EclipseLink Moxy 似乎是可能的,但令人畏惧。

另一种版本控制方法似乎是对所有已更改的类进行版本控制,通常是在每次发布 API 时创建一个新包,并在新包中复制所有已修改的 DTO、服务和资源类,以便所有类型信息针对绑定和调度系统进行版本控制。这种方法对我来说似乎更费力。

我的问题是:您如何设计 Jave API 提供程序以实现向后兼容性?什么有效,什么无效?

非常感谢有关该主题的案例研究或博客文章的链接;我已经进行了一些谷歌搜索,但没有找到对此的太多讨论。

JAX-RS and JAX-WS are great for producing an API. However, they don't address the concern of backwards compatibility at all.

In order to avoid breaking old client when new capabilities are introduced to the API, you essentially have to accept and provide the exact same input and output format as you did before; many of the XML and JSON parsers out there seem to have a fit if they find a field that doesn't map to anything, or has the wrong type.

Some JSON libraries out there, such as Jackson and Gson, provide a feature where you can specify a different input/output representation for a given object based on a runtime setting, which seems like a suitable way to handle versioning for many cases. This makes it possible to provide backwards compatibility by annotating added and removed fields so they only appear according to the version of the API in use by the client.

Neither JAXB nor any other XML databinding library I have found to date has decent support for this concept, nevermind being able to re-use the same annotations for both JSON and XML. Adding it to the JAXB-RI or EclipseLink Moxy seems potentially possible, but daunting.

The other approach to versioning seems to be to version all the classes that have changed, often by creating a new package each time the API is published and making copies of all modified DTO, Service, and Resource classes in the new package so that all the type information is versioned for the binding and dispatch systems. This approach seems more laborious to me.

My question is: how have you designed your Jave API providers for backwards compatibility? What worked, what didn't?

Links to case studies or blog posts on the subject much appreciated; I've done some googling but haven't been finding much discussion of this.

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

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

发布评论

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

评论(1

御弟哥哥 2024-10-09 18:19:57

我是 EclipseLink MOXy 的技术主管,我对您的版本控制要求非常感兴趣。您可以通过我的博客联系我:

MOXy 提供了一种将 JAXB 元数据表示为 XML 文件的方法。您可以利用它为同一对象模型创建多个映射:

I'm the tech lead for EclipseLink MOXy, I'm very interested in your versioning requirements. You can reach me through my blog:

MOXy offers a means to represent the JAXB metadata as an XML file. You can leverage this to create multiple mappings for the same object model:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文