SpringBoot 中常用注解及各種注解作用
本篇文章將介紹幾種SpringBoot 中常用注解
其中,各注解的作用為:
@PathVaribale 獲取url中的數(shù)據(jù)
@RequestParam 獲取請(qǐng)求參數(shù)的值
@GetMapping 組合注解,是@RequestMapping(method = RequestMethod.GET)的縮寫
@RestController是@ResponseBody和@Controller的組合注解。
@PathVaribale 獲取url中的數(shù)據(jù)
看一個(gè)例子,如果我們需要獲取Url=localhost:8080/hello/id中的id值,實(shí)現(xiàn)代碼如下:
@RestController public class HelloController { @RequestMapping(value="/hello/{id}",method= RequestMethod.GET) public String sayHello(@PathVariable("id") Integer id){ return "id:"+id; } }
@RequestParam 獲取請(qǐng)求參數(shù)的值
直接看一個(gè)例子,如下
@RestController public class HelloController { @RequestMapping(value="/hello",method= RequestMethod.GET) public String sayHello(@RequestParam("id") Integer id){ return "id:"+id; } }
在瀏覽器中輸入地址:localhost:8080/hello?id=1000,可以看到如下的結(jié)果:
當(dāng)我們?cè)跒g覽器中輸入地址:localhost:8080/hello?id ,即不輸入id的具體值,此時(shí)返回的結(jié)果為null。具體測(cè)試結(jié)果如下:
@GetMapping 組合注解
@GetMapping是一個(gè)組合注解,是@RequestMapping(method = RequestMethod.GET)
的縮寫。該注解將HTTP Get 映射到 特定的處理方法上。
即可以使用@GetMapping(value = “/hello”)
來代替@RequestMapping(value=”/hello”,method= RequestMethod.GET)
。即可以讓我們精簡(jiǎn)代碼。
例子
@RestController public class HelloController { //@RequestMapping(value="/hello",method= RequestMethod.GET) @GetMapping(value = "/hello") //required=false 表示url中可以不穿入id參數(shù),此時(shí)就使用默認(rèn)參數(shù) public String sayHello(@RequestParam(value="id",required = false,defaultValue = "1") Integer id){ return "id:"+id; } }
@RestController
Spring4之后新加入的注解,原來返回json需要@ResponseBody
和@Controller
配合。
即@RestController
是@ResponseBody
和@Controller
的組合注解。
@RestController public class HelloController { @RequestMapping(value="/hello",method= RequestMethod.GET) public String sayHello(){ return "hello"; } }
與下面的代碼作用一樣
@Controller @ResponseBody public class HelloController { @RequestMapping(value="/hello",method= RequestMethod.GET) public String sayHello(){ return "hello"; } }
注解@RequestParam 和 @PathVarible的區(qū)別
@RequestParam是請(qǐng)求中的參數(shù)。如get?id=1
@PathVarible是請(qǐng)求路徑中的變量如 get/id=1
總結(jié)
以上所述是小編給大家介紹的SpringBoot 中常用注解及各種注解作用,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
Spring Boot 集成Shiro的多realm實(shí)現(xiàn)以及shiro基本入門教程
這篇文章主要介紹了Spring Boot 集成Shiro的多realm實(shí)現(xiàn)以及shiro基本入門,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-10-10Java數(shù)據(jù)結(jié)構(gòu)優(yōu)先隊(duì)列實(shí)練
通常都把隊(duì)列比喻成排隊(duì)買東西,大家都很守秩序,先排隊(duì)的人就先買東西。但是優(yōu)先隊(duì)列有所不同,它不遵循先進(jìn)先出的規(guī)則,而是根據(jù)隊(duì)列中元素的優(yōu)先權(quán),優(yōu)先權(quán)最大的先被取出,這篇文章主要介紹了java優(yōu)先隊(duì)列的真題,感興趣的朋友一起看看吧2022-07-07利用Java發(fā)送郵件的實(shí)現(xiàn)代碼
這篇文章給大家分享了如何利用Java發(fā)送郵件,文章通過實(shí)例代碼介紹的很詳細(xì),有需要的可以參考借鑒。2016-08-08SpringBoot +Vue開發(fā)考試系統(tǒng)的教程
這篇文章主要介紹了SpringBoot +Vue開發(fā)考試系統(tǒng),支持多種題型:選擇題、多選題、判斷題、填空題、綜合題以及數(shù)學(xué)公式。支持在線考試,教師在線批改試卷。本文通過實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2020-05-05SpringMVC中的ResourceUrlProviderExposingInterceptor詳解
這篇文章主要介紹了SpringMVC中的ResourceUrlProviderExposingInterceptor詳解,ResourceUrlProviderExposingInterceptor是Spring MVC的一個(gè)HandlerInterceptor,用于向請(qǐng)求添加一個(gè)屬性,需要的朋友可以參考下2023-12-12詳解SpringBoot如何刪除引用jar包中的無用bean
為了趕速度和直接將之前多模塊的maven項(xiàng)目中的部分模塊,直接以jar包的形式引入到新項(xiàng)目中了,雖然省去了不少開發(fā)時(shí)間,導(dǎo)致項(xiàng)目臃腫,啟動(dòng)很慢。本文將用@ComponentScan注解去實(shí)現(xiàn)讓項(xiàng)目只加載自己需要的bean,需要的可以參考一下2022-06-06SpringBoot集成EasyExcel實(shí)現(xiàn)Excel導(dǎo)入的方法
這篇文章主要介紹了SpringBoot集成EasyExcel實(shí)現(xiàn)Excel導(dǎo)入的方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01微信企業(yè)號(hào)驗(yàn)證/發(fā)送/接收消息
這篇文章主要介紹了微信企業(yè)號(hào)驗(yàn)證/發(fā)送/接收消息的相關(guān)資料,非常不錯(cuò)具有參考借鑒價(jià)值,需要的朋友可以參考下2016-10-10