【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'}";
    }
}
相关推荐
骁的小小站几秒前
Learn C the Hardway学习笔记和拓展知识(一)
c语言·开发语言·c++·经验分享·笔记·学习·bash
代码充电宝18 分钟前
LeetCode 算法题【中等】189. 轮转数组
java·算法·leetcode·职场和发展·数组
我命由我1234521 分钟前
PDFBox - PDDocument 与 byte 数组、PDF 加密
java·服务器·前端·后端·学习·java-ee·pdf
花哥码天下25 分钟前
Oracle下载JDK无需登录
java·开发语言
摇滚侠40 分钟前
Spring Boot 3零基础教程,yml语法细节,笔记16
java·spring boot·笔记
早点.早点.40 分钟前
QT登陆界面
开发语言·qt
楼田莉子42 分钟前
C++学习:异常及其处理
开发语言·c++·学习·visual studio
fsnine1 小时前
Python Web框架对比与模型部署
开发语言·前端·python
wei8440678721 小时前
本地项目第一次推送到gitee上的完整命令
java·android studio
星球奋斗者1 小时前
计算机方向如何才能更好的找到工作?(成长心得)
java·后端·考研·软件工程·改行学it