温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

springboot怎样读取http请求url地址中的参数

发布时间:2021-09-29 14:02:53 来源:亿速云 阅读:129 作者:柒染 栏目:编程语言

本篇文章给大家分享的是有关springboot怎样读取http请求url地址中的参数,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。

定义一个Rest接口时通常会利用GET、POST、PUT、DELETE来实现数据的增删改查;这几种方式有的需要传递参数,后台开发人员必须对接收到的参数进行参数验证来确保程序的健壮性

  • GET:一般用于查询数据,采用明文进行传输,一般用来获取一些无关用户信息的数据

  • POST:一般用于插入数据

  • PUT:一般用于数据更新

  • DELETE:一般用于数据删除;一般都是进行逻辑删除(即:仅仅改变记录的状态,而并非真正的删除数据)

1、@PathVaribale 获取url中的数据

请求URL:localhost:8080/hello/id 获取id值

实现代码如下:

//java项目www.fhadmin.org@RestControllerpublicclass HelloController {    
    @RequestMapping(value="/hello/{id}/{name}",method= RequestMethod.GET)    public String sayHello(@PathVariable("id") Integer id,@PathVariable("name") String name){        return"id:"+id+" name:"+name;    
    }
    
}

在浏览器中 输入地址:

localhost:8080/hello/100/hello

输出:

id:81name:hello

2、@RequestParam 获取请求参数的值

获取url参数值,默认方式,需要方法参数名称和url参数保持一致

请求URL:localhost:8080/hello?id=1000

//java项目www.fhadmin.org@RestControllerpublicclass HelloController {  
    @RequestMapping(value="/hello",method= RequestMethod.GET)    public String sayHello(@RequestParam Integer id){       return"id:"+id;    
    }
    
}

输出:id:100

url中有多个参数时,如:

localhost:8080/hello?id=98&&name=helloworld

具体代码如下:

//java项目www.fhadmin.org@RestControllerpublicclass HelloController {    
    @RequestMapping(value="/hello",method= RequestMethod.GET)   public String sayHello(@RequestParam Integer id,@RequestParam String name){  return"id:"+id+ " name:"+name;  
    }
    
}

获取url参数值,执行参数名称方式

localhost:8080/hello?userId=1000

//java项目www.fhadmin.org@RestControllerpublicclass HelloController {   
    @RequestMapping(value="/hello",method= RequestMethod.GET)   public String sayHello(@RequestParam("userId") Integer id){    return"id:"+id;  
    }
    
}

输出:id:100

以上就是springboot怎样读取http请求url地址中的参数,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI