SpringMVC接受参数
SpringMVC接受参数
SpringMVC 能将GET、POST请求中的参数(键值对、Json)自动转换成我们需要的类型,如基本类型、数组、List、Set、Map、POJO,以及它们的组合。
private Logger logger = LoggerFactory.getLogger(ParameterController.class);/*接受基本类型参数 * http://localhost:8080/param/primitive.do?id=10&name=张家乐&status=true*/ @RequestMapping("/primitive") @ResponseBody public String primitiveType(Long id, String name, Boolean status) throws UnsupportedEncodingException {name = new String(name.getBytes("ISO8859-1"), "UTF-8");logger.info("ID:{}, NAME:{}, STATUS:{}", id, name, status);StringBuilder str = new StringBuilder(String.valueOf(id)).append(" ").append(name).append(" ").append(status);return str.toString(); }/*接受数组参数,该方式只能在URL中传递 * http://localhost:8080/param/array.do?name=tom&name=rose&name=jack*/ @RequestMapping("/array") @ResponseBody public String[] arrayType(String[] name) {logger.info("Array:{}", Arrays.toString(name));return name; }/*接受数组参数,通过将数组放在Body中以JSON形式传输*/ @RequestMapping(value = "/array2", method = RequestMethod.POST) @ResponseBody public String[] arrayType2(@RequestBody String[] name) {logger.info("Array:{}", Arrays.toString(name));return name; }/*接受简单对象参数 * http://localhost:8080/param/simpleObject.do * Content-Type:application/x-www-form-urlencoded*/ @RequestMapping(value = "/simpleObject", method = RequestMethod.POST) @ResponseBody public User simpleObject(User user) {logger.info("Simple Object:{}", user.toString());return user; }/*接受List类型的参数 * http://localhost:8080/param/list.do * Content-Type:application/json * [1, 2, 3, 4, 5]*/ @RequestMapping(value = "/list", method = RequestMethod.POST) @ResponseBody public List<String> listType(@RequestBody List<String> list) {logger.info("List:{}", Arrays.toString(list.toArray()));return list; }/*接受List类型的参数 * http://localhost:8080/param/list2.do * Content-Type:application/json * [{"id": 10, "username": "jiale", "password": "23223"},{"id": 120, "username": "jiale2", "password": "2322f23"},{"id": 103, "username": "jial3e", "password": "232fd23"}]*/ @RequestMapping(value = "/list2", method = RequestMethod.POST) @ResponseBody public List<User> listType2(@RequestBody List<User> list) {logger.info("List:{}", Arrays.toString(list.toArray()));return list; }/*接受Set类型的参数 * http://localhost:8080/param/set.do * Content-Type:application/json * [{"id": 10, "username": "jiale", "password": "23223"},{"id": 120, "username": "jiale2", "password": "2322f23"},{"id": 103, "username": "jial3e", "password": "232fd23"}]*/ @RequestMapping(value = "/set", method = RequestMethod.POST) @ResponseBody public Set<User> setType(@RequestBody Set<User> set) {set.stream().forEach(e -> {logger.info("Id:{}, Username:{}, Password: {}\n", e.getId(), e.getUsername(), e.getPassword());});return set; }/*接受Map类型的参数 * http://localhost:8080/param/set.do * Content-Type:application/json * {"user01": {"id": 10, "username": "jiale", "password": "23223"}, "user02": {"id": 10, "username": "jiale", "password": "23223"}}*/ @RequestMapping(value = "/map", method = RequestMethod.POST) @ResponseBody public Map<String, User> setType(@RequestBody Map<String, User> map) {Set<String> keys = map.keySet();keys.stream().forEach(e -> {User user = map.get(e);logger.info("Id:{}, Username:{}, Password: {}\n", user.getId(), user.getUsername(), user.getPassword());});return map; }
SpringMVC接受参数相关推荐
- springMVC接受参数总结
springMVC接受参数分类及使用对应注解才能正确接受到参数,否则报400或者接受的参数值为null: 1.接受单个参数 @RequestParam 不需要转json串 2.接受一个实体 @Requ ...
- SpringMVC接受JSON参数详解及常见错误总结我改
SpringMVC接受JSON参数详解及常见错误总结 最近一段时间不想使用Session了,想感受一下Token这样比较安全,稳健的方式,顺便写一个统一的接口给浏览器还有APP.所以把一个练手项目的前 ...
- JAVA_OA(五):SpringMVC接受传入页面的参数值
总述一: SpringMVC页面传入值,一共有三种方式: request,方法参数,对象: //其实有篇思路比较清晰的博文,建议大家读一下:SpringMVC--接收请求参数和页面传参 首先配置和搭建 ...
- SpringMVC接受List的几种方式
说到SpringMVC接受List参数,可能有人会说这个是个坑!下面我来跟大家说怎么补这个坑.下面我分几种情况来讲: 一.只接收一个List 1.form表单提交 controller代码: @Res ...
- springmvc请求参数异常处理
springmvc请求参数异常处理 参考文章: (1)springmvc请求参数异常处理 (2)https://www.cnblogs.com/nosqlcoco/p/5844160.html 备忘一 ...
- python 装饰器 参数-python装饰器参数那些事_接受参数的装饰器
# -*- coding: utf-8 -*- #coding=utf-8 ''' @author: tomcat @license: (C) Copyright 2017-2019, Persona ...
- SpringMVC获取参数的几种方式
SpringMVC获取参数的几种方式 前言: 年末了,忙了一年了却发现系统的整理的东西很少,一些基础的东西都未做整理,这里就将它随便整理一下,增加一些印象,当然在网上看到一些好的资料也会整理下来以备后 ...
- jupyter 接受参数
jupyter 接受参数 在本地的python 文件中 args从命令行中接受 import argparse parser = argparse.ArgumentParser() parser.ad ...
- MapGuide 浏览器可接受参数分析
摘要: 这篇文章我们主要讲述如何通过阅读分析MapGuide的源代码对MapGuide浏览器所接受参数的分析. 如果你也正在困惑于MapGuide Ajax Viewer或者Fusion Viewer ...
最新文章
- 微生物组——宏基因组分析专题培训开课啦!!!
- 15-jQuery补充
- 解决:Caused by: redis.clients.jedis.exceptions.JedisDataException: WRONGTYPE Operation against a key
- Arduino Uno + Lora shield rf95_server 提示错误“Init failed!”
- 极光实时监听怎么调用_源码分析 Sentinel 实时数据采集实现原理(图文并茂)
- Oracle中备份用户对象的两种方法
- 第五篇:白话tornado源码之褪去模板的外衣
- vscode下载python库_如何在vscode中安装python库的方法步骤
- python urllib3 使用_使用Python URLLIB3下载文件
- 设计模式面试题(总结最全面的面试题!!!)
- fanuc系统屏蔽服务器,FANUC伺服轴的屏蔽方法
- win7系统未响应卡住_win7系统文件夹出现假死及未响应的解决方法
- 地图省界线什么样_地图上省份的划分精细复杂,为何分得这么细致?原来有这么多学问...
- linux cpu大小,如何查看linux cpu大小
- 猿辅导国奖选手妈妈在线分享教育经:数学新生代的成长之路
- CVPR 2022 | Accuracy和F1-score真的能代表车道线检测网络性能吗?
- 深度至尊之三信技术GHOST XP SP3装机精英白金版V11.3(NTFS)
- oracle00279,ORACLE數據恢復
- AudioES8374音频驱动调试
- 结对项目-小学生四则运算系统(GUI)
热门文章
- bwiki样式的活动倒计时html代码(带渐变色进度条)
- 红黑数和普通的二叉排序树有什么要求
- 传詹克团、吴忌寒将卸任比特大陆CEO一职
- 跳石(Skipping stone)
- windows PC版微信双开
- 使用CVXQUAD时出现,函数或变量 ‘op_rel_entr_epi_cone‘ 无法识别。
- express获取url路由地址参数的方法 获取get请求参数
- 【AC.HASH】OpenHarmony啃论文俱乐部——在基于位置的隐私感知服务中实现K-匿名之浅析
- PHP的介绍及应用,ajax的介绍及应用,跨域问题及jsonp解决方法
- Python中的乘方计算