当前位置:网站首页>Application of restful API based on MVC
Application of restful API based on MVC
2020-11-06 20:10:00 【itread01】
`PUT/users{id}`[^ Create a resource for the client to maintain the primary key information ] | | Delete | `DELETE/users/{id}` | | modify / to update | `PUT/users/{id}` | | Look up all | `GET/users` | | Primary key query | `GET/users/{id}`
`GET/users?id=26` | | Paging scope query | `GET/users?start=0&size=10`
`GET/users?07,2019-07,2020` | You can see through this `RESTAPI` It's all through the right to == The same resource == The operation of , The difference is through different ==HTTP Method == To achieve different processing of resources . #### 2.`MVC` Yes `REST` Support for ##### 1.1 Mainly through annotations * `@Controller` Name a controller that handles requests * `@RequestMapping` Request mapping address , It has several sub annotations for implementation `REST` In terms of style, it is more == Semantic == * `@GETMapping` ==GET Ask for == * `@PUTMapping` ==PUT Ask for == * `@POSTMapping` ==POST Ask for == * `@DELETEMapping` ==DELETE Ask for == * `@ResponseBody` Convert the response content to `JSON` Format * `@RequestBody` The request content is converted to `JSON` Format * `@PathVariable("id")` Used to bind an argument * `@RESTController` Equal to `@Controller`+`@ResponseBody` This annotation is written on the class , All methods that identify this class are only == Return data ==, Instead of == View jump == ##### 1.2 return `HTTP` Status code **`REST` Style `API` One of the most distinctive features is by returning the corresponding `HTTPStatus` To determine whether the operation of the client is completed ** == Here is spring About `Http` The enumeration class described by the state code , This paper lists the common status codes ==( If readers are interested in this, they can check `HttpStatus` Source code ) ~~~java public enum HttpStatus{ OK(200, "OK"),// Used for server with physical response CREATED(201, "Created"),// A new entity has been established , Respond to the entity NO_CONTENT(204, "No Content"),// The server is responding normally , But no physical response BAD_REQUEST(400, "Bad Request"),// Client request syntax error NOT_FOUND(404, "Not Found"),// The target resource does not exist INTERNAL_SERVER_ERROR(500, "Internal Server Error"),// Server internal error NOT_IMPLEMENTED(501, "Not Implemented"),// The server does not support the current request } ~~~ Spring The status code is returned by `@ResponseStatus` Note or `ResponseEntity ` Class . ==`@ResponseStatus` The way == ~~~java @GetMapping(path = "/user/{id}" , produces = "application/json;charset=utf-8") @ResponseStatus(HttpStatus.OK) public User findUserById(@PathVariable("id")Integer id){ User user = userService.findUserById(id); return user ; } ~~~ ==`ResponseEntity `== The way ~~~java @GetMapping(produces = "application/json;charset=utf-8") public ResponseEntity
-
> findAll(){ List
-
>(users , HttpStatus.OK); } ~~~ ##### 1.3 Because of `MVC` Default not supported `PUT` and `DELETE` Method , So you need to turn it on manually * stay `tomcat` Server's `web.xml` Open the configuration in the file * ~~~xml
-
> findAll(){ List
-
>(users , HttpStatus.OK); } /**、 * According to ID Inquire about * @param id * @return */ @GetMapping(path = "/{id}" , produces = "application/json;charset=utf-8") @ResponseStatus(HttpStatus.OK) public User findUserById(@PathVariable("id")Integer id){ User user = userService.findUserById(id); return user ; } /** * Add a user * Return the user */ @PostMapping(produces = "application/json;charset=utf-8") @ResponseStatus(HttpStatus.CREATED) public User addUser(@RequestBody User user){ User newUser = userService.addUser(user); return newUser ; } /** * to update * @param user */ @PutMapping(path = "/{id}" ,produces = "application/json;charset=utf-8") public ResponseEntity
版权声明
本文为[itread01]所创,转载请带上原文链接,感谢
边栏推荐
- How to get started with new HTML5 (2)
- Advanced Vue component pattern (3)
- Natural language processing - wrong word recognition (based on Python) kenlm, pycorrector
- Brief introduction and advantages and disadvantages of deepwalk model
- ES6学习笔记(二):教你玩转类的继承和类的对象
- Cglib 如何实现多重代理?
- Shh! Is this really good for asynchronous events?
- How to hide part of barcode text in barcode generation software
- 用一个例子理解JS函数的底层处理机制
- A course on word embedding
猜你喜欢

代码生成器插件与Creator预制体文件解析

Wow, elasticsearch multi field weight sorting can play like this

Python基础变量类型——List浅析

Mongodb (from 0 to 1), 11 days mongodb primary to intermediate advanced secret

给字节的学姐讲如何准备“系统设计面试”

The AI method put forward by China has more and more influence. Tianda et al. Mined the development law of AI from a large number of literatures

理解格式化原理

仅用六种字符来完成Hello World,你能做到吗?

StickEngine-架构12-通信协议

游戏开发中的新手引导与事件管理系统
随机推荐
Three Python tips for reading, creating and running multiple files
C#和C/C++混合编程系列5-内存管理之GC协同
nacos、ribbon和feign的簡明教程
Python基础变量类型——List浅析
ES6学习笔记(四):教你轻松搞懂ES6的新增语法
How to hide part of barcode text in barcode generation software
ES6学习笔记(二):教你玩转类的继承和类的对象
Analysis of partial source codes of qthread
html+vue.js 實現分頁可相容IE
視覺滾動[反差美]
What to do if you are squeezed by old programmers? I don't want to quit
How to use Python 2.7 after installing anaconda3?
华为Mate 40 系列搭载HMS有什么亮点?
Building and visualizing decision tree with Python
Solve the problem of database insert data garbled in PL / SQL developer
It's easy to operate. ThreadLocal can also be used as a cache
Even liver three all night, jvm77 high frequency interview questions detailed analysis, this?
開源一套極簡的前後端分離專案腳手架
Simple summary of front end modularization
Python saves the list data