报错
最近在学习python
使用python requests包发送请求到java springboot服务,报错:
Request processing failed; nested exception is org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot construct instance of com.platon.contract.dto.req.Arc721MintReq
(although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value
以下为部分堆栈信息
2021-04-16 14:08:17.357 ERROR 14244 --- [nio-8080-exec-5] o.a.c.c.C.[.[.[.[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [/contract-tool-api] threw exception [Request processing failed; nested exception is org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot construct instance of `com.platon.contract.dto.req.Arc721MintReq` (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('{"ownerPrivateKey": "c5f398ccf4ffa0dc19ae35bf2d1faf9b5d08f78ba5af0a6d22e46c7c59b8ad89", "contractAddress": "atp1a4dgjpzyhjw0q9cu8zsmru02kmt7m0aeq0f86f", "toAddress": "atp15mjtmjrra50v9542pacmpfcynp32tzjre7n9kc", "tokenId": "22816030036766234464678685168595911160232204275235867565159828658153005216337", "url": "http://58.251.94.107:29001/download/meta/10-The Fifth Elemente.json"}'); nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of `com.platon.contract.dto.req.Arc721MintReq` (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('{"ownerPrivateKey": "c5f398ccf4ffa0dc19ae35bf2d1faf9b5d08f78ba5af0a6d22e46c7c59b8ad89", "contractAddress": "atp1a4dgjpzyhjw0q9cu8zsmru02kmt7m0aeq0f86f", "toAddress": "atp15mjtmjrra50v9542pacmpfcynp32tzjre7n9kc", "tokenId": "22816030036766234464678685168595911160232204275235867565159828658153005216337", "url": "http://58.251.94.107:29001/download/meta/10-The Fifth Elemente.json"}')
at [Source: (PushbackInputStream); line: 1, column: 1]] with root cause
com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of `com.platon.contract.dto.req.Arc721MintReq` (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('{"ownerPrivateKey": "c5f398ccf4ffa0dc19ae35bf2d1faf9b5d08f78ba5af0a6d22e46c7c59b8ad89", "contractAddress": "atp1a4dgjpzyhjw0q9cu8zsmru02kmt7m0aeq0f86f", "toAddress": "atp15mjtmjrra50v9542pacmpfcynp32tzjre7n9kc", "tokenId": "22816030036766234464678685168595911160232204275235867565159828658153005216337", "url": "http://58.251.94.107:29001/download/meta/10-The Fifth Elemente.json"}')
at [Source: (PushbackInputStream); line: 1, column: 1]
at com.fasterxml.jackson.databind.exc.MismatchedInputException.from(MismatchedInputException.java:63) ~[jackson-databind-2.11.0.jar:2.11.0]
.
..
at java.lang.Thread.run(Thread.java:748) [na:1.8.0_212]
python客户端代码
request_url = "http://localhost:8080/contract-tool-api/arc721/mint?nodeUrl=http://xxx&chainId=201018" \
"&hrp=atp"
data = {
"ownerPrivateKey": "xxx",
"contractAddress": "atp1a4dgjpzyhjw0q9cu8zsmru02kmt7m0aeq0f86f",
"toAddress": "atp15mjtmjrra50v9542pacmpfcynp32tzjre7n9kc",
"tokenId": "22306936569964232886809895325545608475345079154389659227883238505938709395303",
"url": "xxx"
}
#将python对象编码成Json字符串
data_json = json.dumps(data, ensure_ascii=False)
print(data_json)
# 发请求
response = requests.post(request_url, json=data_json)
Java服务端代码
/**
* 铸币
* @param arc721MintReq
* @return
*/
@PostMapping("/mint")
public BaseResult<TransactionReceipt> mint(@RequestBody @Validated Arc721MintReq arc721MintReq){
String privateKey = arc721MintReq.getOwnerPrivateKey();
String contractAddr = arc721MintReq.getContractAddress();
String toAddress = arc721MintReq.getToAddress();
BigInteger tokenId = arc721MintReq.getTokenId();
String url = arc721MintReq.getUrl();
TransactionReceipt mint = arc721Service.mint(privateKey, contractAddr,toAddress,tokenId, url);
return BaseResult.success(mint);
}
问题原因
spring 的参数解析器无法将传送过来的字符串解析为java对象,是因为python的json.dumps方法将json数据多包了一层。
'{"ownerPrivateKey": "c5f398ccf4ffa0dc19ae35bf2d1faf9b5d08f78ba5af0a6d22e46c7c59b8ad89", "contractAddress": "atp1a4dgjpzyhjw0q9cu8zsmru02kmt7m0aeq0f86f", "toAddress": "atp15mjtmjrra50v9542pacmpfcynp32tzjre7n9kc", "tokenId": "22808825499565028968868482583468432861755947061565311889789731560024519690314", "url": "http://58.251.94.107:29001/download/meta/1-Last and First Men.json"}'); nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of `com.platon.contract.dto.req.Arc721MintReq` (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('{"ownerPrivateKey": "c5f398ccf4ffa0dc19ae35bf2d1faf9b5d08f78ba5af0a6d22e46c7c59b8ad89", "contractAddress": "atp1a4dgjpzyhjw0q9cu8zsmru02kmt7m0aeq0f86f", "toAddress": "atp15mjtmjrra50v9542pacmpfcynp32tzjre7n9kc", "tokenId": "22808825499565028968868482583468432861755947061565311889789731560024519690314", "url": "http://58.251.94.107:29001/download/meta/1-Last and First Men.json"}'
#将python对象编码成Json字符串
data_json = json.dumps(data, ensure_ascii=False)
解决方法
就是去掉json.dumps,直接传输data对象过去就行了。