Spring Cloud Spring Boot mybatis distributed micro-service Cloud Architecture (v)

Source: Internet
Author: User

First, review and elaborate on the @controller, @RestController, @RequestMapping annotations used in the QuickStart. If you are unfamiliar with spring MVC and have not yet tried a QuickStart case, it is recommended that you take a look at QuickStart content first.

@Controller: Modifier class, used to create an object that handles HTTP requests

@RestController: Note added after Spring4, the original return JSON in @controller need @ResponseBody to match, If you replace @controller directly with @restcontroller, you do not need to configure @responsebody, which returns the JSON format by default.

@RequestMapping: Configuring URL Mappings
Below we try to use spring MVC to implement a set of restful APIs for user object operations, with comments detailing how HTTP requests are mapped in spring MVC, how to pass the parameters, and how to write unit tests.

The RESTful API is specifically designed as follows:

User Entity definition:

public class User {       private Long id;       private String name;       private Integer age;       // 省略setter和getter   }  

Implementing an interface for manipulating user objects

@RestController @RequestMapping (value= "/users")//by configuring this to make the following mappings under/users public class Usercontroller {//Create       Thread-Safe Map static Map<long, user> users = Collections.synchronizedmap (new Hashmap<long, user> ()); @RequestMapping (value= "/", Method=requestmethod.get) public list<user> getuserlist () {//processing "/users/ "Get request to get a list of users//can also pass parameters from the page by @requestparam the query condition or the transfer of page information list<user> r = new Arraylist<u           Ser> (Users.values ());       return R;           } @RequestMapping (value= "/", method=requestmethod.post) public String postuser (@ModelAttribute user user) { A POST request that handles "/users/" is used to create a user///In addition to the @modelattribute binding parameter, you can pass parameters from the page @requestparam users.put           (User.getid (), user);       Return "Success";           } @RequestMapping (value= "/{id}", method=requestmethod.get) public User getUser (@PathVariable Long ID) { Get request to process "/users/{id}" to get the ID value in the URLUser information//the ID in the URL can be bound to the function's parameters by @pathvariable return Users.get (ID); } @RequestMapping (value= "/{id}", method=requestmethod.put) public String putuser (@PathVariable Long ID, @Model           Attribute User user) {//Processing "/users/{id}" put request to update user information user U = users.get (id);           U.setname (User.getname ());           U.setage (User.getage ());           Users.put (ID, u);       Return "Success"; } @RequestMapping (value= "/{id}", method=requestmethod.delete) public String deleteuser (@PathVariable Long ID)           {//processing "/users/{id}" delete request, used to delete user users.remove (ID);       Return "Success";   }   }

Spring Cloud Spring Boot mybatis distributed micro-service Cloud Architecture (v)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.