简体   繁体   English

版本化基于Java的REST Web服务?

[英]Versioning a Java based REST web service?

There seems to be an on-going philosophical debate about exactly how to version a REST web service. 关于如何版本化REST Web服务似乎正在进行哲学辩论。 For me though, the number one issue is practical question regarding how easy/hard is it to implement and maintain in a Java servlet based backend. 但对我来说,头号问题是关于在基于Java servlet的后端实现和维护是多么容易/困难的实际问题。 My company is building a new REST web service and while we are not currently concerned with versioning it at this time, I do not want to make an architectural decision that would paint us into a corner. 我的公司正在构建一个新的REST Web服务,虽然我们目前还不关心它的版本化,但我不想做出一个可以让我们陷入困境的架构决策。

I guess the primary decision we have to make right now is should we put the version identifier in our URI or in the media type (or both). 我想我们现在必须做出的主要决定是我们应该将版本标识符放在我们的URI或媒体类型(或两者)中。 In case its relevant, we will mint only a few new media types. 如果相关,我们将只提供一些新的媒体类型。 Also the application has 50+ resource URIs. 该应用程序还有50多个资源URI。

What are the pros and cons of each approach relative to implementing them in our Java servlet? 与在Java servlet中实现它们相比,每种方法的优缺点是什么?

My initial thoughts: 我最初的想法:

1) I like the idea of versioning the media types (eg "application/vnd.mystuff+xml;version=1.0"), because it feels the same as versioning the XML schema for a SOAP/RPC web service. 1)我喜欢对媒体类型进行版本控制的想法(例如“application / vnd.mystuff + xml; version = 1.0”),因为它与为SOAP / RPC Web服务版本化XML模式感觉相同。 Also, it seems like conneg was designed just for this sort of thing. 而且,似乎conneg只是为了这种事情而设计的。 However, It seems like implementing this would be difficult if not impossible because our application is created with domain driven design principals and we can't just create new versions Java classes and have them sit along side the old ones. 但是,似乎实现这一点很困难,如果不是不可能的,因为我们的应用程序是使用域驱动的设计原则创建的,我们不能只创建新版本的Java类并让它们与旧版本一起使用。 So we'd have to implement the new Java classes and then hand code JSON/XML serialization/deserialization to support all the old versions of media types. 因此,我们必须实现新的Java类,然后手动编写JSON / XML序列化/反序列化代码以支持所有旧版本的媒体类型。 I guess this is no different than versioning XML Schema for SOAP... 我想这与为SOAP格式化XML Schema没什么不同...

2) On the other hand, if we were to version the URI's (eg "http://ourapp.com/v1/mystuff"), then our customers could deploy two entire versions of the system and stand them up as two separate servlets with different context mappings. 2)另一方面,如果我们要对URI进行版本控制(例如“http://ourapp.com/v1/mystuff”),那么我们的客户可以部署两个完整版本的系统并将它们作为两个独立的servlet进行部署具有不同的上下文映射。 However, we'd have modify the JPA (ORM) mappings of the v1 Java classes to use the v2 RDBMS schema. 但是,我们已经修改了v1 Java类的JPA(ORM)映射以使用v2 RDBMS模式。 This may not always be simple, but I can easily understand how it could be done. 这可能并不总是很简单,但我可以很容易地理解它是如何完成的。 This approach bugs me however because "application/vnd.mystuff+xml" will return two different XML models corresponding to two different XML schemas. 然而,这种方法让我感到困惑,因为“application / vnd.mystuff + xml”将返回两个不同的XML模型,这两个模型对应于两个不同的XML模式。 But maybe it doesn't matter as the client knows what it asked for because it used the v1 or v2 URI... 但也许无关紧要,因为客户端知道它要求的是什么,因为它使用了v1或v2 URI ...

Or is there an easier to implement, alternate approach we are not considering? 或者是否有更容易实施,我们没有考虑的替代方法? Or is it ok to punt on this entire issue and worry about it later? 或者可以解决这个问题,以后再担心吗?

If the restful services are used by mobile applications then it would a bit difficult to bake in the processing of both responses. 如果移动应用程序使用了休息服务,那么在处理两个响应时都会有点困难。 In my opinion keeping the version in the URL is the better way as this would be easier to decommission the old endpoints when convenient. 在我看来,保留URL中的版本是更好的方法,因为这在方便时更容易解除旧端点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM