欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

SpringBoot之Controller的使用詳解

本文介紹了 SpringBoot之Controller的使用,分享給大家,具體如下:

永善網(wǎng)站制作公司哪家好,找創(chuàng)新互聯(lián)建站!從網(wǎng)頁(yè)設(shè)計(jì)、網(wǎng)站建設(shè)、微信開(kāi)發(fā)、APP開(kāi)發(fā)、響應(yīng)式網(wǎng)站開(kāi)發(fā)等網(wǎng)站項(xiàng)目制作,到程序開(kāi)發(fā),運(yùn)營(yíng)維護(hù)。創(chuàng)新互聯(lián)建站于2013年創(chuàng)立到現(xiàn)在10年的時(shí)間,我們擁有了豐富的建站經(jīng)驗(yàn)和運(yùn)維經(jīng)驗(yàn),來(lái)保證我們的工作的順利進(jìn)行。專注于網(wǎng)站建設(shè)就選創(chuàng)新互聯(lián)建站。

1.@Controller:處理http請(qǐng)求

2.@RestController:Spring4之后新加的注解,原來(lái)返回json需要@ResponseBody配合@Controller

3.@RequestMapping 配置url映射

1.現(xiàn)在有一個(gè)需求(即可以使用localhost:8080/hello和localhost:8080/hi都可以訪問(wèn)):

@RestController
public class HelloController {
  @RequestMapping(value={"/hello","hi"},method = RequestMethod.GET)//使用集合設(shè)置
  public String say(){
    return "Hello Spring Boot";
  }
}

SpringBoot獲取請(qǐng)求參數(shù)

1.@PathVariable–>獲取url中的數(shù)據(jù)

2.@ReqeustParam–>獲取請(qǐng)求參數(shù)的值,可以設(shè)置默認(rèn)值以及是否必傳

3.@GetMapping–>組合注解(相當(dāng)于@RequestMapping同時(shí)限定請(qǐng)求方法為GET 方式)

1.第一種方式:

假如http://localhost:8080/hello為請(qǐng)求,springboot為需要傳遞的參數(shù):http://localhost:8080/hello/spingboot,獲取此種請(qǐng)求的參數(shù)的方式,使用@PathVariable注解

@RestController 
public class HelloController {  
  @RequestMapping("/hello/{params}")//獲取請(qǐng)求為http://localhost:8080/hello/XXX 類型的參數(shù) 
  public String hello(@PathVariable("params") String paramsStr) {//聲明一個(gè)變量接收請(qǐng)求中的參數(shù) 
    return "parameter is "+paramsStr; 
  } 
} 

運(yùn)行程序,輸入http://localhost:8080/hello/spingboot進(jìn)行測(cè)試:

SpringBoot之Controller的使用詳解

2.第二種方式:

獲取請(qǐng)求為http://localhost:8080/hello?params=spingboot類型的參數(shù),使用@RequesParam注解,使用方法為@RequesParam("請(qǐng)求中的參數(shù)名params")

@RestController 
public class HelloController { 
  //獲取請(qǐng)求為http://localhost:8080/hello?xxx=xxx類型的參數(shù) 
  @RequestMapping("/hello") 
  public String hello(@RequestParam("params") String paramsStr) {//requestParam中的參數(shù)名稱與請(qǐng)求中參數(shù)名稱要一致  
    return "parameter is "+paramsStr; 
  } 
} 

如:@RequestParam(value="item_id",required=true) String id

@RequestParam中的其他屬性:

--required:是否必須,默認(rèn)是true,表示請(qǐng)求中一定要有相應(yīng)的參數(shù),否則將報(bào)錯(cuò)

--defaultValue:默認(rèn)值,表示如果請(qǐng)求中沒(méi)有同名參數(shù)時(shí)的默認(rèn)值

啟動(dòng)程序,輸入http://localhost:8080/hello?params=spingboot:

SpringBoot之Controller的使用詳解

對(duì)于@RequestMapping(value="/hello",method = RequestMethod.GET)可以使用:@GetMapping(value="/hello"),如果是Post的話就是用@PostMapping

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。

本文名稱:SpringBoot之Controller的使用詳解
網(wǎng)頁(yè)鏈接:http://chinadenli.net/article6/pgosog.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站維護(hù)、網(wǎng)站設(shè)計(jì)、App開(kāi)發(fā)搜索引擎優(yōu)化、網(wǎng)站收錄商城網(wǎng)站

廣告

聲明:本網(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í)需注明來(lái)源: 創(chuàng)新互聯(lián)

綿陽(yáng)服務(wù)器托管