SpringMVC中REST风格的请求有哪些

发布时间:2020-12-02 17:38:53 作者:Leah
来源:亿速云 阅读:130

这篇文章给大家介绍SpringMVC中REST风格的请求有哪些,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。

一、 在HTTP 协议里面,四个表示操作方式的动词:GET、POST、PUT、DELETE。

它们分别对应四种基本操作:

  1、GET ====== 获 取资源
  2、POST ======新建资源
  3、PUT======= 更新资源
  4、DELETE==== 删除资源

二、REST:即 Representational State Transfer。(资源)表现层状态转化。是目前最流行的一种互联网软件架构。它结构清晰、符合标准、易于理解、扩展方便, 所以正得到越来越多网站的采用。

我们可以通过rest风格占位符方式,利用@PathVariable注解将占位符的值赋给调用方法参数,实现结果:

/某路径/1 HTTP GET : 得到 id = 1 的 一条数据
/某路径/1 HTTP DELETE: 删除 id = 1的 一条数据
/某路径/1   HTTP PUT: 更新id = 1的 一条数据
/某路径 HTTP POST: 新增一条数据

实现方式(REST风格四种请求方式的调用):

我们通过@RequestMapping映射请求中的method参数实现四种请求方式的调用,以下为示例代码。

GET请求:

@RequestMapping(value="/student",method=RequestMethod.GET)
 public ModelAndView toAddPage(){
  ModelAndView mView=new ModelAndView();
  mView.addObject("employee",new Employee());
  mView.setViewName("add-stu");
  mView.addObject("departments", departmentDao.getDepartments());
  return mView;
 }

POST请求:

@RequestMapping(value="/student",method=RequestMethod.POST)
 public String addStu(Employee employee){
  employeeDao.save(employee);
  return "redirect:/show" ;
 }

DELETE请求:

@RequestMapping(value="/student/{id}",method=RequestMethod.DELETE)
 public String deleteStu(@PathVariable(value="id") Integer id){
  employeeDao.delete(id);
  return "redirect:/show" ;
 }

PUT请求:

@RequestMapping(value="/student",method=RequestMethod.PUT)
 public String Update(@RequestParam(value="id")Integer id,Employee employee){
  employeeDao.save(employee);
  return "redirect:/show" ;
 }

三、将POST请求转化为put请求和delele请求

1.在web.xml文件中配置HiddenHttpMethodFilter过滤器:

<!-- HiddenHttpMethodFilter过滤器可以将POST请求转化为put请求和delete请求! -->
   <filter>
    <filter-name>hiddenHttpMethodFilter</filter-name>
    <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
    </filter>
    <filter-mapping>
    <filter-name>hiddenHttpMethodFilter</filter-name>
    <url-pattern>/*</url-pattern>
   </filter-mapping>

2.在表单域中需要携带一个name值为_method,value值为put或者delete的参数,如下所示:

<form action="" method="post">
 <input type="hidden" name="_method" value="delete">
</form>
<form:form action="${pageContext.request.contextPath}/student" method="post" modelAttribute="employee">
  <c:if test="${empty employee.id }">
   姓名:<form:input path="lastName"/><br>
  </c:if>
  <c:if test="${!empty employee.id }">
   姓名:<form:input path="lastName" readonly="true"/><br>
    <form:hidden path="id"/>
    <input type="hidden" name="_method" value="put">
  </c:if>
  邮箱:<form:input path="email"/><br>
  <%
   Map<String,Object>map=new HashMap<String,Object>();
   map.put("1","Male");
   map.put("0", "Female");
   request.setAttribute("genders", map);
  %>
  性别:<form:radiobuttons path="gender" items="${genders}"/><br>
  部门:<form:select path="department.id" items="${departments}" itemValue="id" itemLabel="departmentName"></form:select><br>
  <input type="submit" value="提交">
 </form:form>

最后在Controller层调用即可。根据@RequestMapping的value值以及携带的参数、请求方式查找匹配函数。

关于SpringMVC中REST风格的请求有哪些就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

推荐阅读:
  1. springmvc restful风格遇到的问题
  2. springMVC rest风格

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

springmvc rest st

上一篇:Android应用中的惯性滚动怎么利用 ScrollView取消

下一篇:在java 中使用序列化时出现NotSerializableException报错如何解决

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》