SpringMVC之請(qǐng)求映射RequestMapping的示例分析-創(chuàng)新互聯(lián)

這篇文章將為大家詳細(xì)講解有關(guān)Spring MVC之請(qǐng)求映射RequestMapping的示例分析,小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。

成都創(chuàng)新互聯(lián)公司-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價(jià)比望花網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫(kù),直接使用。一站式望花網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋望花地區(qū)。費(fèi)用合理售后完善,十多年實(shí)體公司更值得信賴。

RequestMapping注解說(shuō)明

@RequestMapping注解的作用將Web請(qǐng)求映射到特定處理程序類和/或處理程序方法,這個(gè)注解可以用于類或者方法上,并通過屬性value指定請(qǐng)求路徑。用在Controller類上表示提供初步的URL請(qǐng)求映射信息,相對(duì)于Web應(yīng)用的根目錄,這是一個(gè)前置請(qǐng)求路徑。用在Controller中方法上,表示提供詳細(xì)的URL映射。如果Controller類上沒有加RequestMapping注解,則方法上注解標(biāo)記的URL則是相對(duì)于Web應(yīng)用的根目錄。

@RequestMapping注解提供以下幾個(gè)屬性:

         name:用于指定映射器名稱

         value:用于指定映射路徑,同path

         path:用于指定映射路徑,同value

         method:用于指定請(qǐng)求類型:GET, POST, HEAD, OPTIONS, PUT, PATCH, DELETE, TRACE

         params:指定請(qǐng)求的參數(shù)

         headers:指定請(qǐng)求頭部,源碼示例:RequestMapping(value = "/something", headers = "content-type=text/*")

         consumes:指定處理請(qǐng)求提交的內(nèi)容類型(Content-Type),例如application/json, text/html,只有在Content-Type匹配這些媒體類型之一時(shí)才會(huì)映射請(qǐng)求

         produces:指定請(qǐng)求返回的內(nèi)容類型 例如:produces = "application/json; charset=UTF-8"

通過value屬性指定映射路徑

Controller類上使用RequestMapping注解

@Controller
@RequestMapping("order") 
public class OrderInfoController {  
  //示例1
  @RequestMapping("orderinfo")  
  public ModelAndView OrderInfo1() {
    return new ModelAndView("order/info", "message", "OrderInfo");
  }
}

在OrderController類上添加了注解RequestMapping("order"),表示所有對(duì)的請(qǐng)求必須是以“根目錄/order” 開始

示例1的請(qǐng)求路徑為:http://localhost:8080/springMvcNext/order/orderinfo

示例1 如果注釋掉Controller上的@RequestMapping("order"),則對(duì)應(yīng)的請(qǐng)求路徑為:http://localhost:8080/springMvcNext /orderinfo

Controller方法上使用RequestMapping注解

 1.常用基礎(chǔ)用法

@Controller
@RequestMapping("order") 
public class OrderInfoController {
  //示例1
  @RequestMapping("orderinfo")  
  public ModelAndView OrderInfo1() {
    return new ModelAndView("order/info", "message", "OrderInfo");
  }
  //示例2 :處理多個(gè)url映射
  @RequestMapping({"info","index"}) //或者@RequestMapping(value={"info","index"})
  public ModelAndView OrderInfo2() {
    return new ModelAndView("order/info","message", "OrderInfo2");
  }               
  //示例3
  @RequestMapping
  public ModelAndView OrderInfo3() {  
    return new ModelAndView("order/info","message", "OrderInfo3");
  }
}

RequestMapping只配置value屬性,不顯示配置其他屬性的情況下,value省略,直接填寫URL映射信息即可,指定其他屬性的情況下value屬性必須明確填寫

上例示例1的訪問路徑為: http://localhost:8080/springMvcNext/order/orderinfo

示例2:RequestMapping接口中value屬性是一個(gè)數(shù)組,所有也支持傳一個(gè)數(shù)組 示例2的訪問路徑:http://localhost:8080/springMvcNext/order/index  或者 http://localhost:8080/springMvcNext/order/info

示例3:當(dāng)value為空時(shí),表示該方法為類下默認(rèn)的Action,示例3的訪問路徑為:http://localhost:8080/springMvcNext/order

 2.URL模板映射

在RequestMapping注解中聲明URI變量,并通過@PathVariable注解的方式訪從實(shí)際請(qǐng)求URL中獲取值,示例如下:

@Controller
public class OrderInfoController {
   // 示例10 帶占位符的URL
   @RequestMapping(value = "user/{userId}/order/{orderNumber}", method = RequestMethod.GET)
   public ModelAndView OrderInfo4(@PathVariable int userId,@PathVariable String orderNumber) {
      return new ModelAndView("order/info", "message", "userid:"+userId+" orderNumber:"+orderNumber);
   }
}

示例10請(qǐng)求URL:  http://localhost:8080/springMvcNext/user/12/order/333 當(dāng)通過此URL發(fā)起請(qǐng)求時(shí),SpringMVC將通過@PathVariable可以提取URL模板中的{×××}中的×××變量, URL變量會(huì)自動(dòng)轉(zhuǎn)換為對(duì)應(yīng)的類型,無(wú)法轉(zhuǎn)換的則返回錯(cuò)誤,比如嘗試用以下url訪問:http://localhost:8080/springMvcNext/user/xxx/order/333  其中參數(shù)Userid=xxx,則發(fā)生錯(cuò)誤:

Spring MVC之請(qǐng)求映射RequestMapping的示例分析

3.Ant風(fēng)格的URL路徑映射

 Ant風(fēng)格通配符如下:

  • ?  匹配一個(gè)字符

  • *    匹配路徑段中的零個(gè)或多個(gè)字符

  • **  匹配零個(gè)或多個(gè)路徑段

 示例:

@Controller
public class OrderInfoController {
    // 示例11 帶占位符的URL
    @RequestMapping(value = "order*", method = RequestMethod.GET)
    //@RequestMapping(value = "order?", method = RequestMethod.GET)
    //@RequestMapping(value = "order/**", method = RequestMethod.GET)
    public ModelAndView OrderInfo5(String orderNumber) {
       return new ModelAndView("order/info", "message", "OrderInfo5");
    }
}

示例11請(qǐng)求URL:  http://localhost:8080/springMvcNext/order/orderdexx?orderNumber=12 可以匹配http://localhost:8080/springMvcNext/order/orderXXXXX?orderNumber=yyyy的所有請(qǐng)求

@RequestMapping(value = "order?", method = RequestMethod.GET)可以匹配諸如 “…/ordera?orderNumber….” “…/orders?orderNumber….”

 @RequestMapping(value = "order/**", method = RequestMethod.GET)可以匹配諸如 “…/order/aaa?orderNumber….” “…/order/bbb/ccc?orderNumber….”

另外 RequestMapping還支持正則表達(dá)式風(fēng)格的URL路徑映射,此處略過

通過method屬性指定請(qǐng)求類型

RequestMapping提供的method屬性請(qǐng)求謂詞的類型,如下示例示例只接受GET請(qǐng)求

  // 示例4
  @RequestMapping(value="detail",method=RequestMethod.GET) //也可直接使用 @GetMapping("detail")
  public ModelAndView Info() {
    return new ModelAndView("order/info", "message", "Info");
  }

對(duì)于每種請(qǐng)求類型,SpringMVC還提供了專用的注解:

@GetMapping

@PostMapping

@PutMapping

@DeleteMapping

@PatchMapping

通過params指定參數(shù)名或參數(shù)值約束

params屬性可以限定請(qǐng)求參數(shù)包含特定的參數(shù),也可限定參數(shù)值的約束,如下代碼所示:

 // 示例5 params 限定參數(shù)包含orderNumber
  @RequestMapping(value = "detail2", params = "orderNumber")
  public ModelAndView Detail2(String orderNumber) {
    return new ModelAndView("order/info", "message", orderNumber);
  }
  // 示例6 params 限定參數(shù)值
  @RequestMapping(value = "detail3", params = "orderNumber!=1222")
  public ModelAndView Detail3(String orderNumber) {
    return new ModelAndView("order/info", "message", orderNumber);
  }

示例5限定請(qǐng)求參數(shù)必須包含參數(shù)orderNumber,如果不包含名為orderNumber的參數(shù),則拒絕訪問:訪問路徑:http://localhost:8080/springMvcNext/order/detail2?orderNumber=12

示例6限定請(qǐng)求參數(shù)必須包含參數(shù)orderNumber并且參數(shù)值不能為1222 訪問路徑:http://localhost:8080/springMvcNext/order/detail3?orderNumber=1222 時(shí)報(bào)錯(cuò)

Spring MVC之請(qǐng)求映射RequestMapping的示例分析

通過headers指定參數(shù)名或參數(shù)值約束

 RequestMapping提供的method屬性可以指定請(qǐng)求頭類型,只有請(qǐng)求數(shù)據(jù)頭部類型符合指定的值時(shí),才能正常訪問

// 示例7 params 限定參數(shù)值
    @RequestMapping(value = "headtest",headers = "apikey=23131313") 
    //@RequestMapping(value = "headtest",headers= {"Accept=application/json"}) 
    public ModelAndView Header() {
      return new ModelAndView("order/info", "message", "Header");
    }

示例7限定請(qǐng)求頭必須包含apikey:23131313才可以正常返回,直接訪問,返回錯(cuò)誤:

Spring MVC之請(qǐng)求映射RequestMapping的示例分析

添加添加header信息apikey:23131313訪問成功:

Spring MVC之請(qǐng)求映射RequestMapping的示例分析

通過consumes指定請(qǐng)求提交的內(nèi)容類型(Content-Type)

  // 示例8 consumes 
  @RequestMapping(value = "consumes", method = RequestMethod.POST, consumes = "application/json")
  public ModelAndView Consumes(String orderNumber) {
    return new ModelAndView("order/info", "message", orderNumber);
  }

示例限定請(qǐng)求參數(shù)類型為application/json,表示該方法只處理請(qǐng)求Content-Type為application/json的請(qǐng)求:

下面通過拋postman測(cè)試:

設(shè)置請(qǐng)求參數(shù)格式為application/json,可以正常訪問:

Spring MVC之請(qǐng)求映射RequestMapping的示例分析

設(shè)置參數(shù)格式為x-form-urlencoded,返回錯(cuò)誤,Http Status 415

Spring MVC之請(qǐng)求映射RequestMapping的示例分析

通過produces指定返回的內(nèi)容類型(Content-Type)

 produces屬性用于設(shè)定返回內(nèi)容類型,并且滿足以下條件:接受請(qǐng)求header中包含Accept的值與produces設(shè)定的值相同,或者接受的請(qǐng)求使用不顯示設(shè)置accept值

 // 示例8 produces 限定返回?cái)?shù)據(jù)application/json
    @RequestMapping(value = "produces", method = RequestMethod.GET, produces = "application/json")
    public ModelAndView Produces(String orderNumber) {
      return new ModelAndView("order/info", "message", orderNumber);
    }

示例8 表示返回內(nèi)容格式application/json ,當(dāng)客戶端設(shè)置的accept格式為text/json時(shí),運(yùn)行報(bào)錯(cuò),Http status 406

Spring MVC之請(qǐng)求映射RequestMapping的示例分析

當(dāng)客戶端設(shè)置的accept格式為application/json或者不設(shè)置accept值時(shí),可以正常運(yùn)行

Spring MVC之請(qǐng)求映射RequestMapping的示例分析

關(guān)于“Spring MVC之請(qǐng)求映射RequestMapping的示例分析”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。

新聞標(biāo)題:SpringMVC之請(qǐng)求映射RequestMapping的示例分析-創(chuàng)新互聯(lián)
分享URL:http://muchs.cn/article22/coeicc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供面包屑導(dǎo)航外貿(mào)網(wǎng)站建設(shè)、動(dòng)態(tài)網(wǎng)站、搜索引擎優(yōu)化、企業(yè)網(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)

外貿(mào)網(wǎng)站建設(shè)