欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

springboot中Getmapping獲取參數(shù)的實(shí)現(xiàn)方式

 更新時(shí)間:2022年05月06日 14:39:25   作者:check_bug  
這篇文章主要介紹了springboot中Getmapping獲取參數(shù)的實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

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)文章

最新評(píng)論