码迷,mamicode.com
首页 > 编程语言 > 详细

spring-mvc使用restful开发风格

时间:2021-06-08 23:47:41      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:定义   path   list()   ges   网络资源   ons   str   tco   success   

1.0 Rest

2.0 Rest行为约定方式

注意:上述行为是约定方式,约定不是规范,可以打破,所以称Rest风格,而不是Rest规范

3.0 前台发送请求

//<a href="javascript:void(0);" @click="list">GET请求发送给请求查询user列表</a>
list(){
    axios.get("user/1/5").then(resp=>{
        console.log(resp.data)
    });
}
//<a href="javascript:void(0);" @click="findById">GET请求根据id查询user信息</a>
findById(){
    axios.get("user/10").then(resp=>{
        console.log(resp.data);
    });
}

//<a href="javascript:void(0);" @click="addUser">POST请求添加用户信息</a>
addUser(){
    axios.post("user",this.user).then(resp=>{
        alert(resp.data);
    });
}
//<a href="javascript:void(0);" @click="updateUser">PUT请求修改用户信息</a>
updateUser(){
    axios.put("user",this.user).then(resp=>{
        alert(resp.data);
    });
}
//<a href="javascript:void(0);" @click="deleteUser">DELETE请求删除用户信息</a>
deleteUser(){
    axios.delete("user/10").then(resp=>{
        alert(resp.data);
    });
}

4.0 后台接收请求

/*@Controller
@ResponseBody*/
@RestController //等价于@Controller+@ResponseBody
@RequestMapping("/user")
public class UserController {

  //分页查询用户信息的方法
  //@RequestMapping(value = "/{currentPage}/{pageSize}",method = RequestMethod.GET)
  @GetMapping("/{currentPage}/{pageSize}")
  public PageInfo<User> list(@PathVariable Integer currentPage,
                             @PathVariable Integer pageSize){
    System.out.println("currentPage--->"+currentPage);
    System.out.println("pageSize--->"+pageSize);
    //此处省略500行代码
    return pageInfo;
  }
  //根据id查询User信息的方法
  //@RequestMapping(value = "/{id}",method = RequestMethod.GET)
  @GetMapping("/{id}")
  public User findById(@PathVariable Integer id){
    System.out.println("findById--->"+id);
    //此处省略500行代码
    return user;
  }
  //添加用户
  @PostMapping
  //@RequestMapping(method = RequestMethod.POST)
  public String addUser(@RequestBody User user){
    System.out.println("addUser--->"+user);
     //此处省略500行代码
    return "success";
  }

  //修改用户
  //@RequestMapping(method = RequestMethod.PUT)
  @PutMapping
  public String updateUser(@RequestBody User user){
    System.out.println("updateUser--->"+user);
    //此处省略500行代码
    return "success";
  }
  //删除
  //@RequestMapping(value = "/{id}",method = RequestMethod.DELETE)
  @DeleteMapping("/{id}")
  public String deleteUser(@PathVariable Integer id){
    System.out.println("deleteUser--->"+id);
    //此处省略500行代码
    return "success";
  }
}

spring-mvc使用restful开发风格

标签:定义   path   list()   ges   网络资源   ons   str   tco   success   

原文地址:https://www.cnblogs.com/zijing314/p/14864418.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!