resteasy-link的架构分析
resteasy-link的架构分析
resteasy-link的核心设计是三个classes:LinkResource,RESTServiceDiscovery和AtomLink,此外还有一个用来标记的AddLinks。下面是类图:

其中RESTServiceDiscovery是扩展了ArrayList:

RESTServiceDiscovery里面装了一堆AtomLink。
整个resteasy-link的最终输出:
<?xml version=“1.0” encoding=“UTF-8” standalone=“yes”?>
<scrollableCollection xmlns:atom=“http://www.w3.org/2005/Atom” start=“0” limit=“1” totalRecords=“2”>
<comments xmlid=“0”>
<text>great book</text>
<rest rel=“update” href=“http://localhost:8081/book/foo/comment/0”/>
<rest rel=“remove” href=“http://localhost:8081/book/foo/comment/0”/>
<rest rel=“self” href=“http://localhost:8081/book/foo/comment/0”/>
<rest rel=“collection” href=“http://localhost:8081/book/foo/comment-collection”/>
<rest rel=“list” href=“http://localhost:8081/book/foo/comments”/>
<rest rel=“add” href=“http://localhost:8081/book/foo/comments”/>
</comments>
<rest rel=“home” href=“http://localhost:8081/“/>
<rest rel=“collection” href=“http://localhost:8081/book/foo/comment-collection”/>
<rest rel=“next” href=“http://localhost:8081/book/foo/comment-collection;query=book?start=1&limit=1”/>
<rest rel=“list” href=“http://localhost:8081/book/foo/comments”/>
<rest rel=“add” href=“http://localhost:8081/book/foo/comments”/>
</scrollableCollection>
上面的输出是LinkResource组成的。下面是LinkResource的组成:

RESTUtils负责串起来LinkResource,RESTServiceDiscovery和AtomLink。

入口方法是addDiscovery(...):

addDiscovery(...)会调用processLinkResources(...):
.png)
下面是processLinkResources(...)方法:
.png)
上面的方法会去调用processLinkResource(...)方法:
.png)
上面的方法会调用addInstanceService(...)或者addService(...)。而LinkResource的instance在这里面叫做service,其中service被用来生成AtomLink所需的数据,后续放入AtomLink里面。
以addService(...)为例:
.png)
可以看到最终是走到RESTServiceDiscovery.addLink(...)方法里面,这个方法就是往RESTServiceDiscovery里面添加AtomLink,在上面已经有介绍。