SpingBoot Controller接收请求参数
程序员文章站
2024-02-18 18:25:46
...
一、SpringBoot Controller 接收请求参数------URL参数接收(get与post方法通用)
1.通过@PathVariable注解获取URL请求参数
/**
* 通过@PathVariable获取URL中的参数
* @param username
* @param password
* @return
*/
@RequestMapping(value="/test/{username}/{password}")
public String addUser4(@PathVariable String username,@PathVariable String password) {
System.out.println("username is:"+username);
System.out.println("password is:"+password);
return null;
}
2.通过JavaBean接收
Javabean:
public class UserModel {
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
Controller:
/**
* @param user
* @return
*/
@RequestMapping("/test")
public String addUser3(UserModel user) {
System.out.println("username is:"+user.getUsername());
System.out.println("password is:"+user.getPassword());
return null;
}
3.通过@RequestParam注解接收
/**
* 通过@RequestParam绑定请求参数到方法入参
* @param username
* @param password
* @return
*/
@RequestMapping(value="/test")
public String addUser6(@RequestParam("username") String username,@RequestParam("password") String password) {
System.out.println("username is:"+username);
System.out.println("password is:"+password);
return null;
}
二、SpringBoot Controller 接收请求参数------Post参数
1.JavaBean 配合 @RequestBody注解接收参数
JavaBean:
public class UserModel {
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
Controller:
/**
* @param user
* @return
*/
@RequestMapping("/test")
public String addUser3(@RequestBody UserModel user) {
System.out.println("username is:"+user.getUsername());
System.out.println("password is:"+user.getPassword());
return null;
}
2.fastjson配合@RequestBody注解接收JSON对象参数
/**
* @param user
* @return
*/
@RequestMapping("/test")
public String addUser3(@RequestBody JSONObject user) {
System.out.println("username is:"+user.getString("userName"));
System.out.println("password is:"+user.getString("passWord"));
return null;
}
上一篇: js的各种数据类型判断的介绍
下一篇: JS对象转URL参数