springboot学习随笔(三):Controller参数映射
接上章内容,我们看看浏览器参数如何映射到我们的Controller层
我们新建UserController和User实

User.java
package com.example.main;
import java.io.Serializable;
public class User implements Serializable {
/**
*
*/
private static final long serialVersionUID = -7359143284242091031L;
private Long id;
private String name;
private Integer age;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
UserController.java
package com.example.demo.controller; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import com.example.main.User; @RestController
public class UserController { @RequestMapping("/user")
public User user(Long id,String name,int age){
User user = new User();
user.setId(id);
user.setName(name);
user.setAge(18);
return user;
}
}
参数映射的方式有三种:
第一种:参数映射
@RestController
public class UserController { @RequestMapping("/user")
public User user(Long id,String name,int age){
User user = new User();
user.setId(id);
user.setName(name);
user.setAge(age);
return user;
}
}
在浏览器访问,携带参数
第二种:对象映射
@RequestMapping("/user")
public User user(User user){
return user;
}
在浏览器访问,携带参数

第三种:通过ServletRequest对象
@RequestMapping("/user")
public User user(ServletRequest request){
Long id=Long.valueOf(request.getParameter("id"));
String name =request.getParameter("name");
int age =Integer.parseInt(request.getParameter("age")) ;
User user = new User();
user.setId(id);
user.setName(name);
user.setAge(age);
return user;
}

springboot学习随笔(三):Controller参数映射的更多相关文章
- Springboot学习02-webjars和静态资源映射规则
Springboot学习01-webjars和静态资源映射规则 前言 1-以前我们在IDEA中创建一个项目,添加web依赖包,我们现在是一个web应用,应该在man目录下面有一个webapp文件夹,将 ...
- springboot学习(三)——http序列化/反序列化之HttpMessageConverter
以下内容,如有问题,烦请指出,谢谢! 上一篇说掉了点内容,这里补上,那就是springmvc的http的序列化/反序列化,这里简单说下如何在springboot中使用这个功能. 使用过原生netty ...
- SpringMVC学习总结(三)——Controller接口详解(2)
4.5.ServletForwardingController 将接收到的请求转发到一个命名的servlet,具体示例如下: package cn.javass.chapter4.web.servle ...
- springboot学习(三)————使用HttpMessageConverter进行http序列化和反序列化
以下内容,如有问题,烦请指出,谢谢! 对象的序列化/反序列化大家应该都比较熟悉:序列化就是将object转化为可以传输的二进制,反序列化就是将二进制转化为程序内部的对象.序列化/反序列化主要体现在程序 ...
- springboot学习随笔(四):Springboot整合mybatis(含generator自动生成代码)
这章我们将通过springboot整合mybatis来操作数据库 以下内容分为两部分,一部分主要介绍generator自动生成代码,生成model.dao层接口.dao接口对应的sql配置文件 第一部 ...
- SpringBoot学习(三)
一.单个 controller 范围的异常处理 package com.xxx.secondboot.web; import org.springframework.web.bind.annotati ...
- springboot学习(三)——使用HttpMessageConverter进行http序列化和反序列化
以下内容,如有问题,烦请指出,谢谢! 对象的序列化/反序列化大家应该都比较熟悉:序列化就是将object转化为可以传输的二进制,反序列化就是将二进制转化为程序内部的对象.序列化/反序列化主要体现在程序 ...
- springboot学习(三) springboot文件配置
1.简介 springboot没有了原来自己整合Spring应用时繁多的XML配置内容,替代它的是在pom.xml中引入模块化的Starter POMs,其中各个模块都有自己的默认配置,所以如果不是特 ...
- SpringBoot学习(三)—— springboot快速整合swagger文档
目录 MyBatis 简介 引入mybatis组件 代码实战 MyBatis @ 简介 优点 最大的优点是SQL语句灵活,适合调优情景,业务复杂情景 劣势 最大的劣势是不同数据库之间的迁移 引入myb ...
随机推荐
- mysql Incorrect usage of UNION and ORDER BY 错误备忘
出现这个错误的语句是酱紫的 select xxx from aaa order by xxx union all select yyy from bbb order by yyy 错误原因居然是,如果 ...
- PowerShell 知识点总结
- Win10无法访问网上邻居电脑共享的文件夹怎么办
Win10无法访问网上邻居电脑共享的文件夹怎么办 现在许多电脑上装的都是Win系统,Win10无法访问网上邻居电脑共享的文件夹怎么办呢?下面小编为大家介绍下解决的方法吧! 1点击桌面上的“此电脑”图标 ...
- 【代码问题】MatConvNet自带example中 fast_rcnn_evaluate出错
fast_rcnn_evaluate中调用cnn_setup_data_voc07函数读取相关数据时,在类似 [gtids,t]=textread(sprintf(VOCopts.imgsetpath ...
- ubuntu 14.04升级gcc 4.8到5.3
添加PPA库 #sudo apt-get install software-properties-common #sudo add-apt-repository ppa:ubuntu-toolchai ...
- 备份原有yum源,设置为自建yum源的脚本
#脚本开始 #!/bin/bash cd /etc/yum.repos.d/ tar -zcvf yum.bak.tar.gz CentOS-* rm -rf CentOS* touch /etc/y ...
- form表单提交数据,页面必定会刷新,ajax提交数据不会刷新,做到悄悄提交,多选删除,ajax提交实例
很多页面用到的模态对话框,如知明网站https://dig.chouti.com/的登录页都是模态对话框, 当点登录时,是用的ajax提交,因为输入错了信息,有返回消息,而页面没有刷新. jquery ...
- 将centos的yum源修改为阿里云的yum源
CentOS系统更换软件安装源 第一步:备份你的原镜像文件,以免出错后可以恢复. mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentO ...
- 如何一步一步新建一个Owin项目
打开VS2015,新建Web应用程序,命名为OwinWeb(名字随意). 在弹出的“新建ASP.NET项目”窗口中选择“Empty”模板,“为以下项添加文件夹和核心引用”处全不选.点击确定. 创建完后 ...
- python3学习笔记13(数据结构)
参考http://www.runoob.com/python3/python3-data-structure.html 列表 列表是可修改的. 列表方法让列表可以方便的作为一个堆栈来使用,堆栈作为特定 ...
