restful 风格 加上springmvc
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了restful 风格 加上springmvc相关的知识,希望对你有一定的参考价值。
一、spring 版本:spring-framework-3.2.7.RELEASE
二、所需其它Jar包:
三、主要代码:
1
2
3
4
5
6
7
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
|
<? xml version = "1.0" encoding = "UTF-8" ?> < web-app xmlns = "http://java.sun.com/xml/ns/javaee" xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation = "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version = "2.5" > < context-param > < param-name >log4jConfigLocation</ param-name > < param-value >classpath:log4j.properties</ param-value > </ context-param > < context-param > < param-name >log4jRefreshInterval</ param-name > < param-value >60000</ param-value > </ context-param > < context-param > < param-name >contextConfigLocation</ param-name > < param-value >classpath:applicationContext.xml</ param-value > </ context-param > <!-- 编码过虑 --> < filter > < filter-name >encodingFilter</ filter-name > < filter-class >org.springframework.web.filter.CharacterEncodingFilter</ filter-class > < init-param > < param-name >encoding</ param-name > < param-value >UTF-8</ param-value > </ init-param > < init-param > < param-name >forceEncoding</ param-name > < param-value >true</ param-value > </ init-param > </ filter > < filter-mapping > < filter-name >encodingFilter</ filter-name > < url-pattern >/*</ url-pattern > </ filter-mapping > <!-- Spring监听 --> < listener > < listener-class >org.springframework.web.context.ContextLoaderListener</ listener-class > </ listener > <!-- Spring MVC DispatcherServlet --> < servlet > < servlet-name >springMVC3</ servlet-name > < servlet-class >org.springframework.web.servlet.DispatcherServlet</ servlet-class > < init-param > < param-name >contextConfigLocation</ param-name > < param-value >classpath:springMVC-servlet.xml</ param-value > </ init-param > < load-on-startup >1</ load-on-startup > </ servlet > < servlet-mapping > < servlet-name >springMVC3</ servlet-name > < url-pattern >/</ url-pattern > </ servlet-mapping > <!-- 解决HTTP PUT请求Spring无法获取请求参数的问题 --> < filter > < filter-name >HiddenHttpMethodFilter</ filter-name > < filter-class >org.springframework.web.filter.HiddenHttpMethodFilter</ filter-class > </ filter > < filter-mapping > < filter-name >HiddenHttpMethodFilter</ filter-name > < servlet-name >springMVC3</ servlet-name > </ filter-mapping > < display-name >UikitTest</ display-name > < welcome-file-list > < welcome-file >/WEB-INF/jsp/index.jsp</ welcome-file > </ welcome-file-list > </ web-app > |
springMVC-servlet.xml
1
2
3
4
5
6
7
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
|
<? xml version = "1.0" encoding = "UTF-8" ?> < beans default-lazy-init = "true" xmlns = "http://www.springframework.org/schema/beans" xmlns:p = "http://www.springframework.org/schema/p" xmlns:tx = "http://www.springframework.org/schema/tx" xmlns:aop = "http://www.springframework.org/schema/aop" xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns:context = "http://www.springframework.org/schema/context" xmlns:mvc = "http://www.springframework.org/schema/mvc" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"> <!-- 注解驱动 --> < mvc:annotation-driven /> <!-- 扫描包 --> < context:component-scan base-package = "com.citic.test.action" /> <!-- 用于页面跳转,根据请求的不同跳转到不同页面,如请求index.do则跳转到/WEB-INF/jsp/index.jsp --> < bean id = "findJsp" class = "org.springframework.web.servlet.mvc.UrlFilenameViewController" /> < bean class = "org.springframework.web.servlet.handler.SimpleUrlHandlerMapping" > < property name = "mappings" > < props > < prop key = "index.do" >findJsp</ prop > <!-- 表示index.do转向index.jsp页面 --> < prop key = "first.do" >findJsp</ prop > <!-- 表示first.do转向first.jsp页面 --> </ props > </ property > </ bean > <!-- 视图解析 --> < bean class = "org.springframework.web.servlet.view.UrlBasedViewResolver" > <!-- 返回的视图模型数据需要经过jstl来处理 --> < property name = "viewClass" value = "org.springframework.web.servlet.view.JstlView" /> < property name = "prefix" value = "/WEB-INF/jsp/" /> < property name = "suffix" value = ".jsp" /> </ bean > <!-- 对静态资源文件的访问 不支持访问WEB-INF目录 --> < mvc:default-servlet-handler /> <!-- 对静态资源文件的访问 支持访问WEB-INF目录 --> <!-- <mvc:resources location="/uikit-2.3.1/" mapping="/uikit-2.3.1/**" /> --> < bean id = "stringConverter" class = "org.springframework.http.converter.StringHttpMessageConverter" > < property name = "supportedMediaTypes" > < list > < value >text/plain;charset=UTF-8</ value > </ list > </ property > </ bean > <!-- 输出对象转JSON支持 --> < bean id = "jsonConverter" class = "org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" ></ bean > < bean class = "org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" > < property name = "messageConverters" > < list > < ref bean = "stringConverter" /> < ref bean = "jsonConverter" /> </ list > </ property > </ bean > </ beans > |
Controller:
1
2
3
4
5
6
7
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
|
package com.citic.test.action; import java.util.ArrayList; import java.util.List; import net.sf.json.JSONObject; import org.apache.log4j.Logger; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import com.citic.test.entity.Person; /** * 基于Restful风格架构测试 * * @author dekota * @since JDK1.5 * @version V1.0 * @history 2014-2-15 下午3:00:12 dekota 新建 */ @Controller public class DekotaAction { /** 日志实例 */ private static final Logger logger = Logger.getLogger(DekotaAction. class ); @RequestMapping (value = "/hello" , produces = "text/plain;charset=UTF-8" ) public @ResponseBody String hello() { return "你好!hello" ; } @RequestMapping (value = "/say/{msg}" , produces = "application/json;charset=UTF-8" ) public @ResponseBody String say( @PathVariable (value = "msg" ) String msg) { return "{\"msg\":\"you say:‘" + msg + "‘\"}" ; } @RequestMapping (value = "/person/{id:\\d+}" , method = RequestMethod.GET) public @ResponseBody Person getPerson( @PathVariable ( "id" ) int id) { logger.info( "获取人员信息id=" + id); Person person = new Person(); person.setName( "张三" ); person.setSex( "男" ); person.setAge( 30 ); person.setId(id); return person; } @RequestMapping (value = "/person/{id:\\d+}" , method = RequestMethod.DELETE) public @ResponseBody Object deletePerson( @PathVariable ( "id" ) int id) { logger.info( "删除人员信息id=" + id); JSONObject jsonObject = new JSONObject(); jsonObject.put( "msg" , "删除人员信息成功" ); return jsonObject; } @RequestMapping (value = "/person" , method = RequestMethod.POST) public @ResponseBody Object addPerson(Person person) { logger.info( "注册人员信息成功id=" + person.getId()); JSONObject jsonObject = new JSONObject(); jsonObject.put( "msg" , "注册人员信息成功" ); return jsonObject; } @RequestMapping (value = "/person" , |
以上是关于restful 风格 加上springmvc的主要内容,如果未能解决你的问题,请参考以下文章