错误示范:
@PostMapping("/getCustomerByPhone")
public Result getCustomerByPhone(@RequestBody JSONArray jsonarray){
return Result.success(gettelCustomerService.getCustomerByPhone(parameter));
}
虽然这种方式在debug的时候能读到参数值,但是会在转换格式的时候报错。
正确示范:
@PostMapping("/getCustomerByPhone")
public Result getCustomerByPhone(@RequestBody Parameter parameter){
return Result.success(gettelCustomerService.getCustomerByPhone(parameter));
}
使用Parameter类型接收参数,在Service层使用fastjson解析
String dataStr = parameter.getData();
JSONObject data = JSONObject.parseObject(dataStr);
@Override
public GettelCustomer getCustomerByPhone(Parameter parameter) //Service代码
{
String dataStr = parameter.getData();
JSONObject data = JSONObject.parseObject(dataStr);
String phone = data.get("phone").toString();
GettelCustomer customerByPhone = gettelCustomerCache.getCustomerByPhone(phone);
if (customerByPhone == null) {
throw new BusinessException(ResultEnum.VALIDATE_FAILED, "不存在该客户");
}
return customerByPhone;
}