Do you know what Restful style is? Let SpringMVC take us to achieve it!

Do you know what Restful style is? SpringMVC takes us to achieve it!

For those who need more related information, please see the profile on the homepage!

The Restful style API is a software architecture style. The design style is not a standard. It only provides a set of design principles and constraints . It is mainly used for software that interacts between client and server. Software designed based on this style can be more concise, more layered, and easier to implement mechanisms such as caching.

In the Restful style, the url requested by the user uses the same url and uses the request methods: get, post, delete, put, etc. to distinguish the processing methods of the request, so that front-end developers can be separated in the development of front and back There is no confusion about the requested resource address and a lot of trouble of checking method names, forming a unified interface.

SpringMVC Restful style URL configuration implementation method

SpringMVC's resturl is provided through @RequestMapping and @PathVariable annotation. For example, @RequestMapping(value="/blog /{id}",method=RequestMethod.DELETE) can handle the delete request of /blog/1.

  • GET (SELECT): Query from the server. The query method can be distinguished by the requested parameters on the server.
  • POST (CREATE): Create a new resource on the server side and call the insert operation.
  • PUT (UPDATE): Update resources on the server side and call the update operation.
  • PATCH (UPDATE): Update resources on the server side (the client side provides changed attributes). (Currently jdk7 is not implemented, tomcat7 does not support).
  • DELETE (DELETE): To delete a resource from the server, call the delete statement.

Case practice

Get request configuration

/**
*restful-->get 请求 执行查询操作
* @param id
* @return
*/
@RequestMapping(value="queryAccountById02/{id}",method=RequestMethod.GET,produces=MediaType.APPLICATION_JSON_UTF8_VALUE)
@ResponseBody
public MessageModel queryAccountById(@PathVariable Integer id){
    
    
    MessageModel messageModel=new MessageModel();
    if(null==id){
    
    
        messageModel.setCode(300);
        messageModel.setMsg("参数非法!");
        return messageModel;
    }
    messageModel.setResult(accountService.queryById(id));
    return messageModel; 
}

Post request configuration

/**
* restful-->post 请求执行添加操作
* @param id
* @param aname
* @return
*/
@RequestMapping(value="saveAccount",method=RequestMethod.POST,produces=MediaType.APPLICATION_JSON_UTF8_VALUE)
@ResponseBody
public MessageModel queryAccountById04(@RequestBody Account account){
    
    
    MessageModel messageModel=new MessageModel();
    try {
    
    
    	accountService.saveOrUpdateAccount(account);
    } catch (ParamsException e) {
    
     e.printStackTrace();
        messageModel.setCode(e.getErrorCode());
        messageModel.setMsg(e.getErrorMsg());
    }catch (Exception e) {
    
     e.printStackTrace();
        messageModel.setCode(300);
        messageModel.setMsg("操作失败!");
    }
    return messageModel; 
}

Put request configuration

/**
* restful-->put 请求执行更新操作
* @param id
* @param account
* @return
*/
@RequestMapping(value="update/{id}",method=RequestMethod.PUT,produces=MediaType.APPLICATION_JSON_UTF8_VALUE)
@ResponseBody
public MessageModel queryAccountById04(@PathVariable Integer id,@RequestBody Account account){
    
    
    MessageModel messageModel=new MessageModel();
    try {
    
    
    	accountService.saveOrUpdateAccount(account);
    } catch (ParamsException e) {
    
     e.printStackTrace();
   	 	messageModel.setCode(e.getErrorCode());
    	messageModel.setMsg(e.getErrorMsg());
    }catch (Exception e) {
    
     e.printStackTrace();
    	messageModel.setCode(300);
    	messageModel.setMsg("操作失败!");
    }
    return messageModel; 
}

Delete request configuration

 /**
 * restful-->delete 请求 执行删除操作
 * @param id
 * @return
 */
@RequestMapping(value="deleteAccountById/{id}",method=RequestMethod.DELETE,produces=MediaType.APPLICATION_JSON_UTF8_VALUE)
@ResponseBody
public MessageModel queryAccountById05(@PathVariable Integer id){
    
    
    MessageModel messageModel=new MessageModel();
    try {
    
    
    	accountService.deleteAccountById(id);
    } catch (ParamsException e) {
    
     e.printStackTrace();
        messageModel.setCode(e.getErrorCode());
        messageModel.setMsg(e.getErrorMsg());
    }catch (Exception e) {
    
     e.printStackTrace();
        messageModel.setCode(300);
        messageModel.setMsg("操作失败!");
    }
    return messageModel;
}

Extension~REST

[External link image transfer failed. The source site may have an anti-leech link mechanism. It is recommended to save the image and upload it directly (img-pkXNJSn2-1608000365651)(https://imgkr.cn-bj.ufileos.com/21731772-bfbc-48fd -a24b-22a09804602d.png)]

REST (English: Representational State Transfer , REST for short) describes an architectural style network system, such as a web application. It first appeared in Roy Fielding's doctoral thesis in 2000, Roy Fielding was one of the main authors of the HTTP specification. Among the three current mainstream web service interaction solutions, REST is simpler and clearer than SOAP (Simple Object Access Protocol) and XML-RPC . Whether it is URL processing or payload encoding, REST is Tend to use simpler and lighter methods to design and implement. It is worth noting that REST does not have a clear standard, but more like a design style.

REST refers to a set of architectural constraints and principles. An application or design that meets these constraints and principles is RESTful.
Insert picture description here

Guess you like

Origin blog.csdn.net/xyx12321/article/details/111193710