RESTful code rules

REST basic knowledge , add that ordinary HTTP URL is service-oriented, while REST is resource-oriented.

Why is REST resource-oriented?

REST emphasizes the things and nouns that describe the application. This is why some materials or blogs are written with names. The rule is nouns (for example: name), not verbs (for example: queryName)

Take springBoot as an example,

The code difference between springBoot and Spirng is that the @RestController annotation uses @PathVariable to obtain parameter values

Test request format:

Get request localhost:6060/restFulCon/1

package com.lyc.springboot.controller;

import org.springframework.web.bind.annotation.*;

import com.lyc.springboot.pojo.User;

@RestController
@RequestMapping("restFulCon")
public class RestFulDemoController {

    /**
     * post请求
     */
    @PostMapping(value = "/")
    public User addArticle(User user){
        return user;
    }


    /**
     * get请求
     * @param id
     * @return
     */
    @GetMapping(value = "/{id}")
    public String getArticle(@PathVariable("id") String id){
        return  id;
    }

    /**
     * /delete请求
     * @param id
     * @return
     */
    @DeleteMapping(value = "/{id}")
    public String delArticle(@PathVariable("id") String id){
        return  id;
    }


    /**
     * 局部更新用patch,全部更新用put
     * @param user
     * @return
     */
    @PatchMapping(value = "/{id}")
    public User patchArticle(User user){
        return  user;
    }

    @PutMapping(value = "/")
    public User putArticle(User user){
        System.out.println("-----------");
        return  user;
    }
}

 

Guess you like

Origin blog.csdn.net/qq_32923295/article/details/115057188