bl双性强迫侵犯h_国产在线观看人成激情视频_蜜芽188_被诱拐的少孩全彩啪啪漫画

SpringBoot中常用注解是什么-創(chuàng)新互聯(lián)

這篇文章給大家分享的是有關(guān)SpringBoot中常用注解是什么的內(nèi)容。小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過來看看吧。

為陽明等地區(qū)用戶提供了全套網(wǎng)頁設(shè)計(jì)制作服務(wù),及陽明網(wǎng)站建設(shè)行業(yè)解決方案。主營業(yè)務(wù)為成都網(wǎng)站建設(shè)、網(wǎng)站建設(shè)、陽明網(wǎng)站設(shè)計(jì),以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專業(yè)、用心的態(tài)度為用戶提供真誠的服務(wù)。我們深信只要達(dá)到每一位用戶的要求,就會(huì)得到認(rèn)可,從而選擇與我們長期合作。這樣,我們也可以走得更遠(yuǎn)!

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;
  }
}

SpringBoot中常用注解是什么

@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é)果:

SpringBoot中常用注解是什么

當(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

感謝各位的閱讀!關(guān)于“SpringBoot中常用注解是什么”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!

當(dāng)前題目:SpringBoot中常用注解是什么-創(chuàng)新互聯(lián)
轉(zhuǎn)載注明:http://vcdvsql.cn/article16/pjidg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供App開發(fā)商城網(wǎng)站動(dòng)態(tài)網(wǎng)站電子商務(wù)定制網(wǎng)站域名注冊(cè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

小程序開發(fā)