SpringBoot接收前端请求参数

SpringBoot接收前端请求参数

1.前端GET请求的URL参数与Controller类中的方法的参数名称对应

@Controller
@RequestMapping(value = "/test")
public class IndexController {

    @ResponseBody
    @RequestMapping("/test1")
    public String test(String username, String password){
        String str = "username:" + username + ";" + "password:" + password;
        return str;
    }
    
}

SpringBoot接收前端请求参数_第1张图片

2.通过HttpServletRequest来获取前端GET请求的参数

@Controller
@RequestMapping(value = "/test")
public class IndexController {

    @ResponseBody
    @RequestMapping("/test2")
    public String test2(HttpServletRequest request){
        String str = "username:" + request.getParameter("username") + ";" + "password:" + request.getParameter("password");
        return str;
    }

}

SpringBoot接收前端请求参数_第2张图片

3.通过创建一个JavaBean对象来封装表单参数或者是请求url路径中的参数

@Controller
@RequestMapping(value = "/test")
public class IndexController {

    @ResponseBody
    @RequestMapping("/test3")
    public String test3(User user){
        String str = "username:" + user.getUsername() + ";" + "password:" + user.getPassword();
        return str;
    }

}

User类

public class User {

    private Integer id;

    private String username;

    private String password;

    private Integer age;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username == null ? null : username.trim();
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password == null ? null : password.trim();
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }
}

4.@PathVariable

定义单个URL变量

    @ResponseBody
    @RequestMapping("/test4/{username}")
    public String userProfile(@PathVariable("username") String username){
        return "user:" + username;
    }

SpringBoot接收前端请求参数_第3张图片
定义多个URL变量

    @ResponseBody
    @RequestMapping("/user/{username}/pwd/{password}")
    public String test5(@PathVariable("username") String username , @PathVariable("password") String password){
        return "user:" + username + ";" + "pwd:" + password;
    }

SpringBoot接收前端请求参数_第4张图片
用正则表达式精确定义URL变量
除了简单地定义{username}变量,还可以定义正则表达式进行更精确的控制,定义语法是{变量名:正则表达式}[a-zA-Z0-9_]+是一个正则表达式,表示只能包含小写字母,大写字母,数字,下划线。如此设置URL变量规则后,不合法的URL则不会被处理,直接由SpringMVC框架返回404Not Found。

    @ResponseBody
    @RequestMapping("/test6/{username:[a-zA-Z0-9_]+}/{password}")
    public String test6(@PathVariable("username") String username , @PathVariable("password") String password){
        return "user:" + username + ";" + "pwd:" + password;
    }

SpringBoot接收前端请求参数_第5张图片
SpringBoot接收前端请求参数_第6张图片

5.@RequestParam

    @ResponseBody
    @RequestMapping("/test7")
    public String test7(@RequestParam(value = "username", required = false, defaultValue = "null") String username , @RequestParam(value = "password", required = false, defaultValue = "null") String password){
        return "user:" + username + ";" + "pwd:" + password;
    }

SpringBoot接收前端请求参数_第7张图片
SpringBoot接收前端请求参数_第8张图片

6.@RequestBody

@RequestBody能把简单json结构参数转换成实体类

    @ResponseBody
    @PostMapping("/test8")
    public String test8(@RequestBody User user){
        String str = "user:" + user.getUsername() + ";" + "pwd:" + user.getPassword();
        return str;
    }

User类

public class User {

    private Integer id;

    private String username;

    private String password;

    private Integer age;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username == null ? null : username.trim();
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password == null ? null : password.trim();
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }
}

SpringBoot接收前端请求参数_第9张图片

7.@ModelAttribute

    @ResponseBody
    @PostMapping("/test9")
    public String test9(@ModelAttribute("user") User user){
        String str = "user:" + user.getUsername() + ";" + "pwd:" + user.getPassword();
        return str;
    }

User类

public class User {

    private Integer id;

    private String username;

    private String password;

    private Integer age;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username == null ? null : username.trim();
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password == null ? null : password.trim();
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }
}

SpringBoot接收前端请求参数_第10张图片

你可能感兴趣的:(Java)