restful风格快速入门

365bet主页器 时间: 2025-06-27 15:11:19 作者: admin 查阅次数: 9049 公众评价: 594
restful风格快速入门

restful风格入门概述简介:REST(Representational State Transfer),表现形式状态转换。

传统风格

http://localhost/users/getById?id=1

http://localhost/users/saveUser

REST风格

http://localhost/users/1

http://localhost/users

对比:

对比可以发现,虽然传统风格的更详细,知道调用哪个函数,但是了太长了写起来不方便,REST风格的话就很简介,而且还可以隐藏函数调用的函数的名字,无法通过地址得知对资源进行何种操作,更加安全。

如何区分资源访问形式

在后面的代码演示的时候就会知道,REST风格定义了很多种类的Mapping注解,通过这些注解进行识别。

http://localhost/users 查询全部用户信息 GET(查询)http://localhost/users/1 查询指定用户信息 GET(查询)http://localhost/users 添加用户信息 POST(添加/保存)http://localhost/users 修改用户信息 PUT(修改/更新)http://localhost/users 删除用户信息 DELETE(删除)规范

REST风格更多的是一种规范,对于描述模块名称通常使用复数,比如users。

快速入门文件结构

pom.xml

代码语言:javascript代码运行次数:0运行复制

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

org.springframework.boot

spring-boot-starter-parent

2.7.6

com.example

SpringBootRestful

0.0.1-SNAPSHOT

SpringBootRestful

SpringBootRestful

8

org.springframework.boot

spring-boot-starter-web

org.springframework.boot

spring-boot-starter-test

test

org.springframework.boot

spring-boot-maven-plugin

User代码语言:javascript代码运行次数:0运行复制package com.example.springbootrestful.bean;

public class User {

private Integer id;

}Controller传统代码代码语言:javascript代码运行次数:0运行复制package com.example.springbootrestful.controller;

import com.example.springbootrestful.bean.User;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestBody;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.ResponseBody;

@Controller

public class UserController {

// save与update需要的是post方法才可以访问

@RequestMapping("/save")

@ResponseBody

public String save(@RequestBody User user){

System.out.println("user save..." + user);

return "{'module':'user save'}";

}

@RequestMapping("/delete")

@ResponseBody

public String delete(Integer id){

System.out.println("user delete..." + id);

return "{'module':'user delete'}";

}

@RequestMapping("/update")

@ResponseBody

public String update(@RequestBody User user){

System.out.println("user update..." + user);

return "{'module':'update save'}";

}

@RequestMapping("/getById")

@ResponseBody

public String getById(Integer id){

System.out.println("user getById..." + id);

return "{'module':'user getById'}";

}

@RequestMapping("/getAll")

@ResponseBody

public String getAll(){

System.out.println("user getAll...");

return "{'module':'user getAll'}";

}

}运行演示:

RESTful风格代码代码语言:javascript代码运行次数:0运行复制package com.example.springbootrestful.controller;

import com.example.springbootrestful.bean.User;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.*;

@Controller

public class UserController {

// save与update需要的是post方法才可以访问

@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':'update save'}";

}

@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'}";

}

}完善代码语言:javascript代码运行次数:0运行复制package com.example.springbootrestful.controller;

import com.example.springbootrestful.bean.User;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.*;

@RestController

@RequestMapping("/Users")

public class UserController {

// save与update需要的是post方法才可以访问

@PostMapping

public String save(@RequestBody User user){

System.out.println("user save..." + user);

return "{'module':'user save'}";

}

@DeleteMapping

public String delete(@PathVariable Integer id){

System.out.println("user delete..." + id);

return "{'module':'user delete'}";

}

@PutMapping

public String update(@RequestBody User user){

System.out.println("user update..." + user);

return "{'module':'update save'}";

}

@GetMapping("/{id}")

public String getById(@PathVariable Integer id){

System.out.println("user getById..." + id);

return "{'module':'user getById'}";

}

@GetMapping

public String getAll(){

System.out.println("user getAll...");

return "{'module':'user getAll'}";

}

}

关联

值得关注的中文Twitter用户精选指南
best365体育入口中文版

值得关注的中文Twitter用户精选指南

📅 06-27 👁️ 7732
考研英语1575词汇(何凯文)
365bet主页器

考研英语1575词汇(何凯文)

📅 06-27 👁️ 1293

链接