仅使用get方法来进行演示,其他请求方法(POST,DELETE,PUT)接受参数的形式都是一样的。
接受数字类型的参数
通过get的url给后端传递参数,可以看到虽然在地址栏里 id=12345
中的12345是字符串的形式,但controller会自动把字符串转换成整型。如果把 id=12345
换成 id=name
就会报错。
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class CIsBestController {
// 测试url: http://localhost:8080/int?id=12345
// 在浏览器中会显示 : 响应接受到的数字:12345
@GetMapping("/int")
@ResponseBody
public String ResInt(Integer id){
System.out.println("接受到的数字是:" + id);
return "响应接受到的数字:" + id;
}
}
接受字符串类型的参数
直接在地址栏中输入 http://localhost:8080/string?name=氵灬
就能看到结果。
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class CIsBestController {
// 接受整数 测试url: http://localhost:8080/string?name=氵灬
// 浏览器中显示的数据应该是 : 响应接受到的数字:氵灬
@GetMapping("/string")
@ResponseBody
public String ResInt(String name){
System.out.println("接受到的字符串是:" + name);
return "响应接受到的数字:" + name;
}
}
通过 @RequestParam
来显示绑定接受的参数
如果前端在地址栏中输入的是 http://localhost:8080/string?user_name=氵灬
,显然 user_name
这个字段不符合java中驼峰命名的规则。这这个时候就要使用 @RequestParam
注解来显示声明前端这个字段是用哪个参数来接受的。
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class CIsBestController {
// 接受整数 测试url: http://localhost:8080/string?user_name=氵灬
// 浏览器中显示的数据应该是 : 响应接受到的数字:氵灬
@GetMapping("/string")
@ResponseBody
public String ResInt(@RequestParam("user_name") String userName){
System.out.println("接受到的字符串是:" + userName);
return "响应接受到的数字:" + userName;
}
}
@RequestParam 中可以配置两个参数一个是 value
,如果@RequestParam注解中只有一个参数,那value
的值就是默认等于这个参数 @RequestParam("user_name")
的写法和 @RequestParam(value = "user_name")
效果是一样的。还是个参数就是 defaultValue
看字面意思就能知道是设置默认值的意思。如果前端传的这个参数为空则使用默认值提供的值。
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class CIsBestController {
// 接受整数 测试url: http://localhost:8080/string?user_name=氵灬
// 浏览器中显示的数据应该是 : 响应接受到的数字:氵灬
// 接受整数 测试url: http://localhost:8080/string?user_name=
// 浏览器中显示的数据应该是 : 响应接受到的数字:默认值
@GetMapping("/string")
@ResponseBody
public String ResInt(@RequestParam(value = "user_name",defaultValue = "默认值") String userName){
System.out.println("接受到的字符串是:" + userName);
return "响应接受到的数字:" + userName;
}
}
通过对象来接受的参数
可以通过java对象来接受参数。通过对象来接受参数。先定义一个实体类,在再controller中使用
User.java
package com.cisbest.entity;
import lombok.Data;
@Data
public class User {
private String userName;
private Integer age;
private String sex;
}
CIsBestController.java
package com.cisbest.controller;
import com.cisbest.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class CIsBestController {
// 接受整数 测试url: http://localhost:8080/entity?name=氵灬&age=18&sex=男
// 浏览器中显示的数据应该是 : 响应接受到的数字:User(name=氵灬, age=18, sex=男)
@GetMapping("/entity")
@ResponseBody
public String ResInt(User user){
System.out.println("接受到的字符串是:" + user.toString());
return "响应接受到的数字:" + user.toString();
}
}
@RequestMapping
详解
@RequestMapping
可以作用在类上也可以作用到方法上。
写在类上表示上一级请求路径。
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@RequestMapping("/api")
public class CIsBestController {
// 访问路径 http://localhost:8080/api/req
@RequestMapping("/req")
@ResponseBody
public String resp() {
return "响应数据";
}
}
写在方法上:
-
@RequestMapping("/req")
表示不区分请求类型。 -
@RequestMapping(value = "/req",method = RequestMethod.POST)
表示这是一个POST请求。