亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

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

Getmapping獲取參數(shù)的方式

Springboot中Getmapping使用PathVariable、HttpServletRequest、RequestParam獲取參數(shù)

今天在學習Springboot中遇得到了一個問題,放一段代碼

?@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è)務實體,service.studentLogin是service層里的方法

這樣一看卻是是沒有什么問題,使用接口測試工具測試返回的結(jié)果,結(jié)果這里設定的只有true和false。

but!

這里出現(xiàn)了404,表示找不到相應的頁面

嚴格按照了網(wǎng)上各種教程里面的流程,為什么會出現(xiàn)404?

請教了組里的大佬之后發(fā)現(xiàn)問題出現(xiàn)在了一個小小的 ? 上面

我們將下面鏈接里的?去掉

http://localhost:8080/student/login/?newpwd=77777

變成這樣

http://localhost:8080/student/login/newpwd=77777

404的問題不復存在,控制臺也打印出了我們需要的參數(shù)的值。當然新的錯誤就是后面的邏輯錯誤(我并沒有輸入其他需要的參數(shù))。

其他傳參方式

除了PathVariable這個方式之外,還有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ù),如果沒有業(yè)務實體外的參數(shù)傳入,我們只需要申請一個實體對象就可以接受url傳過來的參數(shù)

上面的代碼執(zhí)行結(jié)果

可以看出,實體內(nèi)的參數(shù)和實體外的參數(shù)都被傳入了方法

在此之外

還有HttpServletRequest可以接受參數(shù),為此我寫了一個測試方法

@GetMapping(value="/student/findById")
    public void findById(HttpServletRequest req){
    	String s=req.getParameter("id");
    }

不過這樣的方法需要指定url中值得名稱,就是所謂的 “鍵值對”

運行結(jié)果:

@GetMapping參數(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
}
//請求url:http://localhost:8888/example1?money=123&product=洗潔精

當參數(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
? ? ? ? ? ? }
? ? ? ? }
? ? }
? ? //請求url:http://localhost:8888/example2?keywords=123,456

當參數(shù)為簡單對象時

@GetMapping("/example3")
? ? public void example3(SubTest1 subTest1){
? ? ? ? System.out.println(subTest1);//SubTest1{content='測試內(nèi)容'}
? ? }
? ? //請求url:http://localhost:8888/example3?content=測試內(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 + '\'' +
? ? ? ? ? ? ? ? '}';
? ? }
}

當參數(shù)的對象中嵌套著對象

對象中的屬性為list和map時

@GetMapping("/example4")
? ? public void example4(TestDto testDto){
? ? ? ? System.out.println(testDto);//TestDto{title='測試標題', subTest=SubTest{ids=[123, 456], map={k=value}}, subTest1=SubTest1{content='測試內(nèi)容'}}
? ? }
? ? //請求url:http://localhost:8888/example4?title=測試標題&subTest.ids[0]=123&subTest.ids[1]=456&subTest.map[k]=value&SubTest1.content=測試內(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ù)的時候,程序會報錯的;直接用map作為參數(shù)的時候,沒辦法獲取到值,都是null,但是不會報錯;不知道是姿勢錯誤,還是本身不支持

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

您可能感興趣的文章:

相關文章

最新評論