SpringBoot开发(五)SpringBoot接收请求参数

1. SpringBoot接收请求参数

1.1. 获取参数的方式

(1)通过request对象获取参数

(2)@RequestParam(针对请求头方式为x-www-form-ur lencoded)

(3)@RequestBody(针对请求头方式为application/json)

(4)@PathVariable(接收url路径参数)

1.2. 学员案例

1.2.1. 方式一

(1)在model文件夹下创建Student实体类

javascript 复制代码
package com.zzs.szyj.model;
public class Student {
    private String name;
    private Integer sex;
    private Integer age;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Integer getSex() {
        return sex;
    }
    public void setSex(Integer sex) {
        this.sex = sex;
    }
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
        this.age = age;
    }
}

(2)在controller文件夹下创建StudentController类

javascript 复制代码
package com.zzs.szyj.controller;
import com.zzs.szyj.model.Student;
import org.springframework.web.bind.annotation.*;
@RequestMapping("/student")
@RestController
public class StudentController {
    @PostMapping("save")
    public Student save(Student student) {
        return student;
    }
}

(3)运行,使用postman验证

1.2.2. 方式二@RequestBody

1.2.2.1. post请求

(1)StudentController实体类添加方法

javascript 复制代码
package com.zzs.szyj.controller;
import com.zzs.szyj.model.Student;
import org.springframework.web.bind.annotation.*;
@RequestMapping("/student")
@RestController
public class StudentController {
    /**
     * 获取参数方式一request
     *
     * @param student
     * @return
     */
    @PostMapping("save")
    public Student save(Student student) {
        return student;
    }
    /**
     * 获取参数方式二@RequestParam
     *
     * @param student
     * @return
     */
    @PostMapping("/save/json")
    public Student saveByJson(@RequestBody Student student) {
        return student;
    }
}

(2)运行,使用postman验证

1.2.2.2. get请求

(1)StudentController实体类添加方法

javascript 复制代码
    /**
     * @param student
     * @return
     * @desc 获取参数方式二@RequestParam-get请求
     */
    @GetMapping("/get")
    public Student getByNameAndAge(Student student) {
        return student;
    }

(2)运行,使用postman验证

1.2.3. 方式三@PathVariable

(1)StudentController实体类添加方法

javascript 复制代码
package com.zzs.szyj.controller;
import com.zzs.szyj.model.Student;
import org.springframework.web.bind.annotation.*;
@RequestMapping("/student")
@RestController
public class StudentController {
    /**
     * @param student
     * @return
     * @desc 获取参数方式一request
     */
    @PostMapping("save")
    public Student save(Student student) {
        return student;
    }
    /**
     * @param student
     * @return
     * @desc 获取参数方式二@RequestParam-post请求
     */
    @PostMapping("/save/json")
    public Student saveByJson(@RequestBody Student student) {
        return student;
    }
    /**
     * @param student
     * @return
     * @desc 获取参数方式二@RequestParam-get请求
     */
    @GetMapping("/get")
    public Student getByNameAndAge(Student student) {
        return student;
    }
    /**
     * @param age
     * @return
     * @desc 获取参数方式三@PathVariable-get请求
     */
    @GetMapping("/get/{age}")
    //@GetMapping("/get/{age1}")
    public Student getByUrlParam(@PathVariable Integer age) {
    //public Student getByUrlParam(@PathVariable("age1") Integer age) {
        Student student = new Student();
        student.setAge(age);
        return student;
    }
}

(2)运行,使用postman验证

1.2.4. 方式四@RequestParam

(1)StudentController实体类添加方法

javascript 复制代码
package com.zzs.szyj.controller;

import com.zzs.szyj.model.Student;
import org.springframework.web.bind.annotation.*;

@RequestMapping("/student")
@RestController
public class StudentController {
    /**
     * @param student
     * @return
     * @desc 获取参数方式一request
     */
    @PostMapping("save")
    public Student save(Student student) {
        return student;
    }

    /**
     * @param student
     * @return
     * @desc 获取参数方式二@RequestParam-post请求
     */
    @PostMapping("/save/json")
    public Student saveByJson(@RequestBody Student student) {
        return student;
    }
    /**
     * @param student
     * @return
     * @desc 获取参数方式二@RequestParam-get请求
     */
    @GetMapping("/get")
    public Student getByNameAndAge(Student student) {
        return student;
    }
    /**
     * @param age
     * @return
     * @desc 获取参数方式三@PathVariable-get请求
     */
    @GetMapping("/get/{age}")
    //@GetMapping("/get/{age1}")
    public Student getByUrlParam(@PathVariable Integer age) {
    //public Student getByUrlParam(@PathVariable("age1") Integer age) {
        Student student = new Student();
        student.setAge(age);
        return student;
    }
    /**
     * @param age
     * @return
     * @desc 获取参数方式四@RequestParam-get请求
     */
    @GetMapping(("/getParam"))
    public Student getByRequestParam(
            @RequestParam("name1") String name,
            @RequestParam("age1") Integer age) {
//        public Student getByRequestParam(
//               String name, Integer age) {
        Student student = new Student();
        student.setAge(age);
        student.setName(name);
        return student;
    }
}

(2)运行,使用postman验证

1.2.5. 方式五request

(1)StudentController实体类添加方法

javascript 复制代码
package com.zzs.szyj.controller;

import com.zzs.szyj.model.Student;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RequestMapping("/student")
@RestController
public class StudentController {
    /**
     * @param student
     * @return
     * @desc 获取参数方式一
     */
    @PostMapping("save")
    public Student save(Student student) {
        return student;
    }

    /**
     * @param student
     * @return
     * @desc 获取参数方式二@RequestParam-post请求
     */
    @PostMapping("/save/json")
    public Student saveByJson(@RequestBody Student student) {
        return student;
    }
    /**
     * @param student
     * @return
     * @desc 获取参数方式二@RequestParam-get请求
     */
    @GetMapping("/get")
    public Student getByNameAndAge(Student student) {
        return student;
    }
    /**
     * @param age
     * @return
     * @desc 获取参数方式三@PathVariable-get请求
     */
    @GetMapping("/get/{age}")
    //@GetMapping("/get/{age1}")
    public Student getByUrlParam(@PathVariable Integer age) {
    //public Student getByUrlParam(@PathVariable("age1") Integer age) {
        Student student = new Student();
        student.setAge(age);
        return student;
    }
    /**
     * @param age
     * @return
     * @desc 获取参数方式四@RequestParam-get请求
     */
    @GetMapping(("/getParam"))
    public Student getByRequestParam(
            @RequestParam("name1") String name,
            @RequestParam("age1") Integer age) {
//        public Student getByRequestParam(
//               String name, Integer age) {
        Student student = new Student();
        student.setAge(age);
        student.setName(name);
        return student;
    }
    @Resource
    private HttpServletRequest request;
    /**
     * @return
     * @desc 获取参数方式五-request
     */
    @GetMapping("/getByRequest")
    public Student getByRequest() {
        String name = request.getParameter("name1");
        Integer age = Integer.valueOf(request.getParameter("age1"));
        Student student = new Student();
        student.setAge(age);
        student.setName(name);
        return student;
    }
}

(2)运行,使用postman验证

2. SpringBoot接收请求参数报错

postman踩过的坑: "status": 415, "error": "Unsupported Media Type"

2.1. 问题概况

使用postman调接口的时候,status返回415,error提示Unsupported Media Type(不支持的媒体类型)。

2.2. 解决方案

Headers中添加类型

(1)点击Headers,添加KEY值:Content-Type,VALUE值:application/json,如果传递的不是json则改成对应值。

(2)检查body传参类型

点击Body,传json格式数据时,选择JSON,如果传递的不是json类型,则改成对应值。

相关推荐
H5css�海秀8 小时前
今天是自学大模型的第一天(sanjose)
后端·python·node.js·php
SuniaWang8 小时前
《Spring AI + 大模型全栈实战》学习手册系列 · 专题六:《Vue3 前端开发实战:打造企业级 RAG 问答界面》
java·前端·人工智能·spring boot·后端·spring·架构
韩立学长8 小时前
Springboot校园跑腿业务系统0b7amk02(程序、源码、数据库、调试部署方案及开发环境)系统界面展示及获取方式置于文档末尾,可供参考。
数据库·spring boot·后端
sheji34168 小时前
【开题答辩全过程】以 基于springboot的扶贫系统为例,包含答辩的问题和答案
java·spring boot·后端
代码栈上的思考9 小时前
消息队列:内存与磁盘数据中心设计与实现
后端·spring
程序员小假10 小时前
我们来说一下 b+ 树与 b 树的区别
java·后端
Meepo_haha10 小时前
Spring Boot 条件注解:@ConditionalOnProperty 完全解析
java·spring boot·后端
sheji341611 小时前
【开题答辩全过程】以 基于springboot的房屋租赁系统的设计与实现为例,包含答辩的问题和答案
java·spring boot·后端
Victor35611 小时前
MongoDB(57)如何优化MongoDB的查询性能?
后端
Victor35612 小时前
MongoDB(58)如何使用索引优化查询?
后端