Rest介绍

/
blog
/
1
HTTP GET
=>
得到id
=
1的blog

/
blog
/
1
HTTP DELETE
=>
删除 id
=
1的blog

/
blog
/
1
HTTP PUT
=>
更新id
=
1的blog

/
blog HTTP POST
=>
新增BLOG
以下详细解一下spring rest使用.
首先,我们带着如下两个问题查看本文。
1.如何在java构造没有扩展名的RESTful url,如 /forms/1,而不是 /forms/1.do
2.浏览器的form标签不支持提交delete,put请求,如何曲线解决
springmvc rest 实现
[email protected] [email protected] annotation提供的,
[email protected](value=”/blog /{id}”,method=RequestMethod.DELETE)即可处理/blog/1 的delete请求.

@RequestMapping(value
=
“
/blog/{id}
“
,method
=
RequestMethod.DELETE)
2

public
ModelAndView delete(@PathVariable Long id,HttpServletRequest request,HttpServletResponse response)
{
3

4

5

@RequestMapping @PathVariable如果URL中带参数,则配合使用,如

@RequestMapping(value
=
“
/blog/{blogId}/message/{msgId}
“
,method
=
RequestMethod.DELETE)
2

public
ModelAndView delete(@PathVariable(
“
blogId
“
) Long blogId,@PathVariable(
“
msgId
“
) Long msgId,HttpServletRequest request,HttpServletResponse response)
{
3

1.springmvc web.xml配置

<!–
该servlet为tomcat,jetty等容器提供,将静态资源映射从/改为/static/目录,如原来访问 http://localhost/foo.css ,现在http://localhost/static/foo.css
–>
2

<
servlet-mapping
>
3

<
servlet-name
>
default
</
servlet-name
>
4

<
url-pattern
>
/static/*
</
url-pattern
>
5

</
servlet-mapping
>
6

<
servlet
>
7

<
servlet-name
>
springmvc
</
servlet-name
>
8

<
servlet-class
>
org.springframework.web.servlet.DispatcherServlet
</
servlet-class
>
9

<
load-on-startup
>
1
</
load-on-startup
>
10

</
servlet
>
11

12

<!–
URL重写filter,用于将访问静态资源http://localhost/foo.css 转为http://localhost/static/foo.css
–>
13

<
filter
>
14

<
filter-name
>
UrlRewriteFilter
</
filter-name
>
15

<
filter-class
>
org.tuckey.web.filters.urlrewrite.UrlRewriteFilter
</
filter-class
>
16

<
init-param
>
17

<
param-name
>
confReloadCheckInterval
</
param-name
>
18

<
param-value
>
60
</
param-value
>
19

</
init-param
>
20

<
init-param
>
21

<
param-name
>
logLevel
</
param-name
>
22

<
param-value
>
DEBUG
</
param-value
>
23

</
init-param
>
24

</
filter
>
25

<
filter-mapping
>
26

<
filter-name
>
UrlRewriteFilter
</
filter-name
>
27

<
url-pattern
>
/*
</
url-pattern
>
28

</
filter-mapping
>
29

30

<!–
覆盖default servlet的/, springmvc servlet将处理原来处理静态资源的映射
–>
31

<
servlet-mapping
>
32

<
servlet-name
>
springmvc
</
servlet-name
>
33

<
url-pattern
>
/
</
url-pattern
>
34

</
servlet-mapping
>
35

36

<!–
浏览器不支持put,delete等method,由该filter将/blog?_method=delete转换为标准的http delete方法
–>
37

<
filter
>
38

<
filter-name
>
HiddenHttpMethodFilter
</
filter-name
>
39

<
filter-class
>
org.springframework.web.filter.HiddenHttpMethodFilter
</
filter-class
>
40

</
filter
>
41

42

<
filter-mapping
>
43

<
filter-name
>
HiddenHttpMethodFilter
</
filter-name
>
44

<
servlet-name
>
springmvc
</
servlet-name
>
45

</
filter-mapping
>
2.webapp/WEB-INF/springmvc-servlet.xml配置,[email protected] annotation

<
bean
class
=”org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping”
/>
2

<
bean
class
=”org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter”
/>
3

3.Controller编写

/**
2

3

4

5

6

“
/userinfo
“
)
7

public
class
UserInfoController
extends
BaseSpringController
{
8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88


/userinfo => index()
2

3

4

5

6

7

8

9

注(不使用 /userinfo/add => add() 方法是由于add这个方法会被maxthon浏览器当做广告链接过滤掉,因为包含ad字符)
4.jsp 编写

<
form:form
action
=”${ctx}/userinfo${userInfo.userId}”
method
=”put”
>
2

</
form:form
>
生成的html内容如下, 生成一个hidden的_method=put,并于web.xml中的HiddenHttpMethodFilter配合使用,在服务端将post请求改为put请求

<
form
id
=”userInfo”
action
=”/springmvc_rest_demo/userinfo/2″
method
=”post”
>
2

<
input
type
=”hidden”
name
=”_method”
value
=”put”
/>
3

</
form
>
另外一种方法是你可以使用ajax发送put,delete请求.
5.静态资源的URL重写
如上我们描述,现因为将default servlet映射至/static/的子目录,现我们访问静态资源将会带一个/static/前缀.
如 /foo.gif, 现在访问该文件将是 /static/foo.gif.
那如何避免这个前缀呢,那就是应用URL rewrite,现我们使用 http://tuckey.org/urlrewrite/, 重写规则如下

<
urlrewrite
>
2

<!–
访问jsp及jspx将不rewrite url,其它.js,.css,.gif等将重写,如 /foo.gif => /static/foo.gif
–>
3

<
rule
>
4

<
condition
operator
=”notequal”
next
=”and”
type
=”request-uri”
>
.*.jsp
</
condition
>
5

<
condition
operator
=”notequal”
next
=”and”
type
=”request-uri”
>
.*.jspx
</
condition
>
6

<
from
>
^(/.*/..*)$
</
from
>
7

<
to
>
/static$1
</
to
>
8

</
rule
>
9

</
urlrewrite
>
SpringMVC使用RESTful风格后对静态资源的处理
在*-servlet.xml中配置:
[html] view plain copy
<mvc:annotation-driven />
<!– 对静态资源的访问 –>
<mvc:resources mapping=”/css/**” location=”/css/” />
<mvc:resources mapping=”/image/**” location=”/image/” />
<mvc:resources mapping=”/js/**” location=”/js/” />
mapping:表示页面中使用到的路径
location:表示从该路径下进行查找
以上的配置:css,image,js放置在webapp目录下,页面中使用时:localhost:8080/css/aaa.css
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/tech/pnotes/11236.html