首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 其他教程 > 互联网 >

Rest简介及Spring兑现

2012-07-01 
Rest简介及Spring实现?一 Roy Fielding?2000年Rest被Roy Fielding提出来的,我对Roy Fielding的印象有以下

Rest简介及Spring实现

?一 Roy Fielding

?

2000年Rest被Roy Fielding提出来的,我对Roy Fielding的印象有以下几个.

一是RoyFielding做为Http协议的起草者,在Http协议发布没多久跳起来说这个世界上很多人对于Http的使用是错误的,所以他说大家应该用Rest.

二是没多久RoyFielding做为Rest思想的启蒙者,在Rest被人接受并被广泛使用没多久跳起来说这个世界上很多人对Rest的使用是错误的..

?

所以我在PPT上选了柏拉图的一句话做为副标题,"思想永远是宇宙的统治者".

?

二 Rest

?

Rest本身的内容比我想象的多的多,大致列出来几个关键点如下:

1.满足以下的Constraints:

??

Client–serverStatelessCacheableLayered systemCode on demand (optional)Uniform interface

?

?

?2.设计接口时候的原则

?

????

Identification of resourcesManipulation of resources through these representationsSelf-descriptive messagesHypermedia as the engine of application state

?

?

3.Rest希望实现的目标

?

??

Scalability of component interactionsGenerality of interfacesIndependent deployment of componentsIntermediary components to reduce latency, enforce security and encapsulate legacy systems

?

?

?? 以上内容都摘自Wiki,稍微整理了一下.感觉以上的内容都很深刻,所以我简单的列出来了我认为理解Rest的重要的地方.

?

?

? 4.Rest对于我们来说

?

??

@Controller@RequestMapping(value = "/contact")public class ContactController {private static final Log log = LogFactory.getLog(ContactController.class);@RequestMapping(value = "/{contact}", method = RequestMethod.GET)public String getContactdetail(@PathVariable Long contact, Model model) {Contact c = this.contactService.getContact(contact);if (c == null) {c = new Contact();}model.addAttribute("code", 0);model.addAttribute("contact", c);return "/contact/detail/jmodel";}}

?

?增删改查分别修改"method "以对应Http的四种方法(Post,Delete,Put,Get)就好了.

?变量直接通过@PathVariable 就可以拿到.

?

?

第二个问题我理解起来也很简单.用UrlRewriter将所有的请求分成两种.动态请求加一个前缀"/app/",配置Spring的拦截器只拦截这种请求. 静态资源以前缀"/r/"开始,请求路径不变.

?

这样任何一个请求都会乖乖的分成两部分,是以"/r/"开始的就不会走Spring,不是以"/r/"开头全转成"/app/",交给Spring处理.

?

主要配置如下

?

?

web.xml 写道<filter-mapping>
<filter-name>UrlRewriteFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--Spring只拦截/app/的请求-->
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/app/*</url-pattern>
</servlet-mapping>

?

urlrewrite.xml 写道<urlrewrite default-match-type="wildcard">
<rule>
<from>/r/**</from>
<to>/r/$1</to>
</rule>
<rule>
<from>/**</from>
<to>/app/$1</to>
</rule>
<outbound-rule>
<from>/app/**</from>
<to>/$1</to>
</outbound-rule>
</urlrewrite>

?

?

?? UrlRewrite可以用其它有同样功能的任意代替,Apache神马的最讨厌了.

?

?? 最后附上我用到的UrlRewriter的Pom文件.不记得在哪儿看到的了,先贴上来再说.

??

pom.xml 写道
<dependency>
<groupId>org.tuckey</groupId>
<artifactId>urlrewritefilter</artifactId>
<version>3.1.0</version>
</dependency>

?

?

?

?

?

Over,这个东西不是一个很详细的Spring配置说明.我记得第一次配的时候还是出了不少问题的.不过我觉得源码如果公开的话就什么问题都么有了~

等等看什么时候可以把Labs的源码公开了.

?

?

?

??

?

?

?

?

?

?

??

?

?

?

?

?

??

???

热点排行