盐城网站平台建设,深圳网站开发怎么样,物业网站宣传册怎么做,直播app软件开发定制目录 1. rest风格基础2. 开启方法3. 实战练习 1. rest风格基础
我们都知道GET、POST、PUT、DELETE分别对应查、增、改、删除
虽然Postman这些工具可以直接发送GET、POST、PUT、DELETE请求。但是RequestMapping并不支持PUT和DELETE请求操作。需要我们手动开启
2. 开启方法
P… 目录 1. rest风格基础2. 开启方法3. 实战练习 1. rest风格基础
我们都知道GET、POST、PUT、DELETE分别对应查、增、改、删除
虽然Postman这些工具可以直接发送GET、POST、PUT、DELETE请求。但是RequestMapping并不支持PUT和DELETE请求操作。需要我们手动开启
2. 开启方法
PUT和DELETE还是通过methodPOST进行请求但是需要添加对应的隐藏域_methodPUT/DELETE
然后配置文件进行参数的配置spring.mvc.hiddenmethod.filter.enabledtrue
3. 实战练习
添加如下参数到application.properties文件。表示开启隐藏方法使用
spring.mvc.hiddenmethod.filter.enabledtrue编写Controller。在一个请求路径上分别定义了GET、POST、PUT、DELETE四种method
其中GetMapping、PostMapping、PutMapping、DeleteMapping等同于RequestMapping对应的method。如下所示
package com.hh.springboottest.myController;import org.springframework.web.bind.annotation.*;RestController
public class HelloController {// GetMapping(/user)RequestMapping(value/user, method RequestMethod.GET)public String getUser() {return get user;}// PostMapping(/user)RequestMapping(value/user, method RequestMethod.POST)public String saveUser() {return save user;}// PutMapping(/user)RequestMapping(value/user, method RequestMethod.PUT)public String editUser() {return edit user;}// DeleteMapping(/user)RequestMapping(value/user, method RequestMethod.DELETE)public String deleteUser() {return delete user;}
}编写resources/static/index.html页面。PUT和DELETE还是通过methodPOST进行请求但是需要添加对应的隐藏域_methodPUT/DELETE。还支持的一个隐藏域是_methodPATCH
!DOCTYPE html
html langzh-CN
headmeta charsetUTF-8titletest title/title
/head
bodyform action/user methodgetinput valuerest get提交 typesubmit /
/form
form action/user methodpostinput valuerest post提交 typesubmit /
/form
form action/user methodpostinput name_method typehidden valuePUT /input valuerest put提交 typesubmit /
/form
form action/user methodpostinput name_method typehidden valueDELETE /input valuerest delete提交 typesubmit /
/form/body
/html然后访问http://localhost:8080/点击rest get提交。如下所示 得到的结果如下