原文网址: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)--方法/实例相关推荐

  1. gin获取路径中的参数

    gin获取路径中的参数 func main() {router := gin.Default()// 此规则能够匹配/user/john这种格式,但不能匹配/user/ 或 /user这种格式rout ...

  2. springboot获取多个请求参数_springboot获取URL请求参数的多种方式

    1.直接把表单的参数写在Controller相应的方法的形参中,适用于get方式提交,不适用于post方式提交. /** * 1.直接把表单的参数写在Controller相应的方法的形参中 * @pa ...

  3. Javascript 获取url路径中的参数

    需求 假设骑在路径:https://localhost/zhaopin?name=aa&age=18&state=2,现在要获取url中各参数的值,比如:当请求name时获取到aa,当 ...

  4. 由laravel 5.5无法获取url中的参数引发的apache的.htaccess文件问题

    一.前言 我这边碰到的问题就是,无法获取url中的get参数.本地使用的是laravel 5.5版本.明明url里面清清楚楚的写着参数,但是你却获取不到,这感觉实在是很气很气.刚开始以为是larave ...

  5. android 获取url中的参数,验证邮箱格式,截取字符串中键值对的值,String的字节长度,去空格,替换字符

    String ss="hello"; byte[] buff=ss.getBytes(); int f=buff.length; System.out.println(f); 字节 ...

  6. 如何获取url中的参数并传递给iframe中的报表

    在使用报表软件时,用户系统左边一般有目录树,点击报表节点就会在右侧网页的iframe中显示出报表,同时点击的时候也会传递一些参数给网页,比如时间和用户信息等.如何使网页中的报表能够获取到传递过来的参数 ...

  7. js获取url中的参数

    window.location: window的location对象 window.location.href 整个URl字符串(在浏览器中就是完整的地址栏) window.location.prot ...

  8. 如何获取URL中的参数

    获取URL中的参数 1. 使用JS函数获取URL参数 使用示例 2. Angular应用中,从URL中获取参数信息的方法 使用示例 ActivatedRoute属性 1. 使用JS函数获取URL参数 ...

  9. SpringMVC无法获取请求中的参数的问题的调查与解决(1)

    SpringMVC无法获取请求中的参数的问题的调查与解决(1) 参考文章: (1)SpringMVC无法获取请求中的参数的问题的调查与解决(1) (2)https://www.cnblogs.com/ ...

最新文章

  1. 实习总结之jquery实例
  2. mysql 多数据源访问_通过Spring Boot配置动态数据源访问多个数据库的实现代码
  3. 3-7:常见任务和主要工具之文本处理
  4. vs2015社区版不支持installshield
  5. 推荐常用的小程序Ui框架
  6. nacos配置中心使用_SpringBoot开发案例Nacos配置管理中心
  7. vue 属性是变量_手把手教你如何在生产环境检查 Vue 应用程序
  8. POCO C++库学习和分析 -- 线程 (二)
  9. javaScript读取xml文件
  10. Robo3T 1.4.3下载安装配置
  11. 数学分析教程(科大)——2.7笔记+习题
  12. 基于蓝墨云班课的翻转课堂实践
  13. 功率是电压电流乘积的波形在一个周期内积分后除以周期。
  14. php怎么弄面包屑,php实现面包屑导航例子分享
  15. Navicat:Access violation at address xxxxxxxxx in module 'navicat.exe'.Read of address xxxxxx
  16. Jmeter监控之PerfMon Metrics Collector
  17. 51单片机教程(从原理开始基于汇编)
  18. Chrome插件(扩展)开发全攻略(干货)
  19. EM期望最大化算法实现二项混合分布与高斯混合分布
  20. 一个用js写的沙漏程序 hourglass

热门文章

  1. cmd查询mysql版本
  2. redis有序集合键(数据结构篇)
  3. MySQL数据库同步数据到Oracle
  4. Eclipse中svn的使用 (详细)
  5. Go 结构体格式化输出
  6. 为什么甲骨文在开源上刷两面派
  7. 小数右侧补0,整数左侧补0 c++
  8. 网易实习一面(游戏测试开发)
  9. 网络安全篇 ACS 5.6的安装激活-08
  10. m2接口和nvme协议接口_M.2接口是如何辨别插入的SSD是SATA协议还是NVME协议?