List of topics: juejin, github, smartblue, cyanosis, channing-cyan, fancy, hydrogen, condensed-night-purple, greenwillow, v-green, vue-pro, healer-readable, mk-cute, jzman, geek-black

Contribution Subject:Github.com/xitu/juejin…

36/100 weixin_53403536 new

Do you know what Restful style is? SpringMVC takes us there!

[Img-qzPFY4S9-1608534539661)(imgkr.cn-bj.ufileos.com/50416f44-5b…)]

A Restful API is a software architecture style, a design style rather than a standard, that provides a set of design principles and constraints. It is mainly used for client and server interaction class software. Software designed in this style can be simpler, more hierarchical, and easier to implement mechanisms such as caching.

In Restful style, users request the same URL in the request mode: GET, POST, delete, PUT… In this way, the front-end developers will not be confused about the resource address of the request and a lot of trouble to check the method name, so as to form a unified interface.

SpringMVC Restful style URL configuration implementation

SpringMVC’s restURL is provided by @requestMapping and @PathVariable Annotation, RequestMapping(value=”/blog /{id}”,method= requestmethod.delete)

* GET (SELECT) : queries from the server, which can be distinguished by the parameters in the server. * POST (CREATE) : Creates a new resource on the server side and calls the INSERT operation. * PUT (UPDATE) : Updates resources on the server side, invoking the UPDATE operation. * PATCH (UPDATE) : Updates resources on the server side (clients provide changed properties). (Currently not implemented in JDK7, not supported in Tomcat7). * DELETE (DELETE) : Deletes the resource from the server, invoking the DELETE statement.

A case in field

Get Request Configuration

/** *restful- >get Requests to perform query operations *@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("Invalid parameter!");
        return messageModel;
    }
    messageModel.setResult(accountService.queryById(id));
    return messageModel; 
}
Copy the code

Post Request Configuration

/** * restful--> Post requests to perform the add operation *@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("Operation failed!");
    }
    return messageModel; 
}
Copy the code

Put Request Configuration

/** * restful-->put Requests to perform update operations *@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("Operation failed!");
    }
    return messageModel; 
}
Copy the code

Delete request Configuration

 /** * restful-->delete Requests to perform the deletion operation *@param id
 * @return* /
@RequestMapping(value="deleteAccountById/{id}",method=RequestMethod.DELETE,produces=MediaType.APPLICATION_JSON_UTF8_VALU E)
@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("Operation failed!");
    }
    return messageModel;
}
Copy the code

Extension to REST

[External link image transfer failed, the source station may have anti-theft mechanism, it is recommended to save the image directly upload (IMG-ihvuuj8O-1608534539667)(imgkr.cn-bj.ufileos.com/21731772-bf…)]

REST (Representational State Transfer) describes an architectural style of network system, such as a Web application. It first appeared in 2000 in the doctoral dissertation of Roy Fielding, one of the main authors of the HTTP specification. Among the three mainstream Web service interaction schemes, REST is simpler and clearer than SOAP (Simple Object Access Protocol) and XML-RPC. Whether it is the processing of URL or the encoding of Payload, REST tends to be designed and implemented in a simpler, lightweight way. It is important to note that REST does not have a clear standard, but rather a style of design.

REST tends to be designed and implemented in simpler and lighter ways, whether it’s handling urls or encoding payloads. It is important to note that REST does not have a clear standard, but rather a style of design.

REST refers to a set of architectural constraints and principles. An application or design that satisfies these constraints and principles is RESTful.