Controller 中的参数传递

目录

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
 * <p>
 * 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
 * <p>
 * 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 四种请求分别对应获取、添加、更新、删除数据,其实四种请求方式都一样,正常情况下用哪一种请求都没有区别。 

 

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值