您好,欢迎访问一九零五行业门户网

java详解Spring接收web请求参数的方式

本篇文章给大家带来的内容是java详解spring接收web请求参数的方式 。有一定的参考价值,有需要的朋友可以参考一下,希望对你们有所帮助。
1 查询参数
请求格式:url?参数1=值1&参数2=值2...
同时适用于get和post方式
spring处理查询参数的方法又有几种写法:
方法一:
方法参数名即为请求参数名
  // 查询参数1  @requestmapping(value = /test/query1, method = requestmethod.get)    public string testquery1(string username, string password) {    system.out.println(username= + username + , password= + password);        return username= + username + , password= + password;  }
方法二:
从httpservletrequest中提取参数
  // 查询参数2  @requestmapping(value = /test/query2, method = requestmethod.get)    public string testquery2(httpservletrequest request) {    string username = request.getparameter(username);    string password = request.getparameter(password);    system.out.println(username= + username + , password= + password);        return username= + username + , password= + password;  }
方法三:
方法参数名和请求参数名可以不一样,通过@requestparam注解来绑定参数
  // 查询参数3  @requestmapping(value = /test/query3, method = requestmethod.get)    public string testquery3(@requestparam(username) string un, @requestparam(password) string pw) {    system.out.println(username= + un + , password= + pw);        return username= + un + , password= + pw;  }
方法四:
创建一个实体类对象作为参数承载体,spring会根据参数名称自动将参数绑定到实体类对象的属性上
  // 查询参数4  @requestmapping(value = /test/query4, method = requestmethod.get)    public string testquery4(user user) {    string username = user.getusername();    string password = user.getpassword();    system.out.println(username= + username + , password= + password);        return username= + username + , password= + password;  }
实体类定义如下:
@data@noargsconstructor@allargsconstructor@builderpublic class user {     private string username;     private string password;}
这里用到了第三方库lombok,这样就不需要在代码中手动添加get、set等方法,lombok会自动添加。
发送请求的curl命令如下:
curl -i 'http://192.168.1.14:8080/test/query1?username=aaa&password=bbb'
交互报文如下:
get /test/query1?username=aaa&password=bbb http/1.1host: 192.168.1.14:8080user-agent: curl/7.58.0accept: */*http/1.1 200 content-type: text/plain;charset=utf-8content-length: 26date: thu, 25 oct 2018 07:01:30 gmtusername=aaa, password=bbb
2 表单参数
请求参数不在url中,而是在body体中,格式为:url?参数1=值1&参数2=值2...
适用于post方式
表单参数处理方法和前面的请求参数处理方法几乎完全一样,只是requestmethod注解中将method方法设置成post方法
方法一:
  // 表单参数1  @requestmapping(value = /test/form1, method = requestmethod.post)    public string testform1(string username, string password) {    system.out.println(username= + username + , password= + password);        return username= + username + , password= + password;  }
方法二:
  // 表单参数2  @requestmapping(value = /test/form2, method = requestmethod.post)    public string testform2(httpservletrequest request) {    string username = request.getparameter(username);    string password = request.getparameter(password);    system.out.println(username= + username + , password= + password);        return username= + username + , password= + password;  }
方法三:
  // 表单参数3  @requestmapping(value = /test/form3, method = requestmethod.post)    public string testform3(@requestparam(username) string un, @requestparam(password) string pw) {    system.out.println(username= + un + , password= + pw);        return username= + un + , password= + pw;  }
方法四:
  // 表单参数4  @requestmapping(value = /test/form4, method = requestmethod.post)  public string testform4(user user) {    string username = user.getusername();    string password = user.getpassword();    system.out.println(username= + username + , password= + password);        return username= + username + , password= + password;  }
curl请求命令如下:
curl -x post -i -d username=aaa&password=bbb http://192.168.1.14:8080/test/form1
请求和响应报文如下:
post /test/form1 http/1.1host: 192.168.1.14:8080user-agent: curl/7.58.0accept: */*content-length: 25content-type: application/x-www-form-urlencodedusername=aaa&password=bbbhttp/1.1 200 content-type: text/plain;charset=utf-8content-length: 26date: thu, 25 oct 2018 07:05:35 gmtusername=aaa, password=bbb
3 路径参数
请求参数为url中的一部分,格式为:url/参数1/参数2...
同时适用于get和post方式
代码如下:
  @requestmapping(value = /test/url/{username}/{password}, method = requestmethod.get)    public string testurl(@pathvariable string username, @pathvariable string password) {    system.out.println(username= + username + , password= + password);        return username= + username + , password= + password;  }
请求curl命令如下:
curl -i http://192.168.1.14:8080/test/url/aaa/bbb
请求和响应报文如下:
get /test/url/aaa/bbb http/1.1host: 192.168.1.14:8080user-agent: curl/7.58.0accept: */*http/1.1 200 content-type: text/plain;charset=utf-8content-length: 26date: thu, 25 oct 2018 07:07:44 gmtusername=aaa, password=bbb
4 json格式参数
请求参数在body体中,并且为json格式。需要添加请求头:content-type: application/json;charset=utf-8
适用于post方式
方法一:
定义实体类,将json对象解析成实力类,需要添加requestbody注解
  // json参数1  @requestmapping(value = /test/json1, method = requestmethod.post)    public string testjson1(@requestbody user user) {    string username = user.getusername();    string password = user.getpassword();    system.out.println(username= + username + , password= + password);        return username= + username + , password= + password;  }
方法二:
如果不像定义实体类,也可以将json请求直接解析成jsonobject类
  // json参数2  @requestmapping(value = /test/json2, method = requestmethod.post)    public string testjson2(@requestbody jsonobject json) {    string username = json.getstring(username);    string password = json.getstring(password);    system.out.println(username= + username + , password= + password);        return username= + username + , password= + password;  }
方法三:
也可以将json对象直接解析成map对象
  // json参数3  @requestmapping(value = /test/json3, method = requestmethod.post)    public string testjson3(@requestbody map18fed3eaa375b9cabd6da695ec776cac usermap) {    string username = usermap.get(username);    string password = usermap.get(password);    system.out.println(username= + username + , password= + password);        return username= + username + , password= + password;  }
请求curl命令如下:
curl -x post -i -h 'content-type: application/json;charset=utf-8' -d '{       username : aaa,       password : bbb} 'http://192.168.1.14:8080/test/json1
请求和响应报文如下:
post /test/json1 http/1.1host: 192.168.1.14:8080user-agent: curl/7.58.0accept: */*content-type: application/json;charset=utf-8content-length: 52{ "username" : "aaa", "password" : "bbb"}http/1.1 200 content-type: text/plain;charset=utf-8content-length: 26date: thu, 25 oct 2018 07:09:06 gmtusername=aaa, password=bbb
以上就是java详解spring接收web请求参数的方式的详细内容。
其它类似信息

推荐信息