springboot请求传参常用模板

注释很详细,直接上代码

项目结构

源码

HelloController

java 复制代码
package com.amoorzheyu.controller;

import com.amoorzheyu.pojo.User;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

/*  注解标识请求处理类 */
@RestController
public class HelloController {

    /* 标识请求路径 */
    //test
    @RequestMapping( "/hello")
    public String hello(){
        System.out.println("hello world~");
        return "hello world~";
    }

    //简单参数的接收---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    /*
    * 直接接收名字需要一一对应,若无则为null */
    @RequestMapping( "/simpleParameter1")
    public String hello2(String name, Integer age){
        String str = "hello world~" + name+",age:"+age;
        System.out.println(str);
        return str;
    }

    /*
    * 可使用RequestParam修改变量映射名,默认为必须,如果未传则会报错
    * @value:映射的变量名
    * required:是否必须,默认为true
    * defaultValue:默认值
    *  */
    @RequestMapping( "/simpleParameter2")
    public String hello3(@RequestParam(value = "name111", defaultValue = "unknown user",required = false) String name,
                         @RequestParam(value = "age111", defaultValue = "18",required = false) Integer age){
        String str = "hello world~" + name+",age:"+age;
        System.out.println(str);
        return str;
    }

    //实体参数--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    /* 简单实体参数,需要字段名一一对应 */
    @RequestMapping( "/entityParameter1")
    public String hello4(User user){
        String str = "hello world~" + user.getName()+",age:"+user.getAge();
        System.out.println(str);
        return str;
    }

    /* 复杂实体参数,同理,传值为addres.province的形式 */
    @RequestMapping( "/entityParameter2")
    public String hello5(User user){
        String str = "hello world~" + user.getName()+",age:"+user.getAge()+",province:"+user.getAddres().getProvince()+",city:"+user.getAddres().getCity();
        System.out.println(str);
        return str;
    }

    //数组集合参数----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

    /* 数组参数,请求参数名与形参中数组变量名相同,可以直接使用数组封装 */
    @RequestMapping( "/arrayParameter1")
    public String hello6(String[] name){
        System.out.println(Arrays.toString(name));
        return Arrays.toString(name);
    }

    /* 集合参数,请求参数名与形参中集合变量名相同,通过@RequestParam绑定参数关系 */
    @RequestMapping( "/arrayParameter2")
    public String hello7(@RequestParam List<String> hobby){
        System.out.println(hobby);
        return hobby.toString();
    }

    //日期时间类型参数--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    @RequestMapping( "/dateParameter")
    public String hello8(@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime updateTime){
        System.out.println(updateTime);
        return updateTime.toString();
    }

    //json类型参数----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    @RequestMapping( "/jsonParameter")
    public String hello9(@RequestBody User user){
        System.out.println(user);
        return user.toString();
    }

    //路径参数----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    @RequestMapping(value = "/pathVariable/{id}/{type}")
    public String hello10(@PathVariable("id") String id, @PathVariable("type") String type){
        System.out.println(id+" "+type);
        return id+" "+type;
    }
}

Address

java 复制代码
package com.amoorzheyu.pojo;

public class Address {
    private  String province;
    private  String city;

    @Override
    public String toString() {
        return "Addres{" +
                "province='" + province + '\'' +
                ", city='" + city + '\'' +
                '}';
    }

    public String getProvince() {
        return province;
    }

    public void setProvince(String province) {
        this.province = province;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }
}

User

java 复制代码
package com.amoorzheyu.pojo;

public class User {
    private Integer age;
    private String name;

    private Address address;

    public Address getAddres() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "User{" +
                "age=" + age +
                ", name='" + name + '\'' +
                ", addres=" + address +
                '}';
    }
}
相关推荐
_oP_i1 小时前
Pinpoint 是一个开源的分布式追踪系统
java·分布式·开源
mmsx1 小时前
android sqlite 数据库简单封装示例(java)
android·java·数据库
武子康1 小时前
大数据-258 离线数仓 - Griffin架构 配置安装 Livy 架构设计 解压配置 Hadoop Hive
java·大数据·数据仓库·hive·hadoop·架构
豪宇刘2 小时前
MyBatis的面试题以及详细解答二
java·servlet·tomcat
秋恬意2 小时前
Mybatis能执行一对一、一对多的关联查询吗?都有哪些实现方式,以及它们之间的区别
java·数据库·mybatis
刘大辉在路上3 小时前
突发!!!GitLab停止为中国大陆、港澳地区提供服务,60天内需迁移账号否则将被删除
git·后端·gitlab·版本管理·源代码管理
FF在路上3 小时前
Knife4j调试实体类传参扁平化模式修改:default-flat-param-object: true
java·开发语言
真的很上进3 小时前
如何借助 Babel+TS+ESLint 构建现代 JS 工程环境?
java·前端·javascript·css·react.js·vue·html
众拾达人4 小时前
Android自动化测试实战 Java篇 主流工具 框架 脚本
android·java·开发语言
皓木.4 小时前
Mybatis-Plus
java·开发语言