栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Controller 中的参数传递

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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;


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

    
    @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;


@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;


@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();
    }

    
    @PutMapping("/up")
    public String update02(@RequestBody Student student) {
        return "username=" + student.getUsername() + " , age=" + student.getAge();
    }
}
测试结果

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

 自定义数据类型

 json 数据类型

delete 请求 测试结果 基本数据类型与引用数据类型

自定义数据类型

   json 数据类型

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

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/976176.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号