日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

java常用注解及功能_SpringBoot 中常用注解及各种注解作用

發布時間:2025/3/20 javascript 16 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java常用注解及功能_SpringBoot 中常用注解及各种注解作用 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

本篇文章將介紹幾種SpringBoot 中常用注解

其中,各注解的作用為:

@PathVaribale 獲取url中的數據

@RequestParam 獲取請求參數的值

@GetMapping 組合注解,是@RequestMapping(method = RequestMethod.GET)的縮寫

@RestController是@ResponseBody和@Controller的組合注解。

@PathVaribale 獲取url中的數據

看一個例子,如果我們需要獲取Url=localhost:8080/hello/id中的id值,實現代碼如下:

@RestController

public class HelloController {

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

public String sayHello(@PathVariable("id") Integer id){

return "id:"+id;

}

}

@RequestParam 獲取請求參數的值

直接看一個例子,如下

@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,可以看到如下的結果:

當我們在瀏覽器中輸入地址:localhost:8080/hello?id ,即不輸入id的具體值,此時返回的結果為null。具體測試結果如下:

@GetMapping 組合注解

@GetMapping是一個組合注解,是@RequestMapping(method = RequestMethod.GET)的縮寫。該注解將HTTP Get 映射到 特定的處理方法上。

即可以使用@GetMapping(value = “/hello”)來代替@RequestMapping(value=”/hello”,method= RequestMethod.GET)。即可以讓我們精簡代碼。

例子

@RestController

public class HelloController {

//@RequestMapping(value="/hello",method= RequestMethod.GET)

@GetMapping(value = "/hello")

//required=false 表示url中可以不穿入id參數,此時就使用默認參數

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的區別

@RequestParam是請求中的參數。如get?id=1

@PathVarible是請求路徑中的變量如 get/id=1

總結

以上所述是小編給大家介紹的SpringBoot 中常用注解及各種注解作用,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網站的支持!

總結

以上是生活随笔為你收集整理的java常用注解及功能_SpringBoot 中常用注解及各种注解作用的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。