Spring MVC之@RequestMapping注解詳解
引言:
前段時(shí)間項(xiàng)目中用到了REST風(fēng)格來開發(fā)程序,但是當(dāng)用POST、PUT模式提交數(shù)據(jù)時(shí),發(fā)現(xiàn)服務(wù)器端接受不到提交的數(shù)據(jù)(服務(wù)器端參數(shù)綁定沒有加任何注解),查看了提交方式為application/json, 而且服務(wù)器端通過request.getReader() 打出的數(shù)據(jù)里確實(shí)存在瀏覽器提交的數(shù)據(jù)。為了找出原因,便對(duì)參數(shù)綁定(@RequestParam、 @RequestBody、 @RequestHeader 、 @PathVariable)進(jìn)行了研究,同時(shí)也看了一下HttpMessageConverter的相關(guān)內(nèi)容,在此一并總結(jié)。
簡(jiǎn)介:
@RequestMapping
RequestMapping是一個(gè)用來處理請(qǐng)求地址映射的注解,可用于類或方法上。用于類上,表示類中的所有響應(yīng)請(qǐng)求的方法都是以該地址作為父路徑。
RequestMapping注解有六個(gè)屬性,下面我們把她分成三類進(jìn)行說明。
1、 value, method;
value: 指定請(qǐng)求的實(shí)際地址,指定的地址可以是URI Template 模式(后面將會(huì)說明);
method: 指定請(qǐng)求的method類型, GET、POST、PUT、DELETE等;
2、 consumes,produces;
consumes: 指定處理請(qǐng)求的提交內(nèi)容類型(Content-Type),例如application/json, text/html;
produces: 指定返回的內(nèi)容類型,僅當(dāng)request請(qǐng)求頭中的(Accept)類型中包含該指定類型才返回;
3、 params,headers;
params: 指定request中必須包含某些參數(shù)值是,才讓該方法處理。
headers: 指定request中必須包含某些指定的header值,才能讓該方法處理請(qǐng)求。
示例:
1、value / method 示例
默認(rèn)RequestMapping("....str...")即為value的值;
@Controller @RequestMapping("/appointments") public class AppointmentsController { private final AppointmentBook appointmentBook; @Autowired public AppointmentsController(AppointmentBook appointmentBook) { this.appointmentBook = appointmentBook; } @RequestMapping(method = RequestMethod.GET) public Map<String, Appointment> get() { return appointmentBook.getAppointmentsForToday(); } @RequestMapping(value="/{day}", method = RequestMethod.GET) public Map<String, Appointment> getForDay(@PathVariable @DateTimeFormat(iso=ISO.DATE) Date day, Model model) { return appointmentBook.getAppointmentsForDay(day); } @RequestMapping(value="/new", method = RequestMethod.GET) public AppointmentForm getNewForm() { return new AppointmentForm(); } @RequestMapping(method = RequestMethod.POST) public String add(@Valid AppointmentForm appointment, BindingResult result) { if (result.hasErrors()) { return "appointments/new"; } appointmentBook.addAppointment(appointment); return "redirect:/appointments"; } }
value的uri值為以下三類:
A) 可以指定為普通的具體值;
B) 可以指定為含有某變量的一類值(URI Template Patterns with Path Variables);
C) 可以指定為含正則表達(dá)式的一類值( URI Template Patterns with Regular Expressions);
example B)
@RequestMapping(value="/owners/{ownerId}", method=RequestMethod.GET) public String findOwner(@PathVariable String ownerId, Model model) { Owner owner = ownerService.findOwner(ownerId); model.addAttribute("owner", owner); return "displayOwner"; }
example C)
@RequestMapping("/spring-web/{symbolicName:[a-z-]+}-{version:\d\.\d\.\d}.{extension:\.[a-z]}") public void handle(@PathVariable String version, @PathVariable String extension) { // ... } }
2 consumes、produces 示例
cousumes的樣例:
@Controller @RequestMapping(value = "/pets", method = RequestMethod.POST, consumes="application/json") public void addPet(@RequestBody Pet pet, Model model) { // implementation omitted }
方法僅處理request Content-Type為“application/json”類型的請(qǐng)求。
produces的樣例:
@Controller @RequestMapping(value = "/pets/{petId}", method = RequestMethod.GET, produces="application/json") @ResponseBody public Pet getPet(@PathVariable String petId, Model model) { // implementation omitted }
方法僅處理request請(qǐng)求中Accept頭中包含了"application/json"的請(qǐng)求,同時(shí)暗示了返回的內(nèi)容類型為application/json;
3 params、headers 示例
params的樣例:
@Controller @RequestMapping("/owners/{ownerId}") public class RelativePathUriTemplateController { @RequestMapping(value = "/pets/{petId}", method = RequestMethod.GET, params="myParam=myValue") public void findPet(@PathVariable String ownerId, @PathVariable String petId, Model model) { // implementation omitted } }
僅處理請(qǐng)求中包含了名為“myParam”,值為“myValue”的請(qǐng)求;
headers的樣例:
@Controller @RequestMapping("/owners/{ownerId}") public class RelativePathUriTemplateController { @RequestMapping(value = "/pets", method = RequestMethod.GET, headers="Referer=http://www.ifeng.com/") public void findPet(@PathVariable String ownerId, @PathVariable String petId, Model model) { // implementation omitted } }
僅處理request的header中包含了指定“Refer”請(qǐng)求頭和對(duì)應(yīng)值為“http://www.ifeng.com/”的請(qǐng)求;
上面僅僅介紹了,RequestMapping指定的方法處理哪些請(qǐng)求,下面一篇將講解怎樣處理request提交的數(shù)據(jù)(數(shù)據(jù)綁定)和返回的數(shù)據(jù)。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
使用Jenkins自動(dòng)化構(gòu)建工具進(jìn)行敏捷開發(fā)
這篇文章主要為大家介紹了使用Jenkins自動(dòng)化構(gòu)建工具進(jìn)行敏捷開發(fā),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步早日升職加薪2022-04-04Java并發(fā)編程中的ConcurrentLinkedQueue詳解
這篇文章主要介紹了Java并發(fā)編程中的ConcurrentLinkedQueue詳解,GetThread線程不會(huì)因?yàn)镃oncurrentLinkedQueue隊(duì)列為空而等待,而是直接返回null,所以當(dāng)實(shí)現(xiàn)隊(duì)列不空時(shí),等待時(shí),則需要用戶自己實(shí)現(xiàn)等待邏輯,需要的朋友可以參考下2023-12-12Springboot集成JUnit5優(yōu)雅進(jìn)行單元測(cè)試的示例
這篇文章主要介紹了Springboot集成JUnit5優(yōu)雅進(jìn)行單元測(cè)試的示例,幫助大家更好的理解和使用springboot框架,感興趣的朋友可以了解下2020-10-10解決swaggerUI頁面沒有顯示Controller方法的坑
這篇文章主要介紹了解決swaggerUI頁面沒有顯示Controller方法的坑,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06SpringSecurity實(shí)現(xiàn)自定義登錄方式
本文介紹自定義登錄流程,包括自定義AuthenticationToken、AuthenticationFilter、AuthenticationProvider以及SecurityConfig配置類,詳細(xì)解析了認(rèn)證流程的實(shí)現(xiàn),為開發(fā)人員提供了具體的實(shí)施指導(dǎo)和參考2024-09-09Java 多態(tài)中繼承的轉(zhuǎn)型詳解與用法分析
繼承是java面向?qū)ο缶幊碳夹g(shù)的一塊基石,因?yàn)樗试S創(chuàng)建分等級(jí)層次的類。繼承就是子類繼承父類的特征和行為,使得子類對(duì)象(實(shí)例)具有父類的實(shí)例域和方法,或子類從父類繼承方法,使得子類具有父類相同的行為2021-10-10springboot的緩存技術(shù)的實(shí)現(xiàn)
這篇文章主要介紹了springboot的緩存技術(shù)的實(shí)現(xiàn),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-05-05