springboot快速入門(三)——Controller的使用


一、概述

  controller的分類:

  

  相關的使用方式和springMVC的類似了,細節不再贅述

二、Controller使用

  1.使用@controller注解 

@Controller public class HelloController { @RequestMapping(value = "/hello", method = RequestMethod.GET) public String hello() { return "Hi"; } }

  直接使用會報一個錯:

  

  原因是缺少了模板的渲染,springboot支持的模板如下:

  

  • Thymeleaf
  • FreeMarker
  • Velocity
  • Groovy
  • Mustache

  // 應當避免使用JSP,不然會喪失很多springboot特性!

  2.Thymeleaf模板的使用

    使用官方推薦的這個模板,先引入依賴:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

    // 關於模板的詳細使用,將會另開隨筆介紹,這里不再贅述

  模板的默認位置是生成的:src/main/resources/templates

  我們在此位置下新建一個HTML文件:index.html:

  

  在代碼中返回進行視圖渲染:

@Controller public class HelloController { @RequestMapping(value = "/hello", method = RequestMethod.GET) public String hello() { return "index"; } }

  重新訪問:

  

  // 和web之前的struts2類似,不過由於thymeleaf是HTML模板,故直接根據文件名.html映射

  再者由於現在很多都是前后端分離了,使用模板也可能帶來性能上的損耗,所以這里暫時不進行深入

  3.@RestController的使用

    這里就不再贅述了,就是相當於之前的@Controller加上@ResponseBody的組合

@RestController public class HelloController { @RequestMapping(value = "/hello", method = RequestMethod.GET) public String hello() { return "index"; } }

   直接返回了字符串,而不進行視圖解析

    

 

 

     4.@RequestMapping的使用

   使用一個進行映射的場景上面已經有示例,這里介紹其他的特性:

    多個url映射

@RequestMapping(value = {"/hello", "/hi"}, method = RequestMethod.GET) public String hello() { return "index"; }

    在類上使用,用於窄化映射

@RestController @RequestMapping(value = "/say") public class HelloController { @RequestMapping(value = {"/hello", "/hi"}, method = RequestMethod.GET) public String hello() { return "index"; } }

  

  當然,其實這些映射關系在日志中都會顯示,必要時可以進行查看

  

 

    request的訪問方式:

   

  // 測試POST可以通過postman進行

 三、請求參數的使用

  基本上和springMVC是一致的:

  

  1.@PathVariable注解

@RestController public class HelloController { @RequestMapping(value = "/hello/{name}", method = RequestMethod.GET) public String hello(@PathVariable("name") String name) { return "your name is" + name; } }

  使用postman測試(現在postman的chrome插件已經放棄更新了,使用插件形式可以在桌面創建它的快捷方式啟動,當然也可以下載獨立軟件)

  

  2.@RequestParam注解

    這個就是傳統的?=xxx形式的了:

@RestController public class HelloController { @RequestMapping(value = "/hello", method = RequestMethod.GET) public String hello(@RequestParam("name") String name) { return "your name is:" + name; } }

  

  當然,還可以有一些其他的常用特性,例如是否必須、給出默認值:

@RestController public class HelloController { @RequestMapping(value = "/hello", method = RequestMethod.GET) public String hello(@RequestParam(value = "name", required = false, defaultValue = "jiangbei") String name) { return "your name is:" + name; } }

  3.GetMapping形式的組合注解

    其實也就是組合了method=RequestMethod.GET,進行了簡化,相應的還有配套的PostMapping等!

@RestController public class HelloController { // @RequestMapping(value = "/hello", method = RequestMethod.GET)
    @GetMapping(value = "/hello") public String hello(@RequestParam(value = "name", required = false, defaultValue = "jiangbei") String name) { return "your name is:" + name; } }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM