springboot中Getmapping獲取參數(shù)的實(shí)現(xiàn)方式
Getmapping獲取參數(shù)的方式
Springboot中Getmapping使用PathVariable、HttpServletRequest、RequestParam獲取參數(shù)
今天在學(xué)習(xí)Springboot中遇得到了一個(gè)問(wèn)題,放一段代碼
?@GetMapping(value="/student/login/{newpwd}") ? ? public Map studentLogin(@PathVariable("newpwd") String newpwd, Student stu){ ? ? ? ? System.out.println("pwd:"+newpwd); ? ? ? ? String res=service.studentLogin(stu.getUsername(),stu.getPswd()); ? ? ? ? System.out.println(res); ? ? ? ? Map map=new HashMap(); ? ? ? ? map.put("result",res); ? ? ? ? return map;
代碼中的Student是業(yè)務(wù)實(shí)體,service.studentLogin是service層里的方法
這樣一看卻是是沒(méi)有什么問(wèn)題,使用接口測(cè)試工具測(cè)試返回的結(jié)果,結(jié)果這里設(shè)定的只有true和false。
but!
這里出現(xiàn)了404,表示找不到相應(yīng)的頁(yè)面
嚴(yán)格按照了網(wǎng)上各種教程里面的流程,為什么會(huì)出現(xiàn)404?
請(qǐng)教了組里的大佬之后發(fā)現(xiàn)問(wèn)題出現(xiàn)在了一個(gè)小小的 ? 上面
我們將下面鏈接里的?去掉
http://localhost:8080/student/login/?newpwd=77777
變成這樣
http://localhost:8080/student/login/newpwd=77777
404的問(wèn)題不復(fù)存在,控制臺(tái)也打印出了我們需要的參數(shù)的值。當(dāng)然新的錯(cuò)誤就是后面的邏輯錯(cuò)誤(我并沒(méi)有輸入其他需要的參數(shù))。
其他傳參方式
除了PathVariable這個(gè)方式之外,還有RequestParam的方式,這里放一下具體的代碼
@GetMapping(value="/student/login") public Map studentLogin(@RequestParam("newpwd") String newpwd, Student stu){ System.out.println("pwd:"+newpwd); String res=service.studentLogin(stu.getUsername(),stu.getPswd()); System.out.println(res); Map map=new HashMap(); map.put("result",res); return map; }
為了看得更明白,我放一下service代碼:
public String studentLogin(String userName,String pswd){ String isUser="false"; Student s=properties.findByUsername(userName); if(s.getPswd().equals(pswd)) isUser="true"; return isUser; }
這樣即使我們不規(guī)定傳入的參數(shù),也可以自行傳入任何參數(shù),如果沒(méi)有業(yè)務(wù)實(shí)體外的參數(shù)傳入,我們只需要申請(qǐng)一個(gè)實(shí)體對(duì)象就可以接受url傳過(guò)來(lái)的參數(shù)
上面的代碼執(zhí)行結(jié)果
可以看出,實(shí)體內(nèi)的參數(shù)和實(shí)體外的參數(shù)都被傳入了方法
在此之外
還有HttpServletRequest可以接受參數(shù),為此我寫(xiě)了一個(gè)測(cè)試方法
@GetMapping(value="/student/findById") public void findById(HttpServletRequest req){ String s=req.getParameter("id"); }
不過(guò)這樣的方法需要指定url中值得名稱,就是所謂的 “鍵值對(duì)”
運(yùn)行結(jié)果:
@GetMapping參數(shù)接收理解
當(dāng)參數(shù)為基本類型時(shí)
@GetMapping("/example1") public void example1(Float money, String product){ ? ? System.out.println("product:"+ product);//product:洗潔精 ? ? System.out.println("money:"+ money);//money:123.0 } //請(qǐng)求url:http://localhost:8888/example1?money=123&product=洗潔精
當(dāng)參數(shù)為數(shù)組時(shí)
?@GetMapping("/example2") ? ? public void example2(String[] keywords){ ? ? ? ? if (keywords != null){ ? ? ? ? ? ? for (int i=0; i<keywords.length; i++){ ? ? ? ? ? ? ? ? System.out.println(keywords[i]);//123 456 ? ? ? ? ? ? } ? ? ? ? } ? ? } ? ? //請(qǐng)求url:http://localhost:8888/example2?keywords=123,456
當(dāng)參數(shù)為簡(jiǎn)單對(duì)象時(shí)
@GetMapping("/example3") ? ? public void example3(SubTest1 subTest1){ ? ? ? ? System.out.println(subTest1);//SubTest1{content='測(cè)試內(nèi)容'} ? ? } ? ? //請(qǐng)求url:http://localhost:8888/example3?content=測(cè)試內(nèi)容 public class SubTest1 { ? ? private String content; ? ? public String getContent() { ? ? ? ? return content; ? ? } ? ? public void setContent(String content) { ? ? ? ? this.content = content; ? ? } ? ? @Override ? ? public String toString() { ? ? ? ? return "SubTest1{" + ? ? ? ? ? ? ? ? "content='" + content + '\'' + ? ? ? ? ? ? ? ? '}'; ? ? } }
當(dāng)參數(shù)的對(duì)象中嵌套著對(duì)象
對(duì)象中的屬性為list和map時(shí)
@GetMapping("/example4") ? ? public void example4(TestDto testDto){ ? ? ? ? System.out.println(testDto);//TestDto{title='測(cè)試標(biāo)題', subTest=SubTest{ids=[123, 456], map={k=value}}, subTest1=SubTest1{content='測(cè)試內(nèi)容'}} ? ? } ? ? //請(qǐng)求url:http://localhost:8888/example4?title=測(cè)試標(biāo)題&subTest.ids[0]=123&subTest.ids[1]=456&subTest.map[k]=value&SubTest1.content=測(cè)試內(nèi)容 public class TestDto { ? ? private String title; ? ? private SubTest subTest; ? ? private SubTest1 subTest1; ? ? public SubTest1 getSubTest1() { ? ? ? ? return subTest1; ? ? } ? ? public void setSubTest1(SubTest1 subTest1) { ? ? ? ? this.subTest1 = subTest1; ? ? } ? ? @Override ? ? public String toString() { ? ? ? ? return "TestDto{" + ? ? ? ? ? ? ? ? "title='" + title + '\'' + ? ? ? ? ? ? ? ? ", subTest=" + subTest + ? ? ? ? ? ? ? ? ", subTest1=" + subTest1 + ? ? ? ? ? ? ? ? '}'; ? ? } ? ? public String getTitle() { ? ? ? ? return title; ? ? } ? ? public void setTitle(String title) { ? ? ? ? this.title = title; ? ? } ? ? public SubTest getSubTest() { ? ? ? ? return subTest; ? ? } ? ? public void setSubTest(SubTest subTest) { ? ? ? ? this.subTest = subTest; ? ? } } public class SubTest { ? ? private List<Long> ids; ? ? private Map map; ? ? public Map getMap() { ? ? ? ? return map; ? ? } ? ? public void setMap(Map map) { ? ? ? ? this.map = map; ? ? } ? ? public List<Long> getIds() { ? ? ? ? return ids; ? ? } ? ? public void setIds(List<Long> ids) { ? ? ? ? this.ids = ids; ? ? } ? ? @Override ? ? public String toString() { ? ? ? ? return "SubTest{" + ? ? ? ? ? ? ? ? "ids=" + ids + ? ? ? ? ? ? ? ? ", map=" + map + ? ? ? ? ? ? ? ? '}'; ? ? } }
//TODO:在直接用list作為參數(shù)的時(shí)候,程序會(huì)報(bào)錯(cuò)的;直接用map作為參數(shù)的時(shí)候,沒(méi)辦法獲取到值,都是null,但是不會(huì)報(bào)錯(cuò);不知道是姿勢(shì)錯(cuò)誤,還是本身不支持
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
詳解在Spring Boot框架下使用WebSocket實(shí)現(xiàn)消息推送
這篇文章主要介紹了詳解在Spring Boot框架下使用WebSocket實(shí)現(xiàn)消息推送,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2016-12-12Java redis存Map對(duì)象類型數(shù)據(jù)的實(shí)現(xiàn)
本文主要介紹了Java redis存Map<String,RedisCustom>對(duì)象類型數(shù)據(jù),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-05-05IDEA 當(dāng)前在線人數(shù)和歷史訪問(wèn)量的示例代碼
這篇文章主要介紹了IDEA 當(dāng)前在線人數(shù)和歷史訪問(wèn)量的實(shí)例代碼,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-08-08基于SpringBoot2.0默認(rèn)使用Redis連接池的配置操作
這篇文章主要介紹了基于SpringBoot2.0默認(rèn)使用Redis連接池的配置操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-12-12通過(guò)spring boot 設(shè)置tomcat解決 post參數(shù)限制問(wèn)題
這篇文章主要介紹了通過(guò)spring boot 設(shè)置tomcat解決 post參數(shù)限制問(wèn)題,需要的朋友可以參考下2019-05-05解決IDEA無(wú)法讀取maven鏡像,jar包下載失敗的問(wèn)題
這篇文章主要介紹了解決IDEA無(wú)法讀取maven鏡像,jar包下載失敗的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-10-10Nacos與SpringBoot實(shí)現(xiàn)配置管理的開(kāi)發(fā)實(shí)踐
在微服務(wù)架構(gòu)中,配置管理是一個(gè)核心組件,而Nacos為此提供了一個(gè)強(qiáng)大的解決方案,本文主要介紹了Nacos與SpringBoot實(shí)現(xiàn)配置管理的開(kāi)發(fā)實(shí)踐,具有一定的參考價(jià)值2023-08-08java隨機(jī)數(shù)生成具體實(shí)現(xiàn)代碼
這篇文章主要為大家分享了java隨機(jī)數(shù)生成具體實(shí)現(xiàn)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-04-04