麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁(yè) > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

Spring Boot學(xué)習(xí)筆記-常用注解總結(jié)

2019-11-06 08:16:31
字體:
供稿:網(wǎng)友

寫在前面

  本來,是不想寫這篇博客的,一是因?yàn)閼校且驗(yàn)橹皩W(xué)習(xí)過SPRing MVC,而Spring Boot中的大部分常用注解其實(shí)都是Spring MVC中的注解。不過,為了以后方便自己查閱和養(yǎng)成寫博客的習(xí)慣,我覺得我還是記錄下來吧。


@Controller

  主要處理HTTP請(qǐng)求,Spring會(huì)將接收到的HTTP請(qǐng)求交給被@Controller所標(biāo)記的類。現(xiàn)在強(qiáng)調(diào)前后臺(tái)分離,所以,該注解現(xiàn)在主要與@ResponseBody配合使用來返回json數(shù)據(jù)。

import org.springframework.stereotype.Controller; @Controller public class HelloController { }

@ResponseBody

作用:   該注解用于將Controller的方法返回的對(duì)象,通過適當(dāng)?shù)腍ttpMessageConverter轉(zhuǎn)換為指定格式后,寫入到Response對(duì)象的body數(shù)據(jù)區(qū)。 使用時(shí)機(jī):   返回的數(shù)據(jù)不是html標(biāo)簽的頁(yè)面,而是其他某種格式的數(shù)據(jù)時(shí)(如json、xml等)使用;

  一般與@Controller配合使用來返回json數(shù)據(jù)。

import org.springframework.beans.factory.annotation.Autowired; 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; @Controller public class HelloController { @Autowired //自動(dòng)注入 private Person person; @ResponseBody @RequestMapping(value = {"/hello", "/hi"}, method = RequestMethod.GET) public Person hello() { return person; } }

@RestController

  該注解是Spring4之后新加的注解,等同于@Controller@ResponseBody的組合。

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; @RestController public class HelloController { @Autowired private Person person; @RequestMapping(value = {"/hello", "/hi"}, method = RequestMethod.GET) public Person hello() { return person; } }

URL映射

  每個(gè)Controller類,接收到HTTP請(qǐng)求,會(huì)通過請(qǐng)求的URL和請(qǐng)求方法(GET,POST…)來映射使用哪個(gè)控制器方法來執(zhí)行請(qǐng)求。這種映射主要通過@RequestMapping注解來實(shí)現(xiàn)。

單個(gè)URL映射

  一個(gè)控制器方法對(duì)應(yīng)一個(gè)URL,注解@RequestMapping中,value的值即為所映射的URL。

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 { /** * 啟動(dòng)應(yīng)用,瀏覽器打開http://localhost:8080/hello,會(huì)調(diào)用該方法,打印:Hello, Spring Boot. * @return */ @RequestMapping(value = "/hello", method = RequestMethod.GET) public String helloGet() { return "Hello, Spring Boot"; } }
不同方法的映射

  同一個(gè)URL,請(qǐng)求方法不同,也能對(duì)應(yīng)不同的控制器方法。

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 { /** * 啟動(dòng)應(yīng)用,瀏覽器打開http://localhost:8080/hello,會(huì)調(diào)用該方法,打印:Hello, Spring Boot.Request:GET. * @return */ @RequestMapping(value = "/hello", method = RequestMethod.GET) public String helloGet() { return "Hello, Spring Boot.Request:GET"; } /** * 啟動(dòng)應(yīng)用后,通過模擬Http軟件,以post方式請(qǐng)求http://localhost:8080/hello,會(huì)調(diào)用該方法,打印:Hello, Spring Boot.Request:POST. * HTTP模擬軟件推薦postman。多平臺(tái)支持。 * @return */ @RequestMapping(value = "/hello", method = RequestMethod.POST) public String helloPost() { return "Hello, Spring Boot.Request:POST"; } }
映射的簡(jiǎn)寫

  針對(duì)不同的請(qǐng)求方法,Spring都提供了它的簡(jiǎn)寫方式,如@GetMapping@PostMapping。下面的代碼與上面的代碼實(shí)現(xiàn)的效果相同。

import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloController { @GetMapping("/hello") //等同于@RequestMapping(value = "/hello", method = RequestMethod.GET) public String helloGet() { return "Hello, Spring Boot.Request.GET"; } @PostMapping("hello") //等同于@RequestMapping(value = "/hello", method = RequestMethod.POST) public String helloPost() { return "Hello, Spring Boot.Request:POST"; } }

多個(gè)URL映射

  一個(gè)控制器方法也可以對(duì)應(yīng)多個(gè)URL,即value的值可以對(duì)應(yīng)一個(gè)URL的集合。

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 { /** * 此時(shí)value對(duì)應(yīng)兩個(gè)URL,訪問/hello和/hi是一樣的效果 * @return */ @RequestMapping(value = {"/hello", "/hi"}, method = RequestMethod.GET) public String helloGet() { return "Hello, Spring Boot."; } }

類級(jí)URL映射

  @RequestMapping不只可以在方法上使用,也可以在一個(gè)控制器類上使用。在一個(gè)控制器類上使用該注解,那么類里的其它注解的URL,需要與該注解的URL相加進(jìn)行訪問才可以。代碼如下:

import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/howieli") //類級(jí)URL映射 public class HelloController { /** * 此時(shí)訪問該方法需要訪問/howieli/hello或者/howieli/hi * @return */ @RequestMapping(value = {"/hello", "/hi"}, method = RequestMethod.GET) public String helloGet() { return "Hello, Spring Boot."; } }

URL數(shù)據(jù)獲取

  在實(shí)際開發(fā)中,參數(shù)肯定是不可或缺的一部分,那么,在控制器中應(yīng)該怎么獲取呢?我們需要知道,在實(shí)際開發(fā)中,傳遞的數(shù)據(jù)主要是分為兩種的,第一種的直接通過URL傳遞,比如:http://localhost:8080/say/3,其中3為所傳遞數(shù)據(jù)。還有一種是通過參數(shù)傳遞,比如GET的傳參方式:http://localhost:8080/say?id=3。   針對(duì)第一種情況,Spring提供了@PathVariable注解,針對(duì)第二種情況,Spring也提供了@RequestParam注解。

import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloController { /** * 在URL映射的注解中,我們可以看到URL中被大括號(hào)括起來的id,這就代表我們要傳遞的數(shù)據(jù) * 在helloGet方法傳入?yún)?shù)時(shí)使用@PathVariable注解,表示將URL中的id所代表的數(shù)據(jù)作為參數(shù)傳入方法中 * name這個(gè)參數(shù),是通過參數(shù)的方式傳入,此時(shí)可以使用@RequestParam注解,其實(shí)在這里注解可以省略不寫,因?yàn)槲覀冏兞棵且粯拥? * 比如我們參數(shù)是?name=howieli中的name與方法參數(shù)name是相同的,就可以省略注解。 * @param id * @return */ @GetMapping(value = "/say/{id}") public String helloGet(@PathVariable("id") int id, @RequestParam("name") String name) { return "id: " + id + ",name:" + name; } }

  啟動(dòng)應(yīng)用,訪問http://localhost:8080/say/5?name=howieli,即可打印id: 5,name:howieli。   這個(gè)部分寫的有點(diǎn)亂。


結(jié)語

  這只是一些比較常用的注解,之后碰到其它重要注解會(huì)慢慢補(bǔ)充。   個(gè)人博客:https://www.howieli.cn 和個(gè)人CSDN博客: http://blog.csdn.net/howieli_1995。


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 蜜桃视频在线免费播放 | 久久精品艹 | 久久精品久久精品久久精品 | 九九色网站 | 免费看污视频在线观看 | 超级av在线 | 护士hd老师fre0性xxx | 操你啦免费视频 | 欧美毛片在线观看 | 韩国19禁在线 | 国产精品午夜小视频观看 | 国产一及毛片 | 国产1区在线观看 | 一级成人欧美一区在线观看 | 亚洲日本高清 | 欧美自拍| 久久精热 | 久久久久久久亚洲精品 | av电影在线观看网址 | 特片网久久 | 久久精品国产99久久久古代 | 日本黄色免费片 | 蜜桃精品视频 | 在线观看视频亚洲 | 成年性羞羞视频免费观看 | 精品中文一区 | 91精品国产日韩91久久久久久360 | 97黄色网| 极色品影院 | 蜜桃网在线观看 | 农村寡妇偷毛片一级 | 日韩视频一区二区三区在线观看 | 红杏网站永久免费视频入口 | 成人做爽爽爽爽免费国产软件 | 午夜啪视频 | 久久久成人精品视频 | 成人激情综合网 | 亚洲天堂ww | 亚洲特黄a级毛片在线播放 久久久入口 | 色戒在线版 | 国产成人在线免费视频 |