一、請求及路徑映射部分注解介紹
注解名稱 | 描述 |
@Controller | 處理http請求 |
@RestController | Spring4之后新加的注解,原來返回json,需要@ResponseBody配合@Controller |
@RequestMapping | 配置url映射 |
1、@Controller的使用(了解)
相當於serverlet的jsp,前后端不分離的開發,就模板而言很好性能,所以提倡前后端分離式的開發,這里我就不啰嗦了。
接着再來演示下,這個注解的使用,必須配合模板來使用,首先在pom文件中添加如下依賴:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
說明:這里的spring boot版本必須為1.4.1,否則使用模板時,不顯示頁面,報錯404
<parent>
<groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.1.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent>
在src/main/resources/templates/下,
創建一個名為index的html文件,寫入內容如下:
<h1>hello,Spring boot!</h1>
編寫Controller,示例如下:
package com.rongrong.springboot.demo; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.*; /** * @author rongrong * @version 1.0 * @description: * @date 2019/12/28 21:32 */ @Controller public class HtmlController { @RequestMapping(value = "/say",method = RequestMethod.GET) public String index(){ return "index"; } }
啟動項目,訪問頁面:http://localhost:8888/say,顯示結果如下:
2、@RestController的使用
@RestController在實際開發中應用廣泛,與@controller相比,不依賴於模板,前后端分離式的開發,開發效率也很高。
3、@RequestMapping的使用
該注解既可以在類上方使用,又可以方法上使用,在類上方使用,如在該處添加,接口訪問時采取路徑拼接方式訪問,如:localhost:8888/spring/hellow
示例代碼如下:
package com.rongrong.springboot.demo; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; /** * @author rongrong * @version 1.0 * @description: * @date 2019/12/26 20:34 */ @RestController /** * 在類上方使用,如在該處添加,接口訪問時采取路徑拼接方式訪問 * 如:localhost:8888/spring/hellow */ @RequestMapping("/spring") public class HellowController { @Autowired Person person; //在方法 上方使用 @RequestMapping(value = "/hellow",method = RequestMethod.GET) public String say(){ return person.getGName(); } }
訪問接口地址:http://localhost:8888/spring/hellow,具體效果如下:
二、請求參數及組合注解介紹
注解名稱 | 描述 |
@PathVariable | 獲取url中的數據 |
@RequestParam | 獲取請求參數的值 |
@GetMapping | 組合注解 |
1、@PathVariable的使用
一般在restful風格接口使用居多,形式為:/路徑/{變量}
具體示例如下:
//Restful風格接口 @RequestMapping(value = "/getID/{id}", method = RequestMethod.GET) public String getID(@PathVariable("id") Integer id) { return "Id:"+id; }
訪問示例:http://localhost:8888/spring/getID/1,效果如下:
2、@RequestParam使用
為傳統風格形式使用,具體示例如下:
/*** * 傳統方式接口 * @RequestParam(value = "id",required = false,defaultValue = "0") * value為傳入字段名,required為true時必須傳參,defaultValue為傳入參數默認值 * @param id * @return */ @RequestMapping(value = "/getMyId", method = RequestMethod.GET) public String getMyId(@RequestParam(value = "id",required = false,defaultValue = "0") Integer id) { return "getMyId: \t"+id; }
訪問示例:http://localhost:8888/spring/getMyId?id=1,效果如下:
3、組合注解及多個路徑可以訪問同一個接口
@GetMapping可以理解為@RequestMapping,具體示例如下:
/** * 組合注解及多個路徑可以訪問同一個接口 * @param id * @return */ @GetMapping({"/getUserId","/sayhi"}) //@RequestMapping(value = "/getMyId", method = RequestMethod.GET) public String getUserId(@RequestParam(value = "id",required = false,defaultValue = "0") Integer id) { return "getMyId: \t"+id; }
訪問示例:
http://localhost:8888/spring/getUserId?id=1,
http://localhost:8888/spring/sayhi?id=1
效果如下: