问题背景
在接口开发过程中,有一个新增接口,需要支持批量新增数据,这时入参就需要用到json格式数据,且包含list集合,比如这样的数据格式(整个数据是String),接口连接类似:http://www.test.com/url?data=urlEncode(String)
{"sign":"11111111111111","courseList":[{"courseCategory":"1021","courseSort":21,"courseTypeTwo":22,"courseLevel":"1","courseYear":"2023","isFreeCourse":0,"courseDuration":"142","courseId":"2023306111","courseTeacher":"张克1","courseCredit":"10.00","courseName":"中国特色社会主义思想"}],"schoolCode":"wx0001"}
通过调用接口传参之后需要将接收到的参数进行转换并用于后续业务逻辑处理,简单的说也就是将接收到的String转换成JSONObject,代码示例如下
publicstaticvoidmain(String[] args) { StringjsonString="{\"sign\":\"11111111111111\",\"courseList\":[{\"courseCategory\":\"1021\",\"courseSort\":21,\"courseTypeTwo\":22,\"courseLevel\":\"1\",\"courseYear\":\"2023\",\"isFreeCourse\":0,\"courseDuration\":\"142\",\"courseId\":\"2023306111\",\"courseTeacher\":\"张克1\",\"courseCredit\":\"10.00\",\"courseName\":\"中国特色社会主义思想\"}],\"schoolCode\":\"wx0001\"}"; System.out.println(jsonString); JSONObjectjsonObject=JSON.parseObject(jsonString); System.out.println(JSON.toJSONString(jsonObject)); }
执行main函数后的打印结果如下
{"sign":"11111111111111","courseList":[{"courseCategory":"1021","courseSort":21,"courseTypeTwo":22,"courseLevel":"1","courseYear":"2023","isFreeCourse":0,"courseDuration":"142","courseId":"2023306111","courseTeacher":"张克1","courseCredit":"10.00","courseName":"中国特色社会主义思想"}],"schoolCode":"wx0001"} {"sign":"11111111111111","courseList":[{"courseYear":"2023","courseName":"中国特色社会主义思想","courseLevel":"1","courseDuration":"142","courseTypeTwo":22,"courseCredit":"10.00","isFreeCourse":0,"courseTeacher":"张克1","courseId":"2023306111","courseCategory":"1021","courseSort":21}],"schoolCode":"wx0001"}
这里可以很明显的看到经过JSON.parseObject(jsonString)后的JSONObject再转换成String打印之后和之前的字符串参数顺序不一样了,那么通常情况下如果业务本身不关注参数顺序的话是没有影响的,但是接口调用时需要调用双方两边保持同样的入参顺序进行(入参+密钥+时间戳)的MD5加密,这样就会导致两边MD5后的字符串不一致,那么如何处理呢?
解决方案
这种情况下,想要保证同样的入参同样的加密方式同样的加密密钥加密出来的字符串一致的话,那就需要保证在接收到的入参字符串与传过来的入参一致,fastjson支持这样的属性Feature.OrderedField,调整代码
publicstaticvoidmain(String[] args) { StringjsonString="{\"sign\":\"11111111111111\",\"courseList\":[{\"courseCategory\":\"1021\",\"courseSort\":21,\"courseTypeTwo\":22,\"courseLevel\":\"1\",\"courseYear\":\"2023\",\"isFreeCourse\":0,\"courseDuration\":\"142\",\"courseId\":\"2023306111\",\"courseTeacher\":\"张克1\",\"courseCredit\":\"10.00\",\"courseName\":\"中国特色社会主义思想\"}],\"schoolCode\":\"wx0001\"}"; System.out.println(jsonString); JSONObjectjsonObject=JSON.parseObject(jsonString, Feature.OrderedField); System.out.println(JSON.toJSONString(jsonObject)); }
执行结果
{"sign":"11111111111111","courseList":[{"courseCategory":"1021","courseSort":21,"courseTypeTwo":22,"courseLevel":"1","courseYear":"2023","isFreeCourse":0,"courseDuration":"142","courseId":"2023306111","courseTeacher":"张克1","courseCredit":"10.00","courseName":"中国特色社会主义思想"}],"schoolCode":"wx0001"} {"sign":"11111111111111","courseList":[{"courseCategory":"1021","courseSort":21,"courseTypeTwo":22,"courseLevel":"1","courseYear":"2023","isFreeCourse":0,"courseDuration":"142","courseId":"2023306111","courseTeacher":"张克1","courseCredit":"10.00","courseName":"中国特色社会主义思想"}],"schoolCode":"wx0001"}
这里可以看到的入参和接收后数据字符串顺序完全一样了。