快速入门springboot 网页开发(四)springboot控制器之参数传递方法

准备工作

◼ 新建项目 demo4
◼ 新建 bean 包,并新建 Student类(代码见下页)
◼ 新建 controller包,并新建 TestController 控制器
本节控制器使用 @RestController 注解


public class Student {

    private Integer id;//主键.

    private String name;


    public Student(Integer id, String name) {
        this.id = id;
        this.name = name;
    }

    public Student() {
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

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


1. 无注解获取参数

◼ 直接把 HTTP 请求的参数写在后台方法的形参中, 允许参数为空。
◼ HTTP 请求的参数值(字符串)将自动转换为方法形参的类型。
◼ 要求:方法的参数名称要和 HTTP 请求的参数名称保持一致。
◼ 适用于 GET 和 POST 请求方式。

控制器例子

 @RequestMapping("/controller")
    public Student test(Integer id,String name){
        Student student = new Student();
        student.setId(id);
        student.setName(name);
        return student;
    }

访问 http://localhost:8080/controller?id=2000&name=张三
在这里插入图片描述

返回:
在这里插入图片描述
使用postman进行测试

2. 使用HttpServletRequest对象

◼ 方法的参数中添加 HttpServletRequest 对象。
◼ 通过该对象 getParameter(“xxx”) 获取请求的 “xxx” 参数值(字符串值)。
◼ 适用于 GET 和 POST 请求方式

控制器例子

@RequestMapping("/request")
    public Student test2(HttpServletRequest request){
        Integer id = Integer.parseInt(request.getParameter("id"));
        String name = request.getParameter("name");
        Student student = new Student();
        student.setId(id);
        student.setName(name);
        return student;
    }

访问 http://localhost:8080/request?id=312&name=李四
返回值
{
“id”: 312,
“name”: “李四”
}
发现和之前的方法有相同的功能

3. 使用实体类封装 ★★★

◼ 直接使用实体类做为控制器参数, Spring Boot会自动创建这个类的对象
并用 HTTP 参数装配它。
◼ 要求:实体类属性名称要和 HTTP 请求的参数名称保持一致。
◼ 适用于 GET 和 POST 请求方式。

@RequestMapping("/test3")
public Student test3( Student s ) {
return s;
}

在这里插入图片描述

4. 使用 @RequestParam 获取参数

◼ 在无注解的情况下,要求 HTTP 参数名与控制器参数名称保持一致,然
而现在在前后台分离的趋势下, 前端的命名规则可能与后端的不一样
◼ 使用 @RequestParam 注解来确定前后端参数名称的映射关系
◼ 适用于 GET 和 POST 请求方式。
◼ @RequestParam 有三个配置参数:
◼ value 为接收 HTTP 请求的参数名。
◼ required 表示是否必须, 默认为 true,表示参数必填。
◼ defaultValue 可设置请求参数的默认值。

 @RequestMapping("/test4")
    public Student test4( @RequestParam(value="stu_id") Integer id,
                          @RequestParam(value="stu_name") String name) {
        Student s=new Student();
        s.setId(id);
        s.setName(name);
        return s;
    }

访问 http://localhost:8080/test4?stu_id=312&stu_name=李四 ,可以返回值

required默认为true,如果少一个参数会报错,这个可以自己设置

改为

@RequestMapping("/test4")
public Student test4(@RequestParam(value="stu_id", required = true, defaultValue = "2019000") Integer id,
@RequestParam(value="stu_name", required = false) String name) {
Student s=new Student();
s.setId(id);
s.setName(name);
return s;
}

required = false 那么stu_name可以不传
在这里插入图片描述

前端传递数组

使用数组接收

@RequestMapping(value = "/test4")
public Integer[] test4( @RequestParam(value = "ids") Integer[ ] ids ) {
return ids;
}

前端请求: http://localhost:8080/test4?ids=2019001&ids=2019002
或: http://localhost:8080/test4?ids=2019001,2019002

推荐写法:
使用List接收

@RequestMapping(value = "/test4")
public List<Integer> test4( @RequestParam(value = "ids") List<Integer> ids ) {
return ids;
}

5. 使用 @PathVariable 获取参数

◼ REST风格请求, 例如: http://localhost:8080/test4/2019001/小明
◼ 后台请求映射:
@RequestMapping("/test4/{id}/{name}")
◼ 参数绑定:
@PathVariable(“xxx”) 将占位符参数"xxx"绑定到控制器方法的形参中。
◼ 注意:
(1) 路径上必须有参数(required=true) ,且不能设置默认值。
(2) 适用于 GET 和 POST 请求方式。

控制器例子

@RequestMapping("/test5/{id}/{name}")
public Student test5( @PathVariable(value = "id") Integer id,
@PathVariable(value = "name") String name) {
Student s=new Student();
s.setId(id);
s.setName(name);
return s;
}

正确前端请求: http://localhost:8080/test5/2019001/小明
错误前端请求: http://localhost:8080/test5/2019001
在这里插入图片描述

6. 使用 @RequestBody 获取参数

◼ @RequestBody 主要用来接收前端传递的 json 数据。
◼ 注意:
(1)使用 @RequestBody 接收数据时, 前端必须 POST 方式提交;
(2)且必须与 contentType = “application/json” 配合使用。不设置时使用的是默认值: application/x-www-form-urlencoded

示例:前端传递单个对象

使用Map<String,Object>接收json

@RequestMapping( value = "/test6", method = RequestMethod.POST )
public Map<String,Object> test6(@RequestBody Map<String,Object> map) {
return map;
}

使用实体类封装 json

@RequestMapping( value = "/test6", method = RequestMethod.POST )
public Student test6(@RequestBody Student s) {
return s;
}

method = RequestMethod.POST 注明 HTTP 是 POST 请求才能访问(默认是 GET 请求)
补充: @PostMapping("/test6") 注解代替:
@RequestMapping( value = “/test6”, method = RequestMethod.POST )

示例:前端传递对象数组

@RequestMapping( value = "/test6", method = RequestMethod.POST )
public List<Student> test6(@RequestBody List<Student> list) {
for (Student s:list) {
System.out.println(s.getId()+":"+s.getName());
}
return list;
}

7. 获取格式化参数

◼ 在一些应用中,往往需要格式化数据,其中最为典型的当属日期和货币。
◼ 例如:一些系统中日期格式约定为 yyyy-MM-dd,金额约定为货币符号
和用逗号隔开,如100万人民币写作 ¥1,000,000.00
◼ 使用 @DateTimeFormat 和 @NumberFormat 对获取的日期和数字进
行格式化处理。

@RequestMapping(value = "/test7")
public String test7( @DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss") Date date,
@NumberFormat(pattern = "#,###.#") BigDecimal price) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
String s_date = sdf.format(date); //日期格式化
DecimalFormat df = new DecimalFormat("¥#,###.##"); //保留两位小数
String s_price=df.format(price); //数字格式化
return "今天("+s_date+")的价格是: "+s_price;
}

访问 http://localhost:8080/test7?date=2020-12-21 12:21:20&price=1,234,567.987

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

葛济维的博客

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值