在定义一个Rest接口时一般会利用GET、POST、PUT、DELETE来实现数据的增删改查;这几种方式有的须要传递参数,后台开发人员必须对接收到的参数进行参数验证来确保程序的健壮性java
GET:通常用于查询数据,采用明文进行传输,通常用来获取一些无关用户信息的数据spring
POST:通常用于插入数据浏览器
PUT:通常用于数据更新app
DELETE:通常用于数据删除;通常都是进行逻辑删除(即:仅仅改变记录的状态,而并不是真正的删除数据)spring-boot
请求URL:localhost:8080/hello/id 获取id值
url
实现代码以下:code
@RestController publicclass HelloController { @RequestMapping(value="/hello/{id}/{name}",method= RequestMethod.GET) public String sayHello(@PathVariable("id") Integer id,@PathVariable("name") String name){ return"id:"+id+" name:"+name; } }
在浏览器中 输入地址:接口
localhost:8080/hello/100/hello
开发
输出:class
id:81name:hello
获取url参数值,默认方式,须要方法参数名称和url参数保持一致
请求URL:localhost:8080/hello?id=1000
@RestController publicclass HelloController { @RequestMapping(value="/hello",method= RequestMethod.GET) public String sayHello(@RequestParam Integer id){ return"id:"+id; } }
输出:id:100
url中有多个参数时,如:
localhost:8080/hello?id=98&&name=helloworld
具体代码以下:
@RestController publicclass HelloController { @RequestMapping(value="/hello",method= RequestMethod.GET) public String sayHello(@RequestParam Integer id,@RequestParam String name){ return"id:"+id+ " name:"+name; } }
获取url参数值,执行参数名称方式
localhost:8080/hello?userId=1000
@RestController publicclass HelloController { @RequestMapping(value="/hello",method= RequestMethod.GET) public String sayHello(@RequestParam("userId") Integer id){ return"id:"+id; } }
输出:id:100