SSM + RESTful + ajax-- Java Web development

  Some time ago to attend college at the time of a game with friends to get a simple web forum project, the use of technology has ssm, mysql, ajax, jquery, html and so on. Development before and after the end of the separation plan to the beginning of time, since no previous experience, so I fumbled write. The project is probably a good front-end written in html, does not contain data, the back-end interface to provide url, call interface when entering the page, then fill in the returned data to the front end of the html. Finally, at the time of acceptance of the project have asked have no use RESTful, though heard, but did not carefully understand, so simple to understand a bit on the Internet, and recorded.


 

What is RESTful:

  RESTful design and development is a way to network applications. After the separation idea started to grow back end of the front, the front static pages need to call the API to get the specified data, and how to design an easy to understand, use API became a problem, and RESTful API is used to regulate a constraint of .

  RESTful, the resource by URL location, defined by the HTTP methods (POST, GET, etc.) to complete what function.


  In a RESTful style, using the same URL, agreed different HTTP methods (POST, GET, etc.) to implement different business.

 

 

  generally   

CRUD operations HTTP method
Create POST
Read GET
Update PUT/PATCH
deletes DELETE

  Previously, the data might look like this :( increase in the controller class is declared in the outer RequestMapping, the actual access URL should be post / addPost)

1 /**
2      * 发帖
3      * @param post
4      * @return
5      */
6     @RequestMapping(value = "/addPost",produces = "application/json; charset=utf-8")
7     public @ResponseBody String addPost(HttpSession session,@RequestBody Post post){
8         //具体操作省略
9     }

Front-end using ajax, access / addPost URL conducted to increase the data.

  After Spring4.3, in order to support RESTful style, adds @ PutMapping, @ GetMapping, @ DeleteMapping, @ PostMapping these notes, and can directly attribute @RequestMapping binding method, the first increase in data can be:

. 1   @PostMapping (value = "/ {ID}" )
 2      public @ResponseBody String addPost (Long @PathVariable ID) {
 . 3           // processing data    
4      }    

  Such functionality can be achieved only through increased data access POST post.


 

RESTful specification

  1, the parameter naming

    Parameters hump nomenclature or underline name.

  2, url naming convention

    In a RESTful architecture, each represents a resource url, each represents a resource url url so can not have a verb, a noun only, and should also use the plural noun. Implemented should use appropriate verbs Http GET, POST, PUT, PATCH, DELETE, HEAD these resources can be operated

The non-standard url, redundancy does not make sense, form is not fixed, different developers also need to understand the document before calling.

https://example.com/api/getallUsers GET 获取所有用户 
https://example.com/api/getuser/1 GET 获取标识为1用户信息 
https://example.com/api/user/delete/1 GET/POST 删除标识为1用户信息 
https://example.com/api/updateUser/1 POST 更新标识为1用户信息 
https://example.com/api/User/add POST 添加新的用户 

After the specification for RESTful url, fixed form, readable, according to http verbs and nouns users can operate these resources

https://example.com/api/users GET 获取所有用户信息 
https://example.com/api/users/1 GET 获取标识为1用户信息 
https://example.com/api/users/1 DELETE 删除标识为1用户信息 
https://example.com/api/users/1 Patch 更新标识为1用户部分信息,包含在body中 
https://example.com/api/users POST 添加新的用户

  3, unified data format to return

     For legitimate request should return a unified data format, such as a json returned data should include:

       code - contains an integer type of the HTTP response status code

       status - contains the text: "success", "fail" or "error". HTTP status code in the response between 500-599 as "fail", is between 400-499 "error", others are "success" (e.g.: in response to a status code 1XX, 2XX, and 3XX). This fact is based on the actual situation can not happen in the.

       message-- state is "fail" and "error" effective when used to display error messages. Referring to international (il8n) standard, which may contain information number or code that may contain only one, or both comprising and separated by separator.

       data-- body containing the response. When the state is "fail" or "error", data comprising only cause of the error or exception name, or null is also possible.

Returns successful response json format

1 {
2   "code": 200,
3   "message": "success",
4   "data": {
5     "userName": "123456",
6     "age": 16,
7     "address": "beijing"
8   }
9 }

Returns a failure response format json

1 {
2   "code": 401,
3   "message": "error  message",
4   "data": null
5 }

  4, http status codes

  • 1 ** request did not succeed
  • 2 ** request succeeds, the processing of the status code indicates a successful request.
  • 3 ** request is redirected, to fulfill the request indicates the need for further operations. Typically, these status codes for redirection.
  • 4 ** request error status code indicates the request may be wrong, preventing the processing server.
  • 5 ** (Server Error) These status codes indicate internal server error occurred while trying to process the request. These errors may be wrong server itself, not with the request.

  This article documents a very detailed description of the design specifications

     https://github.com/Highflyer/REST_API_DESIGN_GUIDE


 

  This article just after the Internet to understand their own written personal understanding, if there are errors bigwigs want to be able to point out

Guess you like

Origin www.cnblogs.com/ELAIRS/p/12152444.html