SpringMVC 构建Restful风格 及问题处理
基本的请求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风格 及问题处理的更多相关文章
- Spring Boot构建 RESTful 风格应用
Spring Boot构建 RESTful 风格应用 1.Spring Boot构建 RESTful 风格应用 1.1 实战 1.1.1 创建工程 1.1.2 构建实体类 1.1.4 查询定制 1.1 ...
- 构建RESTful风格的WCF服务
构建RESTful风格的WCF服务 RESTful Wcf是一种基于Http协议的服务架构风格. 相较 WCF.WebService 使用 SOAP.WSDL.WS-* 而言,几乎所有的语言和网络平台 ...
- lucene构建restful风格的简单搜索引擎服务
来自于本人博客: lucene构建restful风格的简单搜索引擎服务 本人的博客如今也要改成使用lucene进行全文检索的功能,因此在这里把代码贴出来与大家分享 一,文件夹结构: 二,配置文件: 总 ...
- springMVC+json构建restful风格的服务
首先.要知道什么是rest服务,什么是rest服务呢? REST(英文:Representational State Transfer,简称REST)描写叙述了一个架构样式的网络系统.比方 web 应 ...
- SpringBoot实战(一)之构建RestFul风格
RestFul风格是一种非常流行的架构风格,相关实战可以参考我的这篇博客:SSM框架之RestFul示例 论文可参考:https://www.ics.uci.edu/~fielding/pubs/di ...
- Spring Boot 中 10 行代码构建 RESTful 风格应用
RESTful ,到现在相信已经没人不知道这个东西了吧!关于 RESTful 的概念,我这里就不做过多介绍了,传统的 Struts 对 RESTful 支持不够友好 ,但是 SpringMVC 对于 ...
- Spring Boot2 系列教程(三十一)Spring Boot 构建 RESTful 风格应用
RESTful ,到现在相信已经没人不知道这个东西了吧!关于 RESTful 的概念,我这里就不做过多介绍了,传统的 Struts 对 RESTful 支持不够友好 ,但是 SpringMVC 对于 ...
- SpringMVC实现Restful风格的WebService
1.环境 JDK7 MyEclipse2014 tomcat8 maven 3.3.3 spring4.1.4 2.创建maven工程 使用MyEclipse创建maven工程的方式可以参考这篇博文( ...
- [五]SpringMvc学习-Restful风格实现
1.Restful风格的资源URL 无后缀资源的访问(csdn用法) 2.SpringMvc对Rest风格的支持 2.1将 /*.do改为/ 2.2 3.@PathVariable获取Url变量 @R ...
随机推荐
- js添加删除元素内容
<body> <div id="div" style="background: yellow;width:200px;height:200px;&quo ...
- How to get the mapping relationship between two columns in a table
If a table have column A and B Count(distinct A) as Da Count(distinct B) as Db Count(distinct A, B) ...
- 使用Visual Studio 2017开发python,并在iis上部署Python Django
作为宇宙第一IDE,怎么可以不支持python开发呢? 1.Visual Studio Installer 扩展Python开发 开始菜单中打开Visual Studio Installer,点修改. ...
- js把星期由数字转换为星期名称
//由星期的数字转换为星期的名称,date格式:2016-01-07,用"-"分割 function toWeekName(date) { var newDate = new Da ...
- 面试题1 -- Java 中,怎么在格式化的日期中显示时区?
使用SimpleDateFormat来实现格式化日期 import java.text.SimpleDateFormat; import java.util.Date; public class Da ...
- Tomcat会话保持之session server
author:JevonWei 版权声明:原创作品 Tomcat会话保持之session server 拓扑图 环境 tomcatA 172.16.253.108 tomcatB 172.16.253 ...
- 王爽汇编习题2.2(1):给定地址段为0001H,仅通过变化偏移地址寻址,CPU的寻址范围为____到____
此题解题背景默认为8080型CPU,地址总线为16根.(8080-16,8086-20,8088-20,80286-24,80386-32) 16根地址总线寻址能力:(2 ** 16) / 1024 ...
- jackjson和fastjson进行Bean与json互换
1.jackjson 1.bean到json ObjectMapper m = new ObjectMapper(); String value = m.writeValueAsString(bean ...
- stylus选中hover元素的兄弟元素下的子元素
stylus设置兄弟元素样式:鼠标浮动在 .video-li 元素上时,.video-li 兄弟中 .video-info 下的 .word 显示. .video-li &:hover ~ . ...
- spring整合mybatis错误:class path resource [config/spring/springmvc.xml] cannot be opened because it does not exist
spring 整合Mybatis 运行环境:jdk1.7.0_17+tomcat 7 + spring:3.2.0 +mybatis:3.2.7+ eclipse 错误:class path reso ...