当前位置:网站首页>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]所创,转载请带上原文链接,感谢
边栏推荐
- Electron application uses electronic builder and electronic updater to realize automatic update
- It is really necessary to build a distributed ID generation service
- 快速排序为什么这么快?
- [C / C + + 1] clion configuration and running C language
- Solve the problem of database insert data garbled in PL / SQL developer
- Elasticsearch数据库 | Elasticsearch-7.5.0应用搭建实战
- Read the advantages of Wi Fi 6 over Wi Fi 5 in 3 minutes
- 消息队列(MessageQueue)-分析
- Free patent download tutorial (HowNet, Espacenet)
- JNI-Thread中start方法的呼叫與run方法的回撥分析
猜你喜欢
C#和C/C++混合编程系列5-内存管理之GC协同
一篇文章教会你使用Python网络爬虫下载酷狗音乐
一篇文章带你了解CSS3图片边框
Music generation through deep neural network
快速排序为什么这么快?
一篇文章教会你使用HTML5 SVG 标签
Word segmentation, naming subject recognition, part of speech and grammatical analysis in natural language processing
The difference between gbdt and XGB, and the mathematical derivation of gradient descent method and Newton method
Introduction to the structure of PDF417 bar code system
Brief introduction of TF flags
随机推荐
The importance of big data application is reflected in all aspects
理解格式化原理
Jmeter——ForEach Controller&Loop Controller
StickEngine-架构11-消息队列(MessageQueue)
What are manufacturing and new automation technologies?
C語言I部落格作業03
Uncle Bob: the software architecture is similar to a house. Object oriented is the structure of the house, and the water pipe is functional programming
【转发】查看lua中userdata的方法
【自学unity2d传奇游戏开发】地图编辑器
Discussion on the technical scheme of text de duplication (1)
Individual annual work summary and 2019 work plan (Internet)
What are Devops
有了这个神器,快速告别垃圾短信邮件
DC-1靶機
一部完整的游戏,需要制作哪些音乐?
前端未來趨勢之原生API:Web Components
用一个例子理解JS函数的底层处理机制
Recommendation system based on deep learning
Construction of encoder decoder model with keras LSTM
前端工程师需要懂的前端面试题(c s s方面)总结(二)