rest(representational state transfer),表现形式状态转换,简单来说是访问网络资源的格式。
rest风格优点:隐藏资源的访问行为,无法通过地址得知资源进行的是什么操作,书写简化。
这只是一种风格,一种约束方式,不是规范,可以不遵守,仅仅是提供一种访问资源的思想。
Controller层代码:
package org.example.Controller;
import org.example.pojo.user;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
@Controller
@RequestMapping("/users")
public class userRestful {
@RequestMapping(method = RequestMethod.POST) //写在类上设置浏览器访问具体路径,想访问该方法只能用post请求
@ResponseBody//设置返回值类型,就是把返回的内容作为响应数据
public String save(@RequestBody user user){
System.out.println("springMVC save...");
return "{'a':'save'}";
}
@RequestMapping(value = "/{id}",method = RequestMethod.DELETE) //设置浏览器访问路径
@ResponseBody//设置返回值类型,就是把返回的内容作为响应数据
public String delete(@PathVariable Integer id){
System.out.println("springMVC delete..."+id);
return "{'b':'delete'}";
}
@RequestMapping(method = RequestMethod.PUT)
@ResponseBody
public String update(@RequestBody user user){
System.out.println("springMVC update..."+user);
return "{'c':'update'}";
}
@RequestMapping(value = "/{id}",method = RequestMethod.GET)
@ResponseBody
public String getById(@PathVariable Integer id){
System.out.println("springMVC getById..."+id);
return "{'d':'getById'}";
}
@RequestMapping(method = RequestMethod.GET)
@ResponseBody
public String getAll(){
System.out.println("springMVC getAll...");
return "{'e':'getAll'}";
}
}
新增方法save 只能用post请求访问 http://localhost:3306/usershttp://localhost:3306/users
删除方法delete 只能用delete请求并传入id参数进行访问http://localhost:3306/usershttp://localhost:3306/users/1
更新方法update 只能用put请求并传入一个对象进行访问http://localhost:3306/usershttp://localhost:3306/users
查询方法只能用get请求访问,getAll查询所有,不需要传参;
http://localhost:3306/usershttp://localhost:3306/users
getById根据id查询,需要传入id值, http://localhost:3306/usershttp://localhost:3306/users/1
@PathVariable该注解是把参数以路径变量的形式写入路径,而不是以键值对的形式写在?后面。该注解仅仅是说明变量来源于路径,没说在路径的哪个位置。需要在设置路径时指明位置
上面这种rest风格的书写我们看着会觉得有点繁杂,我们可以简化一下:
package org.example.Controller;
import org.example.pojo.book;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
//@Controller
//@ResponseBody
@RestController
@RequestMapping("/books")
public class booksRestful {
//@RequestMapping(method = RequestMethod.POST) //写在类上设置浏览器访问具体路径,想访问该方法只能用post请求
@PostMapping// 此注解完全可以代替上面的注解
public String save(){
System.out.println("springMVC save...");
return "{'a':'save'}";
}
//@RequestMapping(value = "/{id}",method = RequestMethod.DELETE) //设置浏览器访问路径
@DeleteMapping("/{id}")
public String delete(@PathVariable Integer id){ //该注解是把参数以路径变量的形式写入路径,而不是以键值对的形式写在?后面。该注解仅仅是说明变量来源于路径,没说在路径的哪个位置。需要在设置路径时指明位置
System.out.println("springMVC delete..."+id);
return "{'b':'delete'}";
}
//@RequestMapping(method = RequestMethod.PUT)
@PutMapping
public String update(@RequestBody book book){
System.out.println("springMVC update..."+book);
return "{'c':'update'}";
}
//@RequestMapping(value = "{id}",method = RequestMethod.GET)
@GetMapping("/{id}")
public String getById(@PathVariable Integer id){
System.out.println("springMVC getById..."+id);
return "{'d':'getById'}";
}
//@RequestMapping(method = RequestMethod.GET)
@GetMapping
public String getAll(){
System.out.println("springMVC getAll...");
return "{'e':'getAll'}";
}
}
@RestController 注解是@Controller和@ResponseBody的合并。
@PostMapping此注解完全可以代替原来的注解@RequestMapping(method = RequestMethod.POST)
@DeleteMapping("/{id}")代替 @RequestMapping(value = "/{id}",method = RequestMethod.DELETE)
@PutMapping代替@RequestMapping(method = RequestMethod.PUT)
@GetMapping代替@RequestMapping(method = RequestMethod.GET)
@GetMapping("/{id}")代替@RequestMapping(value = "{id}",method = RequestMethod.GET)