淺談@RequestMapping注解的注意點(diǎn)
@RequestMapping注解注意點(diǎn)
類上加沒(méi)加@RequestMappin注解區(qū)別
1.如果類上加了 @RequestMappin注解,那么就會(huì)去該注解對(duì)應(yīng)的路徑下去找頁(yè)面,如果沒(méi)有對(duì)應(yīng)的頁(yè)面就會(huì)報(bào)錯(cuò)。
舉例說(shuō)明:
@RequestMapping("/user") public class UserController { ?? ?@RequestMapping("/requestParam51") ? ? public String requestParam51(String[] name) { ? ? ? ?return "index.jsp"; ? ? } }
對(duì)應(yīng)的跳轉(zhuǎn)頁(yè)面會(huì)去user目錄下去找,找不到就會(huì)報(bào)錯(cuò)。
2.如果類上沒(méi)有加@RequestMapping注解,就會(huì)直接去根路徑下去找頁(yè)面
3.如果為跳轉(zhuǎn)的頁(yè)面加了"/",還是會(huì)去根路徑下去找對(duì)應(yīng)的頁(yè)面。
舉例:
@RequestMapping("/user") public class UserController { ?? ?@RequestMapping("/requestParam51") ? ? public String requestParam51(String[] name) { ? ? ? ?return "/index.jsp"; ? ? } }
@RequestMapping一個(gè)坑
今天發(fā)現(xiàn)了RequestMapping注解的一個(gè)坑:
當(dāng)RequestMapping用于Class上時(shí),不能用1.0,v1.0這樣帶小數(shù)點(diǎn)的value值做開(kāi)頭
@Controller @RequestMapping(value = "/v1.0") public class TestController { ? ? @RequestMapping(value = "/a", method = RequestMethod.GET, produces = "application/json") ? ? public @ResponseBody ? ? Object getA() { ? ? ? ? return ?"{\"test\" : \"a\"}"; ? ? } ? ? @RequestMapping(value = "/b", method = RequestMethod.GET, produces = "application/json") ? ? public @ResponseBody ? ? Object getB() { ? ? ? ? return ?"{\"test\" : \"b\"}"; ? ? } }
如上代碼運(yùn)行后,訪問(wèn)http://localhost:port/v1.0/a 或者h(yuǎn)ttp://localhost:port/v1.0/b 時(shí)都會(huì)報(bào)錯(cuò):
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping#0': Invocation of init method failed; nested exception is java.lang.IllegalStateException: Ambiguous mapping found. Cannot map 'testController' bean method
public java.lang.Object com.my.test.controller.TestController.getB()
to {[/v1.0],methods=[GET],params=[],headers=[],consumes=[],produces=[application/json],custom=[]}: There is already 'testController' bean method
單看異常信息,還以為是有重名的路徑,結(jié)果搜遍了工程也沒(méi)找到重名的類,后來(lái)"v1.0"改成"v1",就正常運(yùn)行了。
順帶測(cè)試了下,發(fā)現(xiàn)改成1.0也是同樣的錯(cuò)誤。
之后再把一個(gè)方法上RequestMapping的value去掉,采用默認(rèn)寫(xiě)法:
? ? @RequestMapping("/b") ? ? public @ResponseBody ? ? Object getB() { ? ? ? ? return ?"{\"test\" : \"b\"}"; ? ? }
再運(yùn)行起來(lái),訪問(wèn)http://localhost:port/v1.0/a或者h(yuǎn)ttp://localhost:port/v1.0/b 就會(huì)變成404錯(cuò)誤。
HTTP Status 404 - /v1.0/a type Status report message /v1.0/a description The requested resource is not available.
沒(méi)深究根本原因,估計(jì)是Spring的小bug,以后避免帶小數(shù)點(diǎn)的路徑頭。
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
BeanUtils.copyProperties()拷貝id屬性失敗的原因及解決
這篇文章主要介紹了BeanUtils.copyProperties()拷貝id屬性失敗的原因及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09為什么 Java 8 中不需要 StringBuilder 拼接字符串
java8中,編輯器對(duì)“+”進(jìn)行了優(yōu)化,默認(rèn)使用StringBuilder進(jìn)行拼接,所以不用顯示的使用StringBuilder了,直接用“+”就可以了。下面我們來(lái)詳細(xì)了解一下2019-05-05java 中序列化NotSerializableException問(wèn)題解決辦法
這篇文章主要介紹了java 中序列化NotSerializableException問(wèn)題解決辦法的相關(guān)資料,這里對(duì)序列化問(wèn)題進(jìn)行描述說(shuō)明,并提供解決辦法,希望能幫助到大家,需要的朋友可以參考下2017-08-08