restful风格概念

本博博客为转载。 原博客:https://blog.youkuaiyun.com/u013086062/article/details/73716617
--------------------
-

         REST是英文representational state transfer(表象性状态转变)或者表述性状态转移;Rest是web服务的一种架构风格;使用HTTP,URI,XML,JSON,HTML等广泛流行的标准和协议;轻量级,跨平台,跨语言的架构设计;它是一种设计风格,不是一种标准,是一种思想

Rest架构的主要原则

     网络上的所有事物都被抽象为资源

    每个资源都有一个唯一的资源标识符

    同一个资源具有多种表现形式(xml,json等)

    对资源的各种操作不会改变资源标识符

    所有的操作都是无状态的

    符合REST原则的架构方式即可称为RESTful

什么是Restful:

        对应的中文是rest式的;Restful web service是一种常见的rest的应用,是遵守了rest风格的web服务;rest式的web服务是一种ROA(The Resource-Oriented Architecture)(面向资源的架构).

为什么会出现Restful


在Restful之前的操作:
http://127.0.0.1/user/query/1 GET  根据用户id查询用户数据
http://127.0.0.1/user/save POST 新增用户
http://127.0.0.1/user/update POST 修改用户信息
http://127.0.0.1/user/delete GET/POST 删除用户信息

RESTful用法:
http://127.0.0.1/user/1 GET  根据用户id查询用户数据
http://127.0.0.1/user  POST 新增用户
http://127.0.0.1/user  PUT 修改用户信息
http://127.0.0.1/user  DELETE 删除用户信息

之前的操作是没有问题的,大神认为是有问题的,有什么问题呢?你每次请求的接口或者地址,都在做描述,例如查询的时候用了query,新增的时候用了save,其实完全没有这个必要,我使用了get请求,就是查询.使用post请求,就是新增的请求,我的意图很明显,完全没有必要做描述,这就是为什么有了restful.

如何使用:



SpringMVC实现restful服务:

SpringMVC原生态的支持了REST风格的架构设计

所涉及到的注解:

--@RequestMapping

---@PathVariable

---@ResponseBody

  1. package cn.itcast.mybatis.controller;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.http.HttpStatus;
  4. import org.springframework.http.ResponseEntity;
  5. import org.springframework.stereotype.Controller;
  6. import org.springframework.web.bind.annotation.PathVariable;
  7. import org.springframework.web.bind.annotation.RequestMapping;
  8. import org.springframework.web.bind.annotation.RequestMethod;
  9. import org.springframework.web.bind.annotation.RequestParam;
  10. import org.springframework.web.bind.annotation.ResponseBody;
  11. import cn.itcast.mybatis.pojo.User;
  12. import cn.itcast.mybatis.service.NewUserService;
  13. @RequestMapping("restful/user")
  14. @Controller
  15. public class RestUserController {
  16. @Autowired
  17. private NewUserService newUserService;
  18. /**
  19. * 根据用户id查询用户数据
  20. *
  21. * @param id
  22. * @return
  23. */
  24. @RequestMapping(value = "{id}", method = RequestMethod.GET)
  25. @ResponseBody
  26. public ResponseEntity<User> queryUserById(@PathVariable("id") Long id) {
  27. try {
  28. User user = this.newUserService.queryUserById(id);
  29. if (null == user) {
  30. // 资源不存在,响应404
  31. return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
  32. }
  33. // 200
  34. // return ResponseEntity.status(HttpStatus.OK).body(user);
  35. return ResponseEntity.ok(user);
  36. } catch (Exception e) {
  37. e.printStackTrace();
  38. }
  39. // 500
  40. return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
  41. }
  42. /**
  43. * 新增用户
  44. *
  45. * @param user
  46. * @return
  47. */
  48. @RequestMapping(method = RequestMethod.POST)
  49. public ResponseEntity<Void> saveUser(User user) {
  50. try {
  51. this.newUserService.saveUser(user);
  52. return ResponseEntity.status(HttpStatus.CREATED).build();
  53. } catch (Exception e) {
  54. // TODO Auto-generated catch block
  55. e.printStackTrace();
  56. }
  57. // 500
  58. return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
  59. }
  60. /**
  61. * 更新用户资源
  62. *
  63. * @param user
  64. * @return
  65. */
  66. @RequestMapping(method = RequestMethod.PUT)
  67. public ResponseEntity<Void> updateUser(User user) {
  68. try {
  69. this.newUserService.updateUser(user);
  70. return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
  71. } catch (Exception e) {
  72. e.printStackTrace();
  73. }
  74. // 500
  75. return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
  76. }
  77. /**
  78. * 删除用户资源
  79. *
  80. * @param user
  81. * @return
  82. */
  83. @RequestMapping(method = RequestMethod.DELETE)
  84. public ResponseEntity<Void> deleteUser(@RequestParam(value = "id", defaultValue = "0") Long id) {
  85. try {
  86. if (id.intValue() == 0) {
  87. // 请求参数有误
  88. return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
  89. }
  90. this.newUserService.deleteUserById(id);
  91. // 204
  92. return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
  93. } catch (Exception e) {
  94. e.printStackTrace();
  95. }
  96. // 500
  97. return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
  98. }
  99. }



HTTP相应状态码:



基于模拟退火的计算器 在线运行 访问run.bcjh.xyz。 先展示下效果 https://pan.quark.cn/s/cc95c98c3760 参见此仓库。 使用方法(本地安装包) 前往Releases · hjenryin/BCJH-Metropolis下载最新 ,解压后输入游戏内校验码即可使用。 配置厨具 已在2.0.0弃用。 直接使用白菜菊花代码,保留高级厨具,新手池厨具可变。 更改迭代次数 如有需要,可以更改 中39行的数字来设置迭代次数。 本地编译 如果在windows平台,需要使用MSBuild编译,并将 改为ANSI编码。 如有条件,强烈建议这种本地运行(运行可加速、可多次重复)。 在 下运行 ,是游戏中的白菜菊花校验码。 编译、运行: - 在根目录新建 文件夹并 至build - - 使用 (linux) 或 (windows) 运行。 最后在命令行就可以得到输出结果了! (注意顺序)(得到厨师-技法,表示对应新手池厨具) 注:linux下不支持多任务选择 云端编译已在2.0.0弃用。 局限性 已知的问题: - 无法得到最优解! 只能得到一个比较好的解,有助于开阔思路。 - 无法选择菜品数量(默认拉满)。 可能有一定门槛。 (这可能有助于防止这类辅助工具的滥用导致分数膨胀? )(你问我为什么不用其他语言写? python一个晚上就写好了,结果因为有涉及json读写很多类型没法推断,jit用不了,算这个太慢了,所以就用c++写了) 工作原理 采用两层模拟退火来最大化总能量。 第一层为三个厨师,其能量用第二层模拟退火来估计。 也就是说,这套方法理论上也能算厨神(只要能够在非常快的时间内,算出一个厨神面板的得分),但是加上厨神的食材限制工作量有点大……以后再说吧。 (...
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值