RESTful代码规则

REST基础知识,补充一点,普通HTTPURL是面向服务的,而REST是面向资源的。

为什么说REST面向资源呢?

REST强调描述应用程序的事物和名词,这都是为什么有的资料或者博客写的都是起名规则是名词(例如:name),而不是动词(例如:queryName)

以springBoot为例,

springBoot和Spirng的代码区别在于,@RestController注解,都是使用@PathVariable获取参数值

测试请求格式:

Get请求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;
    }
}

猜你喜欢

转载自blog.csdn.net/qq_32923295/article/details/115057188