SpringBoot--获取路径中的参数(x-www-form-urlencoded)--方法/实例
原文网址:SpringBoot--获取路径中的参数(x-www-form-urlencoded)--方法/实例_IT利刃出鞘的博客-CSDN博客
简介
本文用示例介绍SpringMVC如何获取路径中的参数。也就是: Content-Type为x-www-form-urlencoded。
代码
Controller
BasicController.java
package com.example.demo.controller;import com.example.demo.entity.User;
import org.springframework.web.bind.annotation.*;import java.util.Arrays;
import java.util.List;// localhost:8080/basic/no_annotation/string1
@RequestMapping("/basic")
@RestController
public class BasicController {@RequestMapping("/1")public String test1(int intVal, Long longVal, String myStr) {System.out.println("intVal : " + intVal);System.out.println("longVal : " + longVal);System.out.println("myStr : " + myStr);return "Hello world";}@RequestMapping("/2")public User test2(@RequestParam Integer intVal,@RequestParam(value = "my_str", required = false) String[] myStr,@RequestParam String[] password,@RequestParam List<Integer> scoreArray,@RequestParam Integer age,User user) {System.out.println("intVal : " + intVal);System.out.println("age : " + age);System.out.println("myStr : " + Arrays.asList(myStr));System.out.println("password : " + Arrays.asList(password));System.out.println("scoreArray : " + scoreArray);return user;}@RequestMapping("/3")public User test3(@RequestBody User user) {return user;}
}
Entity
User.java
package com.example.demo.entity;import lombok.Data;
import java.util.List;@Data
public class User {private String name;private Integer age;private String[] password;private List<Integer> scoreArray;
}
Account.java
package com.example.demo.entity;import lombok.Data;
import java.io.Serializable;@Data
public class Account implements Serializable {private String phoneNum;private String[] emails;
}
测试
测试1:基本类型为空导致错误
http://localhost:8080/basic/1
后端打印
java.lang.IllegalStateException: Optional int parameter 'intVal' is present but cannot be translated into a null value due to being declared as a primitive type. Consider declaring it as object wrapper for the corresponding primitive type.
前端结果
测试2:正常操作
其实也不正常,最好不要用基本类型接收。
http://localhost:8080/basic/1?intVal=2&myStr=hehe
后端
intVal : 2
longVal : null
myStr : hehe
前端
测试3:正常操作
http://localhost:8080/basic/2?intVal=2&my_str=Tony,Stark&name=Jarvis&age=21&password=ab,cd&scoreArray=99,98&phoneNum=12
后端
intVal : 2
age : 21
myStr : [Tony, Stark]
password : [ab, cd]
scoreArray : [99, 98]
前端
结论
- 如果一个参数,实体类与接口参数都有,则都会赋值。
- 若实体类中有数组/List 成员,也会对它直接赋值。
- 若实体类含有实体类,无法赋值。因为参数中没有与实体类相同名字的key,此时就要用form-data或者json了。(或者x-www-urlencoded也有写类参数的方法,目前不知道)
测试4:@RequestParam缺少参数访问(失败)
http://localhost:8080/basic/2?intVal=2&my_str=Tony,Stark&name=Jarvis&age=21&password=ab,cd&phoneNum=12
后端
2020-07-04 18:00:37.222 WARN 15844 --- [nio-8080-exec-8] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.web.bind.MissingServletRequestParameterException: Required List parameter 'scoreArray' is not present]
前端
测试5:用@RequestBody接收(失败)
http://localhost:8080/basic/3?intVal=2&my_str=Tony,Stark&name=Jarvis&age=21&password=ab,cd&scoreArray=99,98&phoneNum=12
后端
2020-07-04 14:02:55.954 WARN 15844 --- [nio-8080-exec-3] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.http.converter.HttpMessageNotReadableException: Required request body is missing: public com.example.demo.entity.User com.example.demo.controller.BasicController.test3(com.example.demo.entity.User)]
前端
SpringBoot--获取路径中的参数(x-www-form-urlencoded)--方法/实例相关推荐
- gin获取路径中的参数
gin获取路径中的参数 func main() {router := gin.Default()// 此规则能够匹配/user/john这种格式,但不能匹配/user/ 或 /user这种格式rout ...
- springboot获取多个请求参数_springboot获取URL请求参数的多种方式
1.直接把表单的参数写在Controller相应的方法的形参中,适用于get方式提交,不适用于post方式提交. /** * 1.直接把表单的参数写在Controller相应的方法的形参中 * @pa ...
- Javascript 获取url路径中的参数
需求 假设骑在路径:https://localhost/zhaopin?name=aa&age=18&state=2,现在要获取url中各参数的值,比如:当请求name时获取到aa,当 ...
- 由laravel 5.5无法获取url中的参数引发的apache的.htaccess文件问题
一.前言 我这边碰到的问题就是,无法获取url中的get参数.本地使用的是laravel 5.5版本.明明url里面清清楚楚的写着参数,但是你却获取不到,这感觉实在是很气很气.刚开始以为是larave ...
- android 获取url中的参数,验证邮箱格式,截取字符串中键值对的值,String的字节长度,去空格,替换字符
String ss="hello"; byte[] buff=ss.getBytes(); int f=buff.length; System.out.println(f); 字节 ...
- 如何获取url中的参数并传递给iframe中的报表
在使用报表软件时,用户系统左边一般有目录树,点击报表节点就会在右侧网页的iframe中显示出报表,同时点击的时候也会传递一些参数给网页,比如时间和用户信息等.如何使网页中的报表能够获取到传递过来的参数 ...
- js获取url中的参数
window.location: window的location对象 window.location.href 整个URl字符串(在浏览器中就是完整的地址栏) window.location.prot ...
- 如何获取URL中的参数
获取URL中的参数 1. 使用JS函数获取URL参数 使用示例 2. Angular应用中,从URL中获取参数信息的方法 使用示例 ActivatedRoute属性 1. 使用JS函数获取URL参数 ...
- SpringMVC无法获取请求中的参数的问题的调查与解决(1)
SpringMVC无法获取请求中的参数的问题的调查与解决(1) 参考文章: (1)SpringMVC无法获取请求中的参数的问题的调查与解决(1) (2)https://www.cnblogs.com/ ...
最新文章
- 实习总结之jquery实例
- mysql 多数据源访问_通过Spring Boot配置动态数据源访问多个数据库的实现代码
- 3-7:常见任务和主要工具之文本处理
- vs2015社区版不支持installshield
- 推荐常用的小程序Ui框架
- nacos配置中心使用_SpringBoot开发案例Nacos配置管理中心
- vue 属性是变量_手把手教你如何在生产环境检查 Vue 应用程序
- POCO C++库学习和分析 -- 线程 (二)
- javaScript读取xml文件
- Robo3T 1.4.3下载安装配置
- 数学分析教程(科大)——2.7笔记+习题
- 基于蓝墨云班课的翻转课堂实践
- 功率是电压电流乘积的波形在一个周期内积分后除以周期。
- php怎么弄面包屑,php实现面包屑导航例子分享
- Navicat:Access violation at address xxxxxxxxx in module 'navicat.exe'.Read of address xxxxxx
- Jmeter监控之PerfMon Metrics Collector
- 51单片机教程(从原理开始基于汇编)
- Chrome插件(扩展)开发全攻略(干货)
- EM期望最大化算法实现二项混合分布与高斯混合分布
- 一个用js写的沙漏程序 hourglass