服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - springboot中Getmapping获取参数的实现方式

springboot中Getmapping获取参数的实现方式

2022-12-02 17:38check_bug Java教程

这篇文章主要介绍了springboot中Getmapping获取参数的实现方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

Getmapping获取参数的方式

Springboot中Getmapping使用PathVariable、HttpServletRequest、RequestParam获取参数

今天在学习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是业务实体,service.studentLogin是service层里的方法

这样一看却是是没有什么问题,使用接口测试工具测试返回的结果,结果这里设定的只有true和false。

but!

springboot中Getmapping获取参数的实现方式

这里出现了404,表示找不到相应的页面

严格按照了网上各种教程里面的流程,为什么会出现404?

请教了组里的大佬之后发现问题出现在了一个小小的 ? 上面

我们将下面链接里的?去掉

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

变成这样

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

springboot中Getmapping获取参数的实现方式

springboot中Getmapping获取参数的实现方式

404的问题不复存在,控制台也打印出了我们需要的参数的值。当然新的错误就是后面的逻辑错误(我并没有输入其他需要的参数)。

其他传参方式

除了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;
  }

这样即使我们不规定传入的参数,也可以自行传入任何参数,如果没有业务实体外的参数传入,我们只需要申请一个实体对象就可以接受url传过来的参数

上面的代码执行结果

springboot中Getmapping获取参数的实现方式

springboot中Getmapping获取参数的实现方式

可以看出,实体内的参数和实体外的参数都被传入了方法

在此之外

还有HttpServletRequest可以接受参数,为此我写了一个测试方法

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

不过这样的方法需要指定url中值得名称,就是所谓的 “键值对”

运行结果:

springboot中Getmapping获取参数的实现方式

springboot中Getmapping获取参数的实现方式

 

@GetMapping参数接收理解

当参数为基本类型时

@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=洗洁精

当参数为数组时

 @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

当参数为简单对象时

@GetMapping("/example3")
    public void example3(SubTest1 subTest1){
        System.out.println(subTest1);//SubTest1{content='测试内容'}
    }
    //请求url:http://localhost:8888/example3?content=测试内容
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 + '\'' +
                '}';
    }
}

当参数的对象中嵌套着对象

对象中的属性为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='测试内容'}}
    }
    //请求url:http://localhost:8888/example4?title=测试标题&subTest.ids[0]=123&subTest.ids[1]=456&subTest.map[k]=value&SubTest1.content=测试内容
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作为参数的时候,程序会报错的;直接用map作为参数的时候,没办法获取到值,都是null,但是不会报错;不知道是姿势错误,还是本身不支持

以上为个人经验,希望能给大家一个参考,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/check_bug/article/details/101350373

延伸 · 阅读

精彩推荐