基本的请求URL:

/person/{id}  GET     得到id的person

/person   POST        新增person

/person/{id}  PUT      更新id的person

/person/{id}  DELETE    删除id的person

源码地址:https://github.com/loveincode/ssm

1. 查询 GET

@RequestMapping(value = "/{id}", method = RequestMethod.GET)

 @RequestMapping(value = "/{id}", method = RequestMethod.GET)
public @ResponseBody String show(@PathVariable Integer id, HttpServletRequest request,
HttpServletResponse response) {
log.info("ENTER " + ToolsUtil.getMethodName());
ResultVO resultVO = new ResultVO();
Person person = new Person();
person = personService.findById(id);
if (person != null) {
resultVO.setSuccess(true);
resultVO.setData(person);
resultVO.setMessage("查询成功");
} else {
resultVO.setMessage("查询失败,没找到id=" + id + "的Person");
}
return resultVO.toString();
}

测试:

2. 新增 POST

@RequestMapping(method = RequestMethod.POST)

 @RequestMapping(method = RequestMethod.POST)
public @ResponseBody String add(@ModelAttribute("person") Person person, HttpServletRequest request,
HttpServletResponse response) {
ResultVO resultVO = new ResultVO();
System.out.println(person.toString());
if (person.getName() != null) {
personService.add(person);
resultVO.setSuccess(true);
resultVO.setMessage("插入成功");
} else {
resultVO.setMessage("name为空,插入失败");
}
return resultVO.toString();
}

测试:

3. 更新 PUT

@RequestMapping(value = "/{id}", method = RequestMethod.PUT)

@RequestMapping(value = "/{id}", method = RequestMethod.PUT)
@ResponseBody
public String update(@PathVariable Integer id, @ModelAttribute("person") Person person, HttpServletRequest request,
HttpServletResponse response) {
ResultVO resultVO = new ResultVO();
Person oldperson = personService.findById(id);
if (oldperson != null) {
if (person.getName() != null) {
person.setId(oldperson.getId());
personService.update(person);
resultVO.setMessage("更新成功");
} else {
resultVO.setMessage("更新失败,name为空");
}
} else {
resultVO.setMessage("更新失败,不存在 id = " + id + "的Person");
}
return resultVO.toString();
}

测试:

4. 删除 DELETE

@RequestMapping(value = "/{id}", method = RequestMethod.DELETE)

 @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
@ResponseBody
public String delete(@PathVariable Integer id, HttpServletRequest request, HttpServletResponse response) {
ResultVO resultVO = new ResultVO();
Person person = new Person();
person = personService.findById(id);
if (person != null) {
resultVO.setSuccess(true);
personService.delete(id);
resultVO.setMessage("删除成功");
} else {
resultVO.setMessage("删除失败,不存在id = " + id + "的Person");
}
return resultVO.toString();
}

测试:

5. 问题

5.1 SpringMVC接收不了PUT、DELETE  bodydata 解决方法

需要在web.xml中加上:

    <filter>
<filter-name>HttpMethodFilter</filter-name>
<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>HttpMethodFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

然后在请求PUT、DELETE的时候,使用POST请求,在body中加入_method 参数,参数为PUT或DELETE,即可完成对PUT、DELETE的处理。

例如:

strtus2 构建restful风格 https://www.ibm.com/developerworks/cn/java/j-lo-struts2rest/

SpringMVC 构建Restful风格 及问题处理的更多相关文章

  1. Spring Boot构建 RESTful 风格应用

    Spring Boot构建 RESTful 风格应用 1.Spring Boot构建 RESTful 风格应用 1.1 实战 1.1.1 创建工程 1.1.2 构建实体类 1.1.4 查询定制 1.1 ...

  2. 构建RESTful风格的WCF服务

    构建RESTful风格的WCF服务 RESTful Wcf是一种基于Http协议的服务架构风格. 相较 WCF.WebService 使用 SOAP.WSDL.WS-* 而言,几乎所有的语言和网络平台 ...

  3. lucene构建restful风格的简单搜索引擎服务

    来自于本人博客: lucene构建restful风格的简单搜索引擎服务 本人的博客如今也要改成使用lucene进行全文检索的功能,因此在这里把代码贴出来与大家分享 一,文件夹结构: 二,配置文件: 总 ...

  4. springMVC+json构建restful风格的服务

    首先.要知道什么是rest服务,什么是rest服务呢? REST(英文:Representational State Transfer,简称REST)描写叙述了一个架构样式的网络系统.比方 web 应 ...

  5. SpringBoot实战(一)之构建RestFul风格

    RestFul风格是一种非常流行的架构风格,相关实战可以参考我的这篇博客:SSM框架之RestFul示例 论文可参考:https://www.ics.uci.edu/~fielding/pubs/di ...

  6. Spring Boot 中 10 行代码构建 RESTful 风格应用

    RESTful ,到现在相信已经没人不知道这个东西了吧!关于 RESTful 的概念,我这里就不做过多介绍了,传统的 Struts 对 RESTful 支持不够友好 ,但是 SpringMVC 对于 ...

  7. Spring Boot2 系列教程(三十一)Spring Boot 构建 RESTful 风格应用

    RESTful ,到现在相信已经没人不知道这个东西了吧!关于 RESTful 的概念,我这里就不做过多介绍了,传统的 Struts 对 RESTful 支持不够友好 ,但是 SpringMVC 对于 ...

  8. SpringMVC实现Restful风格的WebService

    1.环境 JDK7 MyEclipse2014 tomcat8 maven 3.3.3 spring4.1.4 2.创建maven工程 使用MyEclipse创建maven工程的方式可以参考这篇博文( ...

  9. [五]SpringMvc学习-Restful风格实现

    1.Restful风格的资源URL 无后缀资源的访问(csdn用法) 2.SpringMvc对Rest风格的支持 2.1将 /*.do改为/ 2.2 3.@PathVariable获取Url变量 @R ...

随机推荐

  1. nRF24LE1/nRF31512烧录驱动开发

    一丶协议分析 这两种芯片都是programming through SPI,烧录要用到的引脚有 SPI_MOSI_Port :数据输入: SPI_MISO_Port :数据输出: SPI_SCLK_P ...

  2. LINQ Distinct()

    using System; using System.Collections.Generic; using System.Linq; namespace LinqTest { class Progra ...

  3. 【Tomcat】shell获得war包

    功能: 将maven项目打包复制到tomcat/webapps set git=C:\Users\zhengwenqiang\git set tomcat=e:\tomcat7.0.64 c: cd ...

  4. oracle运行速度与效率高的秘密

    使用过Oracle的人都知道,Oracle的运行速度与效率,在同类数据库中是名列前茅的,特别是对大量数据进行访问时,更加有出色的表现.那么,Oracle数据库是靠什么实现的呢?笔者下面将通过一系列的文 ...

  5. vue2项目使用axios发送请求

    前言:在Vue1.0的时候有一个官方推荐的 ajax 插件 vue-resource,但是自从 Vue 更新到 2.0 之后,官方就不再更新 vue-resource. 目前主流的 Vue 项目,都选 ...

  6. Ubuntu16.04+CUDA8.0+CUNN5.1+caffe+tensorflow+Theano

    title: Ubuntu 16.04+CUDA8.0+CUNN5.1+caffe+tensorflow+Theano categories: 深度学习 tags: [深度学习框架搭建] --- 前言 ...

  7. Ext.grid.EditorGridPanel点击单元格添加菜单栏

    1.定义菜单栏需要的全局变量 var khbm; var type; 2.新建一个菜单栏 var smenu = new Ext.menu.Menu({ id:"sMenu", i ...

  8. .Netcore之日志组件Log4net、Nlog性能比较

    转载请注明出处http://www.cnblogs.com/supernebula/p/7506993.html .Netcore之Log4net.Nlog性能比较 最近在写一个开源.netcore ...

  9. chrome保持元素hover,active状态

    审查元素,选中需要hover的标签 点击"Styles"菜单中的":hov",弹出 Force element state 选中相应的 :hover :acti ...

  10. Jquery 绑定标签事件

    为子元素绑定: $('#foreachResult').delegate('td', 'click', function () {            alert($(this).text());  ...