@RequestParam
用来接收路径中?后参数的值
使用Postman发送数据
后端接收
@PostMapping("get")
public void get(@RequestParam String id){
System.out.println("接收id:"+id);
}
@PathVariable
用来接收直接写在路径中的数据
后端controller
@PostMapping("get/{id}")
public void get(@PathVariable String id){
System.out.println("接收id:"+id);
}
@RequestBody
用来接收客户端传送的JSON对象,使用Postman发送JSON对象,
后端controller的接口如下:
@RestController
@CrossOrigin
@RequestMapping("api/")
public class PumpInfoController {
@Autowired
private PumpInfoService pumpInfoService;
@PostMapping("getOne")
public PumpInfo getOne(@RequestBody String pump_id){
JSONObject jsonObject = JSON.parseObject(pump_id);//fastJson解析接收的json对象,解析后为一键值对map
String id = jsonObject.getString("id");//,使用getString("id")获取id的值
return pumpInfoService.getPumpById(Integer.parseInt(id));
}
}
使用@RequestBody更新对象
后端
@PostMapping("updatePump")
public int updatePump(@RequestBody String updateInfo){
//通过反射机制,将接受的json字符串转换为Java对象,
PumpInfo pumpInfo = JSON.parseObject(updateInfo,PumpInfo.class);
return pumpInfoService.updatePump(pumpInfo);
}