@RequestMapping註解

來源:互聯網
上載者:User

標籤:eth   ram   pat   ati   isp   map   col   []   content   

1、@RequestMapping可以註解在類上和方法上,當註解在類傷的時候,所有的方法都將映射為相對於類層級的請求

如下代碼

  

1 @RequestMapping(value="/user")2 public class UserController{3     // 該方法映射的請求為http://localhost:8080/user/login4      @RequestMapping("/login")5      public String login() {6          return "login";7      }8 }
View Code

2、@RequestMapping註解說明

屬性 類型  是否必要  說明 
 value String[]  否  用於將制定請求的實際地址映射到方法上 
 name String  否  給映射地址制定一個別名 
 method RequestMethod[]  否  映射指定請求的方法的類型,包括GET,POST,HEAD,OPTIONS,PUT,PATCH,DELETE,TRACE 
 consumes String[]  否  指定處理請求提交的內容類型(Content-Type),例如application/json,text/html等 
 params String[]  否  指定request中必須包含某些參數值時,才讓該方法進行處理 
produces String[] 指定返回的內容類型,返回的內容類型必須是request要求標頭(Accept)中包含的類型
headers String[] 指定request中必須包含某些指定的header值時,才讓該方法進行處理 

 

  • value屬性

  

1 @RequestMapping(value="/register")2      public String registerForm() {3         4          return "registerForm";5      }
View Code

 

URL訪問方式如下:http://localhost:8080/user/register
  • method屬性
 @RequestMapping(value="/register",method=RequestMethod.POST) 同時也支援多個http的請求方式 @RequestMapping(value="/register",method={RequestMethod.POST,RequestMethod.GET})
  • consumes屬性
 @RequestMapping(value="/register",method=RequestMethod.POST,consumes="application/json")該方法標識僅畜類 request Content-Type為“application/json”類型的請求
  • produces屬性
@RequestMapping(value="/register",method=RequestMethod.POST,produces="application/json")該方法標識僅處理 request 要求標頭Accept頭中包含了“application/json”類型的請求,同時也指明了返回的內容的類型為application/json

 











@RequestMapping註解

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.