目录

get请求

测试结果

基本数据类型与引用数据类型

自定义数据类型 

 json 数据类型

 post 请求

测试结果

基本数据类型与引用数据类型

自定义数据类型

​编辑 json 数据类型

 put 请求

测试结果

基本数据类型和引用数据类型

 自定义数据类型

 json 数据类型

delete 请求

测试结果

基本数据类型与引用数据类型

自定义数据类型

​编辑 

 json 数据类型

测试工具:idea + Postman

Student 类

@Data

public class Student {

public String username;

private Integer age;

}

get请求

package com.qfedu.springboot02.controller;

import com.qfedu.springboot02.enrity.Student;

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

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

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

/**

* @Author XY

* @Date 2022/6/13 20:11

*

* get 请求通常用作获取数据,可以式获取某个页面,可以是获取查询后的数据等

*/

@RestController

public class GetController {

/*传递基本数据类型与引用数据类型*/

@GetMapping("/")

public String show(String username, Integer age) {

return "username=" + username + " , age=" + age;

}

/*传递自定义数据类型*/

@GetMapping("/s")

public Student show01(Student student) {

return student;

}

/*使用 json 传递参数*/

@GetMapping("/sj")

public Student showJson(@RequestBody Student student) {

return student;

}

}

测试结果

基本数据类型与引用数据类型

自定义数据类型 

 json 数据类型

 post 请求

package com.qfedu.springboot02.controller;

import com.qfedu.springboot02.enrity.Student;

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

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

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

/**

* @Author XY

* @Date 2022/6/13 20:29

*

* post 请求通常用户添加数据

*/

@RestController

public class PostController {

/*传递基本数据类型与引用数据类型*/

@PostMapping("/")

public String add(String username, Integer age) {

return "username=" + username + " , age=" + age;

}

/*自定义数据类型*/

@PostMapping("/s")

public Student add01(Student student) {

return student;

}

@PostMapping("/sj")

public Student addJson(@RequestBody Student student) {

return student;

}

}

测试结果

基本数据类型与引用数据类型

自定义数据类型

 json 数据类型

 put 请求

package com.qfedu.springboot02.controller;

import com.qfedu.springboot02.enrity.Student;

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

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

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

/**

* @Author XY

* @Date 2022/6/13 20:47

*

* put 请求通常用于更新数据

*/

@RestController

public class PutController {

/*基本数据类型与引用数据类型*/

@PutMapping("/")

public String update(String username, Integer age) {

return "username=" + username + " , age=" + age;

}

/*自定义数据类型*/

@PutMapping("/u")

public String update01(Student student) {

return "username=" + student.getUsername() + " , age=" + student.getAge();

}

/*json 数据类型*/

@PutMapping("/up")

public String update02(@RequestBody Student student) {

return "username=" + student.getUsername() + " , age=" + student.getAge();

}

}

测试结果

基本数据类型和引用数据类型

 自定义数据类型

 json 数据类型

delete 请求

测试结果

基本数据类型与引用数据类型

自定义数据类型

 

 json 数据类型

总结:get、post、put、delete 四种请求分别对应获取、添加、更新、删除数据,其实四种请求方式都一样,正常情况下用哪一种请求都没有区别。 

 

查看原文