【java、springMVC】REST风格

描述访问网络资源的格式

传统风格:http://localhost/user/saveUser

rest风格:http://localhost/user

优点

1.隐藏资源访问行为(用行为动作区分操作)

2.书写简化

入门案例(最基础,有不合理)

java 复制代码
@RequestMapping(value = "/users", method = RequestMethod.POST)
    @ResponseBody
    public String save(@RequestBody User user) {
        System.out.println("user save " + user);
        return "{'module':'user save'}";
    }

    @RequestMapping(value = "/users/{id}", method = RequestMethod.DELETE)
    @ResponseBody
    public String delete(@PathVariable Integer id) {
        System.out.println("user delete " + id);
        return "{'module':'user delete'}";
    }

    @RequestMapping(value = "/users", method = RequestMethod.PUT)
    @ResponseBody
    public String update(@RequestBody User user) {
        System.out.println("user update " + user);
        return "{'module':'user update'}";
    }

    @RequestMapping(value = "/users/{id}",method = RequestMethod.GET)
    @ResponseBody
    public String getById(@PathVariable Integer id) {
        System.out.println("user getById " + id);
        return "{'module':'user getById'}";
    }

    @RequestMapping(value = "/users",method = RequestMethod.GET)
    @ResponseBody
    public String getAll() {
        System.out.println("user getAll ");
        return "{'module':'user getAll'}";
    }

快速开发

java 复制代码
package org.example.controller;/*
 * @Auther:huangzhiyang
 * @Date:2023/10/7
 * @Description:
 */

import org.example.domain.Book;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/book")
public class BookController {
   @PostMapping
    public String save(@RequestBody Book book) {
        System.out.println("book save "+book);
        return "{'info':'springmvc'}";
    }
    @DeleteMapping("/{id}")
    public String delete( @PathVariable String id) {
        System.out.println("book delete"+id);
        return "{'info':'springmvc'}";
    }
   @PutMapping
    public String update(@RequestBody Book book) {
        System.out.println("book update");
        return "{'info':'springmvc'}";
    }
}
相关推荐
韩立学长9 分钟前
【开题答辩实录分享】以《基于Python的大学超市仓储信息管理系统的设计与实现》为例进行选题答辩实录分享
开发语言·python
风筝在晴天搁浅12 分钟前
hot100 78.子集
java·算法
froginwe1133 分钟前
Scala 循环
开发语言
m0_706653231 小时前
C++编译期数组操作
开发语言·c++·算法
故事和你911 小时前
sdut-Java面向对象-06 继承和多态、抽象类和接口(函数题:10-18题)
java·开发语言·算法·面向对象·基础语法·继承和多态·抽象类和接口
Bruk.Liu1 小时前
(LangChain实战2):LangChain消息(message)的使用
开发语言·langchain
qq_423233901 小时前
C++与Python混合编程实战
开发语言·c++·算法
m0_715575341 小时前
分布式任务调度系统
开发语言·c++·算法
Configure-Handler2 小时前
buildroot System configuration
java·服务器·数据库
csbysj20202 小时前
选择(Selectable)
开发语言