我们将请求体加密后,访问后台,在获取applyData时报错。
原代码如下:
public class DataEncrypt2Test { public static void main(String[] args) throws Exception { String pubKey = "省略不写";// 生产批次 StringBuilder sb = new StringBuilder(); sb.append("很长字符串1,省略不写,下同"); sb.append("很长字符穿2"); sb.append("很长字符串3"); sb.append("很长字符串4"); sb.append("很长字符串5"); sb.append("很长字符串6"); sb.append("很长字符串7" ); sb.append("很长字符串8"); sb.append("很长字符串9"); sb.append("很长字符串10"); sb.append("很长字符串11"); String str = sb.toString(); String requestData = "{\n" + "\t\"ycocode\": \"09876543211234567890\",\n" + "\t\"yconame\": \"众益制药\",\n" + "\t\"medicode\": \"B1243253\",\n" + "\t\"mediname\": \"当归\",\n" + "\t\"allowname\": \"张山\",\n" + "\t\"locality\": \"深圳\",\n" + "\t\"batchno\": \"B345353\",\n" + "\t\"batchciid\": \"3452352\",\n" + "\t\"prodate\": \"2021-10-10\",\n" + "\t\"checkdate\": \"2021-11-11\",\n" + "\t\"packgg\": \"10/箱\",\n" + "\t\"checkname\": \"王五\",\n" + "\t\"norm\": \"国标\",\n" + "\t\"arts\": \"工艺01\",\n" + "\t\"yreport\": \""+ str +"\",\n" + "\t\"updatetime\": \"2021-12-12\",\n" + "\t\"ybatchid\": \"1232141\",\n" + "\t\"quantity\": \"100\",\n" + "\t\"unit\": \"kg\",\n" + "\t\"checkname\": \"王五\",\n" + "\t\"billtype\": \"销售出库\"\n" + "}"; String encrypt = RSAUtils.encryptByPublicKey(requestData, pubKey); System.out.println("requestData===> "); System.out.println(encrypt);
}
}
后台代码:
@RestController @RequestMapping("/api") public class ProducerBatchController { @Autowired private ProduceBatchService produceBatchService; @PostMapping("/pro") @ResponseBody public String saveProduce(@RequestBody JSONObject params) { JSONObject applyData = params.getJSONObject("applyData"); return produceBatchService.saveProduceBatch(applyData); } }
从params中获取applyData时报错:Message is larger than modules
原因分析:由于requestData中有很多\t或\n,并不是纯粹的json字符串,导致fastjson在解析的时候报错。
解决办法,封装成JSONObject
public class DataEncrypt2Test { public static void main(String[] args) throws Exception { String pubKey = "省略不写";// 生产批次 StringBuilder sb = new StringBuilder(); sb.append("很长字符串1,省略不写,下同"); sb.append("很长字符穿2"); sb.append("很长字符串3"); sb.append("很长字符串4"); sb.append("很长字符串5"); sb.append("很长字符串6"); sb.append("很长字符串7" ); sb.append("很长字符串8"); sb.append("很长字符串9"); sb.append("很长字符串10"); sb.append("很长字符串11"); String str = sb.toString(); JSONObject json = new JSONObject(); json.put("ycocode","09876543211234567890"); json.put("yconame","华润制药"); json.put("medicode","B1243253"); json.put("mediname","当归"); json.put("allowname","张山"); json.put("locality","深圳"); json.put("batchno","B345353"); json.put("batchciid","3452352"); json.put("prodate","2021-10-10"); json.put("checkdate","2021-11-11"); json.put("packgg","10/箱"); json.put("checkname","王五"); json.put("norm","国标"); json.put("arts","工艺01"); json.put("yreport",str); json.put("creport",str); json.put("updatetime","2021-12-12"); json.put("ybatchid","1232141"); json.put("unit","kg"); json.put("quantity","100"); json.put("billtype","销售出库"); String encrypt = RSAUtils.encryptByPublicKey(json.toString(), pubKey); System.out.println("requestData===> "); System.out.println(encrypt); } }
问题解决。