【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'}";
    }
}
相关推荐
FastBean6 分钟前
Jackson View Extension Spring Boot Starter
java·后端
Seven971 小时前
剑指offer-79、最⻓不含重复字符的⼦字符串
java
皮皮林55111 小时前
Java性能调优黑科技!1行代码实现毫秒级耗时追踪,效率飙升300%!
java
冰_河11 小时前
QPS从300到3100:我靠一行代码让接口性能暴涨10倍,系统性能原地起飞!!
java·后端·性能优化
桦说编程14 小时前
从 ForkJoinPool 的 Compensate 看并发框架的线程补偿思想
java·后端·源码阅读
躺平大鹅15 小时前
Java面向对象入门(类与对象,新手秒懂)
java
初次攀爬者16 小时前
RocketMQ在Spring Boot上的基础使用
java·spring boot·rocketmq
花花无缺16 小时前
搞懂@Autowired 与@Resuorce
java·spring boot·后端
Derek_Smart18 小时前
从一次 OOM 事故说起:打造生产级的 JVM 健康检查组件
java·jvm·spring boot