- 浏览: 150237 次
- 性别:
- 来自: 北京
文章分类
最新评论
-
守望麦穗:
楼主好, 我按着你的步骤查找问题 ,到第二步,也没有自定义U ...
Spring Security3边学边写(N)会话管理和并行控制 -
sb33060418:
Notify 写道楼主,但是配置<concurrency ...
Spring Security3边学边写(N)会话管理和并行控制 -
Notify:
楼主,但是配置<concurrency-control ...
Spring Security3边学边写(N)会话管理和并行控制 -
409421884:
你好,我也在做这个功能,但sessionRegistry这个东 ...
Spring Security3边学边写(N)会话管理和并行控制 -
sb33060418:
左眼的彼岸 写道sb33060418 写道左眼的彼岸 写道谢谢 ...
Spring Security3边学边写(N)会话管理和并行控制
基于springmvc实现restful样式的api接口。
1.Restful
Restful样式的接口将功能抽象为资源resource路径映射,以HTTP GET /resource/{id} 的方式访问。主要分为以下几类接口:
{id}称为路径变量,告诉restful你要对哪个资源进行查、改、删。
接口一般返回json/xml格式数据,方便服务端程序、浏览器脚本调用接口并处理返回数据。
按照Restful样式,teacher模块设计为两个资源地址:/restMvc/teachers和/restMvc/teachers/{id}。
基础的spring配置、springmvc配置、各层对象接口实现见前几节内容。
2./teachers
新建TeachersMvcResource资源类:
类中@Controller和@RequestMapping("/restMvc/teachers")注解声明了此类是Controller类并绑定到/restMvc/teachers地址,两个方法的@RequestMapping(method = { RequestMethod.GET })和@RequestMapping(method = { RequestMethod.POST })注解分别响应GET和POST请求,Contentbody里面的参数会被自动封装成Teacher teacher。
3./teacher/{id}
新增TeacherMvcResource类:
类中将服务绑定到/restMvc/teacher/{id}地址,方法的@RequestMapping(method = { RequestMethod.GET })、@RequestMapping(method = { RequestMethod.PUT })和@RequestMapping(method = { RequestMethod.DELETE })注解分别响应GET、PUT和POST请求,@PathVariable("id") Integer id接收{id}路径变量。
4.测试
rest接口可以使用服务端程序(URL、HttpClient库)、js(jquery ajax)进行访问。这里简单实用浏览器插件来测试。
使用FireFox的HttpRquester插件访问接口:
POST
POST http://localhost:8080/testRest/restMvc/teachers
Content-Type: application/x-www-form-urlencoded
name=a&age=1
-- response --
200 OK
Server: Apache-Coyote/1.1
Pragma: no-cache
Cache-Control: no-cache, no-store, max-age=0
Expires: Thu, 01 Jan 1970 00:00:00 GMT
Content-Type: application/json;charset=UTF-8
Content-Language: zh-CN
Transfer-Encoding: chunked
Date: Tue, 23 May 2017 02:31:56 GMT
{"teacher":{"id":1,"age":1,"name":"a"},"status":"y"}
GET
GET http://localhost:8080/testRest/restMvc/teachers
-- response --
200 OK
Server: Apache-Coyote/1.1
Pragma: no-cache
Cache-Control: no-cache, no-store, max-age=0
Expires: Thu, 01 Jan 1970 00:00:00 GMT
Content-Type: application/json;charset=UTF-8
Content-Language: zh-CN
Transfer-Encoding: chunked
Date: Tue, 23 May 2017 02:36:16 GMT
{"teachers":[{"id":1,"age":1,"name":"a"}]}
PUT
PUT http://localhost:8080/testRest/restMvc/teacher/1
Content-Type: application/x-www-form-urlencoded
name=b&age=2
-- response --
200 OK
Server: Apache-Coyote/1.1
Pragma: no-cache
Cache-Control: no-cache, no-store, max-age=0
Expires: Thu, 01 Jan 1970 00:00:00 GMT
Content-Type: application/json;charset=UTF-8
Content-Language: zh-CN
Transfer-Encoding: chunked
Date: Tue, 23 May 2017 02:37:41 GMT
{"teacher":{"id":1,"age":2,"name":"b"},"status":"y"}
GET
GET http://localhost:8080/testRest/restMvc/teacher/1
-- response --
200 OK
Server: Apache-Coyote/1.1
Pragma: no-cache
Cache-Control: no-cache, no-store, max-age=0
Expires: Thu, 01 Jan 1970 00:00:00 GMT
Content-Type: application/json;charset=UTF-8
Content-Language: zh-CN
Transfer-Encoding: chunked
Date: Tue, 23 May 2017 02:38:29 GMT
{"teacher":{"id":1,"age":2,"name":"b"}}
DELETE
DELETE http://localhost:8080/testRest/restMvc/teacher/1
-- response --
200 OK
Server: Apache-Coyote/1.1
Pragma: no-cache
Cache-Control: no-cache, no-store, max-age=0
Expires: Thu, 01 Jan 1970 00:00:00 GMT
Content-Type: application/json;charset=UTF-8
Content-Language: zh-CN
Transfer-Encoding: chunked
Date: Tue, 23 May 2017 02:38:46 GMT
{"status":"y"}
5.使用服务器程序访问rest
使用HttpClient库对rest接口进行测试,测试类TestRestMvc代码如下:
输出结果如下:
get http://localhost:8080/testRest/restMvc/teachers
{"teachers":[]}
post http://localhost:8080/testRest/restMvc/teachers
{"teacher":{"id":1,"age":1,"name":"a"},"status":"y"}
get http://localhost:8080/testRest/restMvc/teachers
{"teachers":[{"id":1,"age":1,"name":"a"}]}
get http://localhost:8080/testRest/restMvc/teacher/1
{"teacher":{"id":1,"age":1,"name":"a"}}
put http://localhost:8080/testRest/restMvc/teacher/1
{"teacher":{"id":1,"age":11,"name":"aa"},"status":"y"}
get http://localhost:8080/testRest/restMvc/teachers
{"teachers":[{"id":1,"age":11,"name":"aa"}]}
delete http://localhost:8080/testRest/restMvc/teacher/1
{"status":"y"}
get http://localhost:8080/testRest/restMvc/teachers
{"teachers":[]}
1.Restful
Restful样式的接口将功能抽象为资源resource路径映射,以HTTP GET /resource/{id} 的方式访问。主要分为以下几类接口:
地址 | 请求方法 | 说明 |
/resources | GET | 获取所有资源 |
/resources | POST | 创建新资源,content中包含资源内容 |
/resource/{id} | GET | 获取编号为id的资源 |
/resource/{id} | PUT | 更新编号为id的资源,content中包含资源内容 |
/resource/{id} | DELETE | 删除编号为id的资源 |
{id}称为路径变量,告诉restful你要对哪个资源进行查、改、删。
接口一般返回json/xml格式数据,方便服务端程序、浏览器脚本调用接口并处理返回数据。
按照Restful样式,teacher模块设计为两个资源地址:/restMvc/teachers和/restMvc/teachers/{id}。
基础的spring配置、springmvc配置、各层对象接口实现见前几节内容。
2./teachers
新建TeachersMvcResource资源类:
package com.sunbin.test.restMvc; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.view.json.MappingJackson2JsonView; import org.springframework.stereotype.Controller; import org.springframework.beans.factory.annotation.Autowired; import com.sunbin.test.teacher.pojo.Teacher; import com.sunbin.test.teacher.service.TeacherService; @Controller @RequestMapping("/restMvc/teachers") public class TeachersMvcResource { @Autowired private TeacherService teacherService; @RequestMapping(method = { RequestMethod.GET }) public ModelAndView get(HttpServletRequest arg0, HttpServletResponse arg1) throws Exception { System.out.println("RestMvc TeachersResource.get"); ModelAndView modelAndView = new ModelAndView( new MappingJackson2JsonView()); modelAndView.addObject("teachers", teacherService.list()); return modelAndView; } @RequestMapping(method = { RequestMethod.POST }) public ModelAndView post(Teacher teacher, HttpServletRequest arg0, HttpServletResponse arg1) throws Exception { ModelAndView modelAndView = new ModelAndView( new MappingJackson2JsonView()); System.out.println("RestMvc TeachersResource.post:" + teacher); teacherService.save(teacher); modelAndView.addObject("status", "y"); return modelAndView; } }
类中@Controller和@RequestMapping("/restMvc/teachers")注解声明了此类是Controller类并绑定到/restMvc/teachers地址,两个方法的@RequestMapping(method = { RequestMethod.GET })和@RequestMapping(method = { RequestMethod.POST })注解分别响应GET和POST请求,Contentbody里面的参数会被自动封装成Teacher teacher。
3./teacher/{id}
新增TeacherMvcResource类:
package com.sunbin.test.restMvc; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; 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.servlet.ModelAndView; import org.springframework.web.servlet.view.json.MappingJackson2JsonView; import org.springframework.stereotype.Controller; import org.springframework.beans.factory.annotation.Autowired; import com.sunbin.test.teacher.pojo.Teacher; import com.sunbin.test.teacher.service.TeacherService; @Controller @RequestMapping("/restMvc/teacher/{id}") public class TeacherMvcResource { @Autowired private TeacherService teacherService; @RequestMapping(method = { RequestMethod.GET }) public ModelAndView get(@PathVariable("id") Integer id, HttpServletRequest arg0, HttpServletResponse arg1) throws Exception { System.out.println("RestMvc TeacherResource.get:" + id); ModelAndView modelAndView = new ModelAndView( new MappingJackson2JsonView()); Teacher teacher = new Teacher(); teacher.setId(id); modelAndView.addObject("teacher", teacherService.get(teacher)); return modelAndView; } @RequestMapping(method = { RequestMethod.PUT }) public ModelAndView put(@PathVariable("id") Integer id, Teacher teacher, HttpServletRequest arg0, HttpServletResponse arg1) throws Exception { ModelAndView modelAndView = new ModelAndView( new MappingJackson2JsonView()); // 如果teacher中不含id属性,需要单独设置 // teacher.setId(id); System.out.println("RestMvc TeacherResource.put:" + id + ":" + teacher); teacherService.update(teacher); modelAndView.addObject("status", "y"); return modelAndView; } @RequestMapping(method = { RequestMethod.DELETE }) public ModelAndView delete(@PathVariable("id") Integer id, HttpServletRequest arg0, HttpServletResponse arg1) throws Exception { ModelAndView modelAndView = new ModelAndView( new MappingJackson2JsonView()); Teacher teacher = new Teacher(); teacher.setId(id); System.out.println("RestMvc TeacherResource.delete:" + id); teacherService.remove(teacher); modelAndView.addObject("status", "y"); return modelAndView; } }
类中将服务绑定到/restMvc/teacher/{id}地址,方法的@RequestMapping(method = { RequestMethod.GET })、@RequestMapping(method = { RequestMethod.PUT })和@RequestMapping(method = { RequestMethod.DELETE })注解分别响应GET、PUT和POST请求,@PathVariable("id") Integer id接收{id}路径变量。
4.测试
rest接口可以使用服务端程序(URL、HttpClient库)、js(jquery ajax)进行访问。这里简单实用浏览器插件来测试。
使用FireFox的HttpRquester插件访问接口:
POST
POST http://localhost:8080/testRest/restMvc/teachers
Content-Type: application/x-www-form-urlencoded
name=a&age=1
-- response --
200 OK
Server: Apache-Coyote/1.1
Pragma: no-cache
Cache-Control: no-cache, no-store, max-age=0
Expires: Thu, 01 Jan 1970 00:00:00 GMT
Content-Type: application/json;charset=UTF-8
Content-Language: zh-CN
Transfer-Encoding: chunked
Date: Tue, 23 May 2017 02:31:56 GMT
{"teacher":{"id":1,"age":1,"name":"a"},"status":"y"}
GET
GET http://localhost:8080/testRest/restMvc/teachers
-- response --
200 OK
Server: Apache-Coyote/1.1
Pragma: no-cache
Cache-Control: no-cache, no-store, max-age=0
Expires: Thu, 01 Jan 1970 00:00:00 GMT
Content-Type: application/json;charset=UTF-8
Content-Language: zh-CN
Transfer-Encoding: chunked
Date: Tue, 23 May 2017 02:36:16 GMT
{"teachers":[{"id":1,"age":1,"name":"a"}]}
PUT
PUT http://localhost:8080/testRest/restMvc/teacher/1
Content-Type: application/x-www-form-urlencoded
name=b&age=2
-- response --
200 OK
Server: Apache-Coyote/1.1
Pragma: no-cache
Cache-Control: no-cache, no-store, max-age=0
Expires: Thu, 01 Jan 1970 00:00:00 GMT
Content-Type: application/json;charset=UTF-8
Content-Language: zh-CN
Transfer-Encoding: chunked
Date: Tue, 23 May 2017 02:37:41 GMT
{"teacher":{"id":1,"age":2,"name":"b"},"status":"y"}
GET
GET http://localhost:8080/testRest/restMvc/teacher/1
-- response --
200 OK
Server: Apache-Coyote/1.1
Pragma: no-cache
Cache-Control: no-cache, no-store, max-age=0
Expires: Thu, 01 Jan 1970 00:00:00 GMT
Content-Type: application/json;charset=UTF-8
Content-Language: zh-CN
Transfer-Encoding: chunked
Date: Tue, 23 May 2017 02:38:29 GMT
{"teacher":{"id":1,"age":2,"name":"b"}}
DELETE
DELETE http://localhost:8080/testRest/restMvc/teacher/1
-- response --
200 OK
Server: Apache-Coyote/1.1
Pragma: no-cache
Cache-Control: no-cache, no-store, max-age=0
Expires: Thu, 01 Jan 1970 00:00:00 GMT
Content-Type: application/json;charset=UTF-8
Content-Language: zh-CN
Transfer-Encoding: chunked
Date: Tue, 23 May 2017 02:38:46 GMT
{"status":"y"}
5.使用服务器程序访问rest
使用HttpClient库对rest接口进行测试,测试类TestRestMvc代码如下:
package com.sunbin.test.restMvc; import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Map.Entry; import org.apache.http.HttpEntity; import org.apache.http.NameValuePair; import org.apache.http.ParseException; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpDelete; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.client.methods.HttpPut; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.message.BasicNameValuePair; import org.apache.http.util.EntityUtils; public class TestRestMvc { public static final String URL_BASE = "http://localhost:8080/testRest/restMvc/"; public static void main(String[] args) throws ParseException, ClientProtocolException, IOException { String module = "teacher"; String url = ""; DefaultHttpClient client = new DefaultHttpClient(); HttpEntity entity = null; String result = ""; Map map = null; HttpGet httpGet = null; HttpPost httpPost = null; HttpPut httpPut = null; HttpDelete httpDelete = null; url = URL_BASE + module + "s"; System.out.println("get\t " + url); httpGet = new HttpGet(url); result = EntityUtils.toString(client.execute(httpGet).getEntity()); System.out.println(result); url = URL_BASE + module + "s"; System.out.println("post\t " + url); httpPost = new HttpPost(url); map = new HashMap(); map.put("name", "a"); map.put("age", "1"); entity = new UrlEncodedFormEntity(getParam(map), "UTF-8"); httpPost.setEntity(entity); result = EntityUtils.toString(client.execute(httpPost).getEntity()); System.out.println(result); url = URL_BASE + module + "s"; System.out.println("get\t " + url); httpGet = new HttpGet(url); result = EntityUtils.toString(client.execute(httpGet).getEntity()); System.out.println(result); url = URL_BASE + module + "/1"; System.out.println("get\t " + url); httpGet = new HttpGet(url); result = EntityUtils.toString(client.execute(httpGet).getEntity()); System.out.println(result); url = URL_BASE + module + "/1"; System.out.println("put\t " + url); httpPut = new HttpPut(url); map = new HashMap(); map.put("name", "aa"); map.put("age", "11"); entity = new UrlEncodedFormEntity(getParam(map), "UTF-8"); httpPut.setEntity(entity); result = EntityUtils.toString(client.execute(httpPut).getEntity()); System.out.println(result); url = URL_BASE + module + "s"; System.out.println("get\t " + url); httpGet = new HttpGet(url); result = EntityUtils.toString(client.execute(httpGet).getEntity()); System.out.println(result); url = URL_BASE + module + "/1"; System.out.println("delete\t " + url); httpDelete = new HttpDelete(url); result = EntityUtils.toString(client.execute(httpDelete).getEntity()); System.out.println(result); url = URL_BASE + module + "s"; System.out.println("get\t " + url); httpGet = new HttpGet(url); result = EntityUtils.toString(client.execute(httpGet).getEntity()); System.out.println(result); } public static List<NameValuePair> getParam(Map parameterMap) { List<NameValuePair> param = new ArrayList<NameValuePair>(); Iterator it = parameterMap.entrySet().iterator(); while (it.hasNext()) { Entry parmEntry = (Entry) it.next(); param.add(new BasicNameValuePair((String) parmEntry.getKey(), (String) parmEntry.getValue())); } return param; } }
输出结果如下:
get http://localhost:8080/testRest/restMvc/teachers
{"teachers":[]}
post http://localhost:8080/testRest/restMvc/teachers
{"teacher":{"id":1,"age":1,"name":"a"},"status":"y"}
get http://localhost:8080/testRest/restMvc/teachers
{"teachers":[{"id":1,"age":1,"name":"a"}]}
get http://localhost:8080/testRest/restMvc/teacher/1
{"teacher":{"id":1,"age":1,"name":"a"}}
put http://localhost:8080/testRest/restMvc/teacher/1
{"teacher":{"id":1,"age":11,"name":"aa"},"status":"y"}
get http://localhost:8080/testRest/restMvc/teachers
{"teachers":[{"id":1,"age":11,"name":"aa"}]}
delete http://localhost:8080/testRest/restMvc/teacher/1
{"status":"y"}
get http://localhost:8080/testRest/restMvc/teachers
{"teachers":[]}
发表评论
-
spring mvc(五)CRUD及PUT/DELETE参数
2017-05-22 17:34 3900使用springmvc实现基于页面的增删改查,只要对Contr ... -
spring mvc(四)返回json
2017-05-15 16:54 2687实现springmvc返回json内容。 1.指定视图类型 ... -
spring mvc(三)注解
2017-05-15 15:19 554注解实现spring mvc的Controlle、servic ... -
spring mvc(二)注入spring bean
2017-05-12 18:52 2044向Controller中注入spring管理的service、 ... -
spring4学习(三)在非Spring类中获取Bean
2017-05-05 18:19 6619在不是spring管理的对象、无法直接取得Applicatio ... -
spring4学习(二)注解配置
2017-05-05 17:41 765使用注解实现spring配置。 个人感觉适合在小团队开发或模块 ... -
spring4学习(一)基础配置
2017-05-05 17:13 782记录下Spring4的基础用法。 环境:Myeclipse10 ... -
spring mvc(一)HelloWorld
2017-05-09 11:27 585记录下SpringMvc4的用法。 环境:Myeclipse1 ... -
Spring Security3边学边写(N)会话管理和并行控制
2013-10-09 13:05 12961在开发系统认证授权时,经常会碰到需要控制单个用户重复登录次数或 ... -
spring3+struts2+mybatis3框架搭建
2013-07-12 01:08 0spring3.2.3 struts2.3.14.2 myba ... -
[转]SpringMVC访问静态资源
2013-07-10 12:40 6606本文转自 http://www.cnblogs.com/fan ... -
Spring Security3边学边写(三)数据库存储角色、权限信息
2013-07-09 01:17 01.创建表 2.创建pojo类 3.创建security相 ... -
Spring Security3边学边写(二)数据库存储用户信息及自定义页面
2013-07-09 01:13 01.创建表 2.创建pojo类 3.配置 修改src/ap ... -
Spring Security3边学边写(一)第一步
2013-07-07 10:55 0之前做过的web系统,认 ...
相关推荐
spring rest 增删改查(put\delete\post\get)简单示例详细讲解,亲测可用。spring rest 增删改查(put\delete\post\get)简单示例详细讲解,亲测可用。spring rest 增删改查(put\delete\post\get)简单示例详细...
Spring MVC REST Demo
通过spring 3.0 的mvc 实现rest功能,代码是myeclipse工程,包括依赖包
二、spring mvc 核心类与接口 三、spring mvc 核心流程图 四、spring mvc DispatcherServlet说明 五、spring mvc 双亲上下文的说明 六、springMVC-mvc.xml 配置文件片段讲解 七、spring mvc 如何访问到静态的文件,...
二、spring mvc 核心类与接口 三、spring mvc 核心流程图 四、spring mvc DispatcherServlet说明 五、spring mvc 双亲上下文的说明 六、springMVC-mvc.xml 配置文件片段讲解 七、spring mvc 如何访问到静态的文件,...
spring mvc 包括 实现各种结构url 和get post 方式 跳转传参 提交等实例,有注释 是初学springmvc 必备入门级 参考.只需5分钟,看了代码就能让你掌握 spring mvc rest 的各种实现
spring 官网上的一个spring mvc实例很有学习价值
用STS(Spring Tool Suite)开发的,spring mvc + spring security 实现的最简单的登录系统,无数据库。
使用Spring MVC创建REST服务简单例子
DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> 3 4 5 6 <bean id="viewResolver" class="org.springframework.web.servlet.view....
运行好,调试好的spring3.0 mvc_rest_demo.war ****************************** * 运行说明 ****************************** 1. 直接部署dist/springmvc_rest_demo.war至tomcat并启动服务器 2. 访问...
赠送jar包:springfox-spring-webmvc-3.0.0.jar; 赠送原API文档:springfox-spring-webmvc-3.0.0-javadoc.jar; 赠送源代码:springfox-spring-webmvc-3.0.0-sources.jar; 赠送Maven依赖信息文件:springfox-...
CXF整合spring实现REST接口开发
Spring MVC + Mybatis+Spring实现的个人博客系统基于SSM实现的个人博客系统.zip
赠送jar包:springfox-spring-webmvc-3.0.0.jar; 赠送原API文档:springfox-spring-webmvc-3.0.0-javadoc.jar; 赠送源代码:springfox-spring-webmvc-3.0.0-sources.jar; 赠送Maven依赖信息文件:springfox-...
Spring+Spring MVC+Spring JDBC+MySql实现简单登录注册
本文详细介绍spring MVC的原理和开发心得体会。
二、spring mvc 核心类与接口 三、spring mvc 核心流程图 四、spring mvc DispatcherServlet说明 五、spring mvc 双亲上下文的说明 六、springMVC-mvc.xml 配置文件片段讲解 七、spring mvc 如何访问到静态的文件,...
spring mvc文件上传,通过监听器实现进度条
使用Spring MVC3基于注解的形式实现的REST小例子 部署运行: 1. 将工程导入eclipse 2. 添加tomcat属性 工程(右键)——> tomcat project——> update context 3. 启动tomcat 4. 在浏览器中输入...