Restful style coding

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util. *;

/**
 * Restful style coding, in simple terms, use a url to handle related operations on the same resource, such as adding, deleting, modifying, and checking.
 */

@RestController
@RequestMapping("/restful/user")
public class RestfulUserController {
    //Store the user in the map to simulate the database
    private static Map<Long, User> users = Collections.synchronizedMap(new HashMap<Long, User>());

    /**
     * return all users
     */
    @RequestMapping(value = "", method = RequestMethod.GET)
    public List<User> getUserList() {
        //Process restful/user's GET request to get the user list
        //You can also pass parameters from the page through @RequestParam for conditional query or transmission of anti-wild information
        List<User> userList = new ArrayList<User>(users.values());
        printMap();
        return userList;
    }

    /**
     * Return the user with a specific ID
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public User getUser(@PathVariable Long id) {
        // Process the GET request of "/users/{id}" to get the User information of the id value in the url
        // The id in the url can be bound to the parameter of the function through @PathVariable
        printMap();
        return users.get(id);
    }

    /**
     * Create new user
     * @param user
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.POST)
    public String postUser(User user) {
        // Process the POST request of "/users/" to create a User
        //@ModelAttribute User user
        // In addition to @ModelAttribute binding parameters, parameters can also be passed from the page through @RequestParam
        users.put(user.getId(), user);
        printMap();
        return "success";
    }

    /**
     * use put to update user
     * @param id
     * @param user
     * @return
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
    public String putUser(@PathVariable Long id, User user) {
        //Process the put request of "/restful/user" to get the User information of the id in the url
        User u = users.get(id);
        u.setName(user.getName());
        users.put(id, u);
        printMap();
        return "success";
    }

    @RequestMapping(value = "/{id}",method = RequestMethod.DELETE)
    public String deleteUser(@PathVariable Long id){
        // Process the DELETE request of "/users/{id}" to delete User
        // The id in the url can be bound to the parameter of the function through @PathVariable
        users.remove(id);
        printMap();
        return"success";
    }
    private void printMap(){
        for (Map.Entry<Long,User> entry:users.entrySet()){
            System.out.println(entry.getValue().toString());
        }
    }
}

In conclusion:

Restful style coding, in simple terms, uses different access methods of the same url to handle different operations on the same resource, such as additions, deletions, changes, and so on.

Simple rules:

Get->select

post->insert   

put->update  

delete->delete

E.g

/user/query->/user (GET方式)

/user/add->/user (POST method)

/user/update->/user (PUT方式)

/user/delete->/user (DELETE方式)

tip: Simple difference between @RequestParam and @PathVariable

The role of @pathVariable is to map the template variables in the request URL to the parameters of the function processing method, that is, take the variables in the uri template as parameters

For example visit the above

/**
 * Return the user with a specific ID
 */
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
public User getUser(@PathVariable Long id) {
    // Process the GET request of "/users/{id}" to get the User information of the id value in the url
    // The id in the url can be bound to the parameter of the function through @PathVariable
    printMap();
    return users.get(id);
}

When calling a method, if this annotation is not added, when the method is accessed using /restful/user/2, the method query result will not be found.

Because there is no such annotation, the actual access is /restful/user/null, so the query result cannot be obtained.

The role of the @RequestParam annotation is to extract parameters.

This annotation has three parameters:

value: parameter name, that is, the name of the input request parameter. For example, username indicates that the value of the parameter named username in the parameter area of ​​the request will be passed in;

required: Whether it is required, the default is true, indicating that there must be corresponding parameters in the request, otherwise a 404 error code will be reported;

defaultValue: The default value, indicating the default value if there is no parameter with the same name in the request, for example:

public List<EasyUITreeNode> getItemTreeNode(@RequestParam(value="id",defaultValue="0") long parentId)

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324377925&siteId=291194637
Recommended