【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'}";
    }
}
相关推荐
冷雨夜中漫步2 小时前
Python快速入门(6)——for/if/while语句
开发语言·经验分享·笔记·python
JH30733 小时前
SpringBoot 优雅处理金额格式化:拦截器+自定义注解方案
java·spring boot·spring
m0_736919104 小时前
C++代码风格检查工具
开发语言·c++·算法
Coder_Boy_4 小时前
技术让开发更轻松的底层矛盾
java·大数据·数据库·人工智能·深度学习
2501_944934734 小时前
高职大数据技术专业,CDA和Python认证优先考哪个?
大数据·开发语言·python
invicinble5 小时前
对tomcat的提供的功能与底层拓扑结构与实现机制的理解
java·tomcat
较真的菜鸟5 小时前
使用ASM和agent监控属性变化
java
黎雁·泠崖5 小时前
【魔法森林冒险】5/14 Allen类(三):任务进度与状态管理
java·开发语言
2301_763472466 小时前
C++20概念(Concepts)入门指南
开发语言·c++·算法
TechWJ6 小时前
PyPTO编程范式深度解读:让NPU开发像写Python一样简单
开发语言·python·cann·pypto