1.简单参数接收
前端请求参数与Controller接受变量名一致
如果参数名不一致,接受不成功。
可以用@RequestParam指定参数名,可以用username接收(不推荐)。
required = true,表示参数必须传递,如果不传递会报错,false代表可选的参数,可以不传递
@RequestMapping("/simplePara")
public String simplePara(@RequestParam(name="name",required = false) String username, Integer age){
System.out.println(username+":"+age);
return "OK";
}
## 2.实体参数接收
请求参数名与形参对象属性名相同
@RequestMapping("/simplePojo")
public String simplePojo(User user){
System.out.println(user);
return "OK";
}
3.复杂实体参数接收
@RequestMapping("/complexPojo")
public String complexPojo(User user){
System.out.println(user);
return "OK";
}
4.数组参数接收
@RequestMapping("/arrayPara")
public String arrayPara(String[] hobby){
System.out.println(Arrays.toString(hobby));
return "OK";
}
5.集合接收
- 请求参数名hobby与 集合变量名相同hobby
- 加上@RequestParam
```java
@RequestMapping(“/listPara”)
public String listPara(@RequestParam List hobby){
System.out.println(hobby);
return “OK”;
}
``
6.日期参数接收
- 后端要使用@DateTimeFormat注解标记参数
- 后端接收要指定接收日期的格式(与前端传递的格式一样):yyyy-MM-dd HH:mm:ss
@RequestMapping("/dataParam")
public String dataParam(@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime updateTime){
System.out.println(updateTime);
return "OK";
}
7.Json格式参数接收
- 后端通过实体对象接收数据
- 请求参数需要用@RequestBody 标记
@RequestMapping("/jsonParam")
public String jsonParam(@RequestBody User user){
System.out.println(user);
return "OK";
}
8.路径参数,参数已经是url的一部分\
- {id}是个路径参数
- 参数需要用@PathVariable 标记
@RequestMapping("/pathParam/{id}")
public String pathParam(@PathVariable Integer id){
System.out.println(id);
return "OK";
}
//多个路径参数
@RequestMapping("/pathParam2/{id}/{name}")
public String pathParam(@PathVariable Integer id,@PathVariable String name){
System.out.println(id + ":" + name);
return "OK";
}
总结
常用注解
每个接口返回的数据有各种类型(字符串,数组,Json),不规范
可以用Result封装返回的数据
-
响应字符串请求
-
响应json请求
-
响应集合请求
public class Result {
private Integer code ;//1 成功 , 0 失败
private String msg; //提示信息
private Object data; //数据 data
public Result() {
}
public Result(Integer code, String msg, Object data) {
this.code = code;
this.msg = msg;
this.data = data;
}
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
public static Result success(Object data){
return new Result(1, "success", data);
}
public static Result success(){
return new Result(1, "success", null);
}
public static Result error(String msg){
return new Result(0, msg, null);
}
@Override
public String toString() {
return "Result{" +
"code=" + code +
", msg='" + msg + '\'' +
", data=" + data +
'}';
}
}
请求处理代码
@RestController
public class ResponseController {
//统一相应结果
@RequestMapping("/hello")
public Result hello(){
System.out.println("Hello World ~");
//return new Result(1,"success","Hello World ~");
return Result.success("Hello World ~");
}
@RequestMapping("/getAddr")
public Result getAddr(){
Address addr = new Address();
addr.setProvince("广东");
addr.setCity("深圳");
return Result.success(addr);
}
@RequestMapping("/listAddr")
public Result listAddr(){
List<Address> list = new ArrayList<>();
Address addr = new Address();
addr.setProvince("广东");
addr.setCity("深圳");
Address addr2 = new Address();
addr2.setProvince("陕西");
addr2.setCity("西安");
list.add(addr);
list.add(addr2);
return Result.success(list);
}
}