一、知识点

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

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-thymeleaf</artifactId>
  4. </dependency>

2)在resources下新建文件夹templates,然后在其中新建一个html,index.html

  1. <h1>hello spring boot!</h1>

3)controller中将@RestController改为@Controller

  1. package com.dechy.girl.girl;
  2.  
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.beans.factory.annotation.Value;
  5. import org.springframework.stereotype.Controller;
  6. import org.springframework.web.bind.annotation.RequestMapping;
  7. import org.springframework.web.bind.annotation.RequestMethod;
  8. import org.springframework.web.bind.annotation.RestController;
  9.  
  10. @Controller
  11. public class HelloController {
  12.  
  13. @Autowired
  14. private GirlProperties girlProperties;
  15.  
  16. @RequestMapping(value = "/hello", method = RequestMethod.GET)
  17. public String say (){
  18. return "index";
  19. }
  20. }

4)启动后,访问得到index.html的内容

2.@RestController

他是@Controller和@ResponseBody的组合

1)修改controller文件

  1. package com.dechy.girl.girl;
  2.  
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.beans.factory.annotation.Value;
  5. import org.springframework.stereotype.Controller;
  6. import org.springframework.web.bind.annotation.RequestMapping;
  7. import org.springframework.web.bind.annotation.RequestMethod;
  8. import org.springframework.web.bind.annotation.ResponseBody;
  9. import org.springframework.web.bind.annotation.RestController;
  10.  
  11. @Controller
  12. @ResponseBody
  13. public class HelloController {
  14.  
  15. @Autowired
  16. private GirlProperties girlProperties;
  17.  
  18. @RequestMapping(value = "/hello", method = RequestMethod.GET)
  19. public String say (){
  20. return girlProperties.getCupSize();
  21. }
  22. }

此处为了简便,采用@RestController即可

3.@RequestMapping

1)可以设置映射多个地址,这样访问多个地址能可以返回相同的内容,例如

  1. package com.dechy.girl.girl;
  2.  
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.beans.factory.annotation.Value;
  5. import org.springframework.web.bind.annotation.RequestMapping;
  6. import org.springframework.web.bind.annotation.RequestMethod;
  7. import org.springframework.web.bind.annotation.RestController;
  8.  
  9. @RestController
  10. public class HelloController {
  11.  
  12. @Autowired
  13. private GirlProperties girlProperties;
  14.  
  15. @RequestMapping(value = {"/hello","/hi"}, method = RequestMethod.GET)
  16. public String say (){
  17. return girlProperties.getCupSize();
  18. }
  19. }

2)可以给整个类映射一个url,如下, 访问http://127.0.0.1:8082/hello/say即可

  1. package com.dechy.girl.girl;
  2.  
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.beans.factory.annotation.Value;
  5. import org.springframework.web.bind.annotation.RequestMapping;
  6. import org.springframework.web.bind.annotation.RequestMethod;
  7. import org.springframework.web.bind.annotation.RestController;
  8.  
  9. @RestController
  10. @RequestMapping("/hello")
  11. public class HelloController {
  12.  
  13. @Autowired
  14. private GirlProperties girlProperties;
  15.  
  16. @RequestMapping(value = "/say", method = RequestMethod.GET)
  17. public String say (){
  18. return girlProperties.getCupSize();
  19. }
  20. }

3)处理url的参数

  1. @PathVariable
获取url中的数据
  1. @RequestParam
获取请求参数的值
  1. @GetMapping
组合注解
  1. @PageableDefault
 指定分页参数默认值

i)@PathVariable

  1. package com.dechy.girl.girl;
  2.  
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.web.bind.annotation.PathVariable;
  5. import org.springframework.web.bind.annotation.RequestMapping;
  6. import org.springframework.web.bind.annotation.RequestMethod;
  7. import org.springframework.web.bind.annotation.RestController;
  8.  
  9. @RestController
  10. @RequestMapping("/hello")
  11. public class HelloController {
  12.  
  13. @Autowired
  14. private GirlProperties girlProperties;
  15.  
  16. @RequestMapping(value = "/say/{id}", method = RequestMethod.GET)
  17. public String say (@PathVariable("id") Integer id){
  18. return "id:"+id;
  19. }
  20. }

或者改成

  1. @RequestMapping(value = "/{id}/say", method = RequestMethod.GET)

ii)@RequestParam

对于传统的url.如http://127.0.0.1:8082/hello/say?id=111

  1. package com.dechy.girl.girl;
  2.  
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.web.bind.annotation.*;
  5.  
  6. @RestController
  7. @RequestMapping("/hello")
  8. public class HelloController {
  9.  
  10. @Autowired
  11. private GirlProperties girlProperties;
  12.  
  13. @RequestMapping(value = "/say", method = RequestMethod.GET)
  14. public String say (@RequestParam("id") Integer myId){
  15. return "id:"+myId;
  16. }
  17. }

其中黄色的不用一直,id对应的是url中的参数,myId可以自定义

另外,可以给@RequestParam设置默认值

  1. package com.dechy.girl.girl;
  2.  
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.web.bind.annotation.*;
  5.  
  6. @RestController
  7. @RequestMapping("/hello")
  8. public class HelloController {
  9.  
  10. @Autowired
  11. private GirlProperties girlProperties;
  12.  
  13. @RequestMapping(value = "/say", method = RequestMethod.GET)
  14. public String say (@RequestParam(value="id",required = false,defaultValue = "0") Integer myId){
  15. return "id:"+myId;
  16. }
  17. }

iii)@GetMapping(推荐)

用@RequestParam的话,感觉代码太长,那么就可以使用@GetMapping,另外还可以有@PostMapping @PutMapping等等

  1. package com.dechy.girl.girl;
  2.  
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.web.bind.annotation.*;
  5.  
  6. @RestController
  7. @RequestMapping("/hello")
  8. public class HelloController {
  9.  
  10. @Autowired
  11. private GirlProperties girlProperties;
  12.  
  13. @GetMapping(value="/say")
  14. public String say (@RequestParam(value="id",required = false,defaultValue = "0") Integer myId){
  15. return "id:"+myId;
  16. }
  17. }

iiii)@PageableDefault

指定指定分页参数默认值。

  1. @RequestMapping(value = "/user",method = RequestMethod.GET)
  2. public List<User> query(UserQueryCondition userQueryCondition, @PageableDefault(page=1,size=10,sort="username,asc") Pageable pageable){
  3. System.out.println(pageable.getPageNumber());
  4. System.out.println(pageable.getSort());
  5. System.out.println(pageable.getPageSize());
  6. System.out.println(userQueryCondition.toString());
  7. List<User> users=new ArrayList<>();
  8. users.add(new User());
  9. users.add(new User());
  10. users.add(new User());
  11. return users;
  12. }

备注:此处Pageable需要引入如下依赖

  1. <dependency>
  2. <groupId>org.springframework.data</groupId>
  3. <artifactId>spring-data-jpa</artifactId>
  4. </dependency>

springboot中controller的使用的更多相关文章

  1. Q1:spring-boot中Controller路径无法被访问的问题

    在学习spring-boot入门的第一个例子就是spring-boot-web的一个在页面上输出hello-world的例子,在运行这个例子的时候我遇到了下面这个简单的问题,但是第一次解决还是花了我很 ...

  2. springboot 中controller 返回html界面或 jsp界面

    参考链接:https://blog.csdn.net/qq_15260315/article/details/80907056 经尝试,返回html界面没问题,但是返回jsp界面是有问题的,just ...

  3. springboot中Controller没有被扫描

    今天给客户开发登陆的密码加密需求,研究一下想,需要在本地搭一套环境,前台用js实现RAS加密,后台使用java解密.本是一套非常简单的环境,看最近springboot比较常用,所以想要搭一下sprin ...

  4. SpringBoot 中 @RestController 和 @Controller 的区别

    1 - 在springboot中,@RestController 相当于 @Controller + @ResponseBody;2 - 即在Controller类中,若想返回jsp或html页面,则 ...

  5. SpringBoot 中常用注解@Controller/@RestController/@RequestMapping的区别

    SpringBoot中常用注解@Controller/@RestController/@RequestMapping的区别 @Controller 处理http请求 @Controller //@Re ...

  6. SpringBoot 中常用注解@Controller/@RestController/@RequestMapping介绍

    原文 SpringBoot 中常用注解 @Controller/@RestController/@RequestMapping介绍 @Controller 处理http请求 @Controller / ...

  7. springboot中的controller注解没有生效

    springboot中的controller注解没有生效  , 启动的Application类没有在controller的父目录或同级目录

  8. 如何在SpringBoot中使用JSP ?但强烈不推荐,果断改Themeleaf吧

    做WEB项目,一定都用过JSP这个大牌.Spring MVC里面也可以很方便的将JSP与一个View关联起来,使用还是非常方便的.当你从一个传统的Spring MVC项目转入一个Spring Boot ...

  9. springboot中swaggerUI的使用

    demo地址:demo-swagger-springboot springboot中swaggerUI的使用 1.pom文件中添加swagger依赖 2.从github项目中下载swaggerUI 然 ...

随机推荐

  1. 将一个dropdownlist从一个div复制到另一个div

    <select id="dropdwon1"> <option value=">Item1</option> <option v ...

  2. 前往央都之行-gdufe1529

    前往央都之行 Time Limit: 2000/1000ms (Java/Others) Problem Description: 刀光哥桐人和尤吉欧两人为了拯救爱丽丝,同时从卢利特村出发要尽快同时赶 ...

  3. kotlin string

    Kotlin String split 操作实践   内容 此文章展示kotlin中对String字符串的split操作,如果你有遇到这方面的需求,希望对你有用. 1. split + 正则 先看下系 ...

  4. python添加到环境变量

    1.命令 vi ~/.bashrc export PATH="/home/tiany/software/python/python3/3.6.1/bin:$PATH" export ...

  5. IconFont使用指南

    [IconFont使用指南] 为了使用IconFont,需要先建立自己的项目. 在IconFont.cn中寻找自己喜欢的图标,加入到这个新建的项目. IconFont有三种使用方式,其中FontCla ...

  6. Tableau-安装的坑

    前言: 为了学习Tableau的教程,我下载了这个软件从官网,结果安装的时候一直报一个奇怪的错误, 由于当时没有截图,只记得错误代码Xo80076666好像是,提示安装失败,已经有另一个产品安装 在我 ...

  7. Laravel5.1 与 Laypage 结合进行分页

    demo地址:http://lara.ytlwin.top/orm 路由 Route::match(array('get','post'),'/orm','StuController@orm'); 控 ...

  8. thymeleaf 处理模板为字符串

    @Autowired private SpringTemplateEngine thymeleaf; public String aa() { Context context = new Contex ...

  9. PHP 用正则获取URL的根域名

    function GetUrlRoot($url){ preg_match('/[\w][\w-]*\.(?:com\.cn|com|cn|co|net|org|gov|cc|biz|info)(\/ ...

  10. Dbutils 连接数据库

    用了JDBC后接着学习Dbutils连接数据库 1.添加驱动包 Class.forName("oracle.jdbc.driver.OracleDriver"); 2.获取数据库连 ...