Spring MVC搭建REST风格网站
REST是表述性状态转移的意思。REST核心是以资源为中心。
比如,URI是统一资源标识符,URL是一种URI,称为统一资源定位符。现在很多网站设计的URL,没有以资源为中心,没有体现URI的标识本质。比如,有一个URL:/user?id=1&name=lcl
,其中id参数是用来定位一个“id=1的用户”,这就有悖于URI的本质,既然URI本身可以标识一个资源,那么就不应该借助额外参数来标识这个资源,所以这是一个非REST风格的URL。
REST提倡让URI回归资源表述的本质。所以上面的URL可以改成:/user/1?name=lcl
。
Controller:
package com.huanle.controller;import java.net.BindException;import javax.servlet.http.HttpServletResponse;import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
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.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;import com.huanle.model.User;@Controller
@RequestMapping("/user")
public class UserController {@RequestMapping( path= "/{id}" , method = RequestMethod.GET)public String getUser(@PathVariable long id){return "user";}@RequestMapping( path= "/{id}" , method = RequestMethod.PUT)@ResponseStatus( HttpStatus.NO_CONTENT )public void putUser(@PathVariable long id){//TODO:save user}@RequestMapping( path= "/{id}" , method = RequestMethod.DELETE)@ResponseStatus( HttpStatus.NO_CONTENT )public void deleteUser(@PathVariable long id){//TODO:delete user}@RequestMapping(method = RequestMethod.POST)@ResponseStatus( HttpStatus.CREATED )public void addUser(@Validated User user,BindingResult result,HttpServletResponse response) throws BindException{if(result.hasErrors()){throw new BindException();}//TODO: saveuser.setName("lcl");response.setHeader("Location", "/user/"+user.getId());}}
- 参考《spring 实战》 278页
Spring MVC搭建REST风格网站相关推荐
- [转] 使用Spring MVC构建REST风格WEB应用
原文地址:http://fancy888.iteye.com/blog/1629120 对于运行在网络上的MIS系统而言,处理数据的是整个系统的主要任务,翻开程序我们可以看到,80%以上的代码都在处理 ...
- Spring MVC 搭建web项目示例
转载自:Bird鸟人 的博文<Spring MVC 搭建web项目示例> https://blog.csdn.net/wcc27857285/article/details/8174903 ...
- Spring MVC开发RESTful风格的URI
一.写在前面 RESTful结构可参考博文:https://blog.csdn.net/codejas/article/details/79799386 我们知道在HTTP 协议中,有四种操作方式的动 ...
- spring mvc和rest风格小例子
之前在新一个项目中用了spring3 的mvc开发,用得很爽,不过当时想找些入门的小例子时,找了好久也没找到, 现在写个简单的小例子出来给初学者学习下. srping3也支持rest,所以例子也包括这 ...
- 搭建Spring MVC详解
文章目录 一.Spring 二.component-scan 三.搭建Spring MVC(一) 四.搭建Servlet 五.搭建Spring MVC(二) 六.搭建Spring MVC(三) 一.S ...
- 【Java学习路线之JavaWeb】Spring MVC框架入门教程
文章目录 读者 阅读条件 MVC设计模式简介 JSP+JavaBean Servlet+JSP+JavaBean MVC优缺点 优点 缺点 Spring MVC是什么 Spring MVC优点 第一个 ...
- Spring MVC详解(学习总结)
一.Sprig MVC简介 1.1介绍 1.2MVC是什么 二.Spring MVC实现原理 2.1核心组件 2.2工作流程 三.第一个Spring MVC 四.常用注解 五.参数绑定 5.1URL风 ...
- 【J2EE实验-3】Spring MVC
一.实验目的 1.理解Spring MVC的处理流程 2. 掌握Spring MVC的基本使用 3. 掌握Spring MVC的参数传递 二.实验学时及环境 实验学时:2 实验环境:IDEA,JDK8 ...
- Spring MVC url提交参数和获取参数
普通URL提交参数 该格式url为:url.do?param1=mahc¶m2=8888.00 需要在上文中的HelloController对象添加方法如下: ? 1 2 3 4 5 ...
最新文章
- AI视频行为分析系统项目复盘——技术篇3:tensorRT技术梳理
- Toward Multimodal Image-to-Image Translation(BicycleGAN)图像一对多转换测试
- C++ 退出双层for循环,解决 break、return、continue无法实现问题
- HDFS分布式文件系统理论知识
- 大数据应用项目创新大赛_温州首届大数据应用创新大赛决赛名单公布!有你的单位吗?...
- Neo4j:使隐式关系成为显式和双向关系
- Sublime Text 2中的正则表达式搜索替换
- sps2013安装错误
- MircoPython 的组件扩展方法
- mysql populate_BeanUtils.populate 的作用
- 传统研发团队的敏捷转型实践之路
- iOS UIScrollView 无法滚动 没有弹簧效果解决方案
- oracle开机自启
- 前端工程化-husky+commitizen+ 自定义cz-customizable适配器 git 提交代码规范化
- C语言实现贪吃蛇小游戏(详解)
- 润和软件全面参与OLA联盟智能家居专委会举办第三次联调测试活动
- Android开发面试题 71道经典题目
- 千兆/万兆单向传输网卡
- 复制文件或文件夹时出错(无法复制***:没有足够的可用磁盘空间。) .
- abp vNext nginx 反向代理设置