如何在Springboot应用程序中的HTTP响应字段中对JSON字符串进行验证?
我从API中收到以下响应:
{
"id" : 1
"name": name
"body": JSONstring
}
我可以使用RESTTEMPLATE.exchange(url,method,null,model.class)直接将其映射到Java Pojo,如果我的模型如下:
class Model {
int id;
String name;
String body;
}
我还想值得启用JSON字符串在身体中进入对象并实现以下模型:
class Model {
int id;
String name;
Object body;
}
我当前会收到以下错误:
org.springframework.http.converter.httpmessagenotradableException: JSON解析错误:无法构建实例
我在做什么错?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我通过以下课程进行了测试,一切都很好。示例中的JSON字符串也无效 - 这是应该看起来像:
编辑
这是我用来检查JSON字符串是否可以将其序列化到类的实例的代码:
{“ id”:1,“ name”:“ name”:“ name”,“ bodon” :“ jsonstring”}
jsonutils类是杰克逊库上的薄包装。它是我写的开源Java库的一部分。这是 jsonutils jonutils jsonutils javadoc 。如果要使用库 - 它称为mgntutils,可以将其作为或 github> github 带代码源和javadoc。I ran the tests with the following class and everything worked just fine. The Json String in your example is also not valid - here is how it should look like:
Edit
Here is the code that I used to check if Json String could be de-serialized into an instance of the class:
And it prints this:
{"id":1,"name":"name","body":"JSONstring"}
JsonUtils class is a thin wrapper over Jackson library. It is part of Open-source java library written by me. Here is a JsonUtils Javadoc. If you want to use the library - It is called MgntUtils and you can get it as Maven artifact or at Github with code source and Javadoc.