请求参数

提交的参数名一致时

请求URL:http://localhost:8080/hello?name=zs&age=20

@RequestMapping("/hello")
publc String hello(String name, int age) {
    System.out.println(name);    // 输出 zs
    System.out.println(age);    // 输出 20
    return "hello";
}

提交的请求参数名不一致

请求URL:http://localhost:8080/hello?username=zs&age=20

@RequestMapping("/hello")
publc String hello(@RequestParam("username") String name, int age) {
    System.out.println(name);    // 输出 zs
    System.out.println(age);    // 输出 20
    return "hello";
}

提交的是一个对象

请求URL:http://localhost:8080/hello?username=zs&age=20

public class User {
    private String name;
    private int age;
}
@RequestMapping("/hello")
publc String hello(User user) {
    System.out.println(user);
    return "hello";
}

使用对象的话,前端传递的参数名和实体类中的属性名必须保持一致,都则就是null。

@RequestBody 获取请求参数

最后更新于

这有帮助吗?