springboot中controller的使用
一、知识点
1 | @Controller | 处理http请求(不推荐使用) |
2 | @RestController | spring4之后新加的注解,原来返回json需要@ResponseBody配合@Controller |
3 | @RequestMapping | 配置Url映射 |
4 | @GetMapping | 当 在方法上的注释时@RequestMapping的简化版,推荐使用,类似的还有@PostMapping等等 |
5 | @RequestParam | 映射请求参数到java方法的参数 |
6 | @PageableDefault | 指定分页参数默认值 |
7 | @RequestBody | 注解用于将Controller的方法参数,根据HTTP Request Header的content-Type的内容,通过适当的HttpMessageConverter转换为JAVA类 |
二、具体使用讲解
1.@Controller(了解即可,现在的开发基本都是前后端分离,不用再使用模版的方式,采用REST方式返回json数据是主流)
需要配合模版的使用
1)打开pom.xml
添加spring官方的一个模版thymeleaf
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
2)在resources下新建文件夹templates,然后在其中新建一个html,index.html
<h1>hello spring boot!</h1>
3)controller中将@RestController改为@Controller
package com.dechy.girl.girl; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController; @Controller
public class HelloController { @Autowired
private GirlProperties girlProperties; @RequestMapping(value = "/hello", method = RequestMethod.GET)
public String say (){
return "index";
}
}
4)启动后,访问得到index.html的内容
2.@RestController
他是@Controller和@ResponseBody的组合
1)修改controller文件
package com.dechy.girl.girl; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController; @Controller
@ResponseBody
public class HelloController { @Autowired
private GirlProperties girlProperties; @RequestMapping(value = "/hello", method = RequestMethod.GET)
public String say (){
return girlProperties.getCupSize();
}
}
此处为了简便,采用@RestController即可
3.@RequestMapping
1)可以设置映射多个地址,这样访问多个地址能可以返回相同的内容,例如
package com.dechy.girl.girl; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController; @RestController
public class HelloController { @Autowired
private GirlProperties girlProperties; @RequestMapping(value = {"/hello","/hi"}, method = RequestMethod.GET)
public String say (){
return girlProperties.getCupSize();
}
}
2)可以给整个类映射一个url,如下, 访问http://127.0.0.1:8082/hello/say即可
package com.dechy.girl.girl; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController; @RestController
@RequestMapping("/hello")
public class HelloController { @Autowired
private GirlProperties girlProperties; @RequestMapping(value = "/say", method = RequestMethod.GET)
public String say (){
return girlProperties.getCupSize();
}
}
3)处理url的参数
@PathVariable |
获取url中的数据 |
@RequestParam |
获取请求参数的值 |
@GetMapping |
组合注解 |
@PageableDefault |
指定分页参数默认值 |
i)@PathVariable
package com.dechy.girl.girl; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController; @RestController
@RequestMapping("/hello")
public class HelloController { @Autowired
private GirlProperties girlProperties; @RequestMapping(value = "/say/{id}", method = RequestMethod.GET)
public String say (@PathVariable("id") Integer id){
return "id:"+id;
}
}
或者改成
@RequestMapping(value = "/{id}/say", method = RequestMethod.GET)
ii)@RequestParam
对于传统的url.如http://127.0.0.1:8082/hello/say?id=111
package com.dechy.girl.girl; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*; @RestController
@RequestMapping("/hello")
public class HelloController { @Autowired
private GirlProperties girlProperties; @RequestMapping(value = "/say", method = RequestMethod.GET)
public String say (@RequestParam("id") Integer myId){
return "id:"+myId;
}
}
其中黄色的不用一直,id对应的是url中的参数,myId可以自定义
另外,可以给@RequestParam设置默认值
package com.dechy.girl.girl; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*; @RestController
@RequestMapping("/hello")
public class HelloController { @Autowired
private GirlProperties girlProperties; @RequestMapping(value = "/say", method = RequestMethod.GET)
public String say (@RequestParam(value="id",required = false,defaultValue = "0") Integer myId){
return "id:"+myId;
}
}
iii)@GetMapping(推荐)
用@RequestParam的话,感觉代码太长,那么就可以使用@GetMapping,另外还可以有@PostMapping @PutMapping等等
package com.dechy.girl.girl; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*; @RestController
@RequestMapping("/hello")
public class HelloController { @Autowired
private GirlProperties girlProperties; @GetMapping(value="/say")
public String say (@RequestParam(value="id",required = false,defaultValue = "0") Integer myId){
return "id:"+myId;
}
}
iiii)@PageableDefault
指定指定分页参数默认值。
@RequestMapping(value = "/user",method = RequestMethod.GET)
public List<User> query(UserQueryCondition userQueryCondition, @PageableDefault(page=1,size=10,sort="username,asc") Pageable pageable){
System.out.println(pageable.getPageNumber());
System.out.println(pageable.getSort());
System.out.println(pageable.getPageSize());
System.out.println(userQueryCondition.toString());
List<User> users=new ArrayList<>();
users.add(new User());
users.add(new User());
users.add(new User());
return users;
}
备注:此处Pageable需要引入如下依赖
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
</dependency>
springboot中controller的使用的更多相关文章
- Q1:spring-boot中Controller路径无法被访问的问题
在学习spring-boot入门的第一个例子就是spring-boot-web的一个在页面上输出hello-world的例子,在运行这个例子的时候我遇到了下面这个简单的问题,但是第一次解决还是花了我很 ...
- springboot 中controller 返回html界面或 jsp界面
参考链接:https://blog.csdn.net/qq_15260315/article/details/80907056 经尝试,返回html界面没问题,但是返回jsp界面是有问题的,just ...
- springboot中Controller没有被扫描
今天给客户开发登陆的密码加密需求,研究一下想,需要在本地搭一套环境,前台用js实现RAS加密,后台使用java解密.本是一套非常简单的环境,看最近springboot比较常用,所以想要搭一下sprin ...
- SpringBoot 中 @RestController 和 @Controller 的区别
1 - 在springboot中,@RestController 相当于 @Controller + @ResponseBody;2 - 即在Controller类中,若想返回jsp或html页面,则 ...
- SpringBoot 中常用注解@Controller/@RestController/@RequestMapping的区别
SpringBoot中常用注解@Controller/@RestController/@RequestMapping的区别 @Controller 处理http请求 @Controller //@Re ...
- SpringBoot 中常用注解@Controller/@RestController/@RequestMapping介绍
原文 SpringBoot 中常用注解 @Controller/@RestController/@RequestMapping介绍 @Controller 处理http请求 @Controller / ...
- springboot中的controller注解没有生效
springboot中的controller注解没有生效 , 启动的Application类没有在controller的父目录或同级目录
- 如何在SpringBoot中使用JSP ?但强烈不推荐,果断改Themeleaf吧
做WEB项目,一定都用过JSP这个大牌.Spring MVC里面也可以很方便的将JSP与一个View关联起来,使用还是非常方便的.当你从一个传统的Spring MVC项目转入一个Spring Boot ...
- springboot中swaggerUI的使用
demo地址:demo-swagger-springboot springboot中swaggerUI的使用 1.pom文件中添加swagger依赖 2.从github项目中下载swaggerUI 然 ...
随机推荐
- 通用坐标投影转换器Uneversal Coord Transformer
关键词:投影,重投影,坐标转换,坐标系,空间参考,北京54,西安80,中国2000,WGS84,UTM,墨卡托,网络墨卡托 软件名称:通用坐标投影转换器Uneversal Coord Transfor ...
- tensorflow 之tensorboard 对比不同超参数训练结果
我们通常使用tensorboard 统计我们的accurate ,loss等,并绘制曲线,通常是使用一次训练中的, 但是,机器学习中通常要对比不同的 ‘超参数’给模型训练和预测能力的不同这时候如何整合 ...
- lunux开放80端口(本地访问不了linux文件可能是这个原因)
/sbin/iptables -I INPUT -p tcp --dport 80 -j ACCEPT #开启80端口 /etc/rc.d/init.d/iptables save #保存配置 / ...
- QQ传文件测试要点
总-分-总 UI: 进度:进度条.百分比.速度.已传文件大小 显示传送文件图标.悬浮有文字 功能入口:图标.菜单项 各种提示:开始传送.各种异常信息的提示.传送结束 给好友传文件.给群传文件 功能 ...
- JDBC的基本概念
英文名:Java DataBase Connectivity 中文名:数据库连接 作用: java操作数据库 本质上(sun公司的程序员)定义的一套操作关系型数据库的规则也就是接口,各数据库厂商实现接 ...
- unity延时方法
http://www.cnblogs.com/louissong/p/3832960.html 借鉴上面的博客中的内容: Invoke(methodName: string, time: float) ...
- yum update 自动忽略内核更新
系统每天凌晨 3 点自动执行 yum update 任务 但升级内核后,会出现下面情况 一些编译软件需要内核模块才能够被调用, 而内核模块需要与当前版本内核编译后才能够使用, 假设内核升级后,之前软件 ...
- 物料没加DUMMY
会加入DUMMY的表 IN_ITEM,IN_ITEM_SITE,IN_SALES_ORDER 加入DUMMY的存储过程名为SAP_MATERIAL_SO. FP_CHANGE_MO_ROUTING的第 ...
- metasploit framework(一):基本使用
它位于/usr/share/metasploit-framework 进入到modules目录,有六大模块 exploits:系统漏洞利用的流程,对系统漏洞注入一些特定的代码,使其覆盖程序执行寄存器, ...
- 数据库类型空间效率探索(三)-char
测试环境 表信息 表数据量22.23万,占用空间44.494M 用到的sql语句 增加列:alter table t_type add column new_column char(1) defaul ...