当前位置: 移动技术网 > IT编程>开发语言>Java > 浅谈SpringBoot处理url中的参数的注解

浅谈SpringBoot处理url中的参数的注解

2019年07月19日  | 移动技术网IT编程  | 我要评论

1.介绍几种如何处理url中的参数的注解

@pathvaribale 获取url中的数据

@requestparam 获取请求参数的值

@getmapping 组合注解,是 @requestmapping(method = requestmethod.get) 的缩写

(1)pathvaribale 获取url中的数据

看一个例子,如果我们需要获取url=localhost:8080/hello/id中的id值,实现代码如下:

@restcontroller
public class 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/helloworld 然后会在html页面上打印出:

id:81

同样,如果我们需要在url有多个参数需要获取,则如下代码所示来做就可以了。

@restcontroller
public class 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/helloworld 然后会在html页面上打印出:

id:100 name:helloworld

以上,通过 @pathvariable 注解来获取url中的参数时的前提条件是我们知道url的格式时怎么样的。

只有知道url的格式,我们才能在指定的方法上通过相同的格式获取相应位置的参数值。

一般情况下,url的格式为: localhost:8080/hello?id=98 ,这种情况下该如何来获取其id值呢,这就需要借助于 @requestparam 来完成了

2.@requestparam 获取请求参数的值

例如:

@restcontroller
public class hellocontroller {
 @requestmapping(value="/hello",method= requestmethod.get)
 public string sayhello(@requestparam("id") integer id){
 return "id:"+id;
 }
}

在浏览器中输入地址: localhost:8080/hello?id=1000 ,可以看到如下的结果:

id:1000

当我们在浏览器中输入地址: localhost:8080/hello?id ,即不输入id的具体值,此时返回的结果为null。具体测试结果如下:

id:null

但是,当我们在浏览器中输入地址: localhost:8080/hello ,即不输入id参数,则会报如下错误:

whitelable error page错误

@requestparam 注解给我们提供了这种解决方案,即允许用户不输入id时,使用默认值,具体代码如下:

@restcontroller
public class hellocontroller {
 @requestmapping(value="/hello",method= requestmethod.get)
 //required=false 表示url中可以不穿入id参数,此时就使用默认参数
 public string sayhello(@requestparam(value="id",required = false,defaultvalue = "1") integer id){
 return "id:"+id;
 }
}

如果在url中有多个参数,即类似于 localhost:8080/hello?id=98&&name=helloworld 这样的url,同样可以这样来处理。具体代码如下:

@restcontroller
public class hellocontroller {
 @requestmapping(value="/hello",method= requestmethod.get)
 public string sayhello(@requestparam("id") integer id,@requestparam("name") string name){
 return "id:"+id+ " name:"+name;
 }
}

在浏览器中的测试结果如下: localhost:8080/hello?id=1000&name=helloworld 地址,就会显示下面的内容:

id:1000 name:helloworld

3.@getmapping 组合注解

@getmapping 是一个组合注解,是 @requestmapping(method = requestmethod.get) 的缩写。该注解将http get 映射到 特定的处理方法上。

即可以使用 @getmapping(value = “/hello”) 来代替 @requestmapping(value=”/hello”,method= requestmethod.get) 。即可以让我们精简代码。

@restcontroller
public class hellocontroller {
//@requestmapping(value="/hello",method= requestmethod.get)
@getmapping(value = "/hello")
//required=false 表示url中可以不穿入id参数,此时就使用默认参数
public string sayhello(@requestparam(value="id",required = false,defaultvalue = "1") integer id){
 return "id:"+id;
 }
 }

4.postmapping组合注解:

方法同getmapping

以上这篇浅谈springboot处理url中的参数的注解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网