如何在Springboot应用程序中的HTTP响应字段中对JSON字符串进行验证?

发布于 2025-01-30 01:28:49 字数 534 浏览 1 评论 0 原文

我从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解析错误:无法构建实例

我在做什么错?

I receive the following response from an API:

{
 "id" : 1
 "name": name
 "body": JSONstring
}

I can map it to a Java POJO directly using restTemplate.exchange(url, method, null, model.class) if my model is as follows:

class Model {
  int id;
  String name;
  String body;
}

However, I want to also deserialize the JSON string in body into an object and achieve the following model:

class Model {
  int id;
  String name;
  Object body;
}

I get the following error currently:

org.springframework.http.converter.HttpMessageNotReadableException:
JSON parse error: Cannot construct instance

What am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

故事与诗 2025-02-06 01:28:49

我通过以下课程进行了测试,一切都很好。示例中的JSON字符串也无效 - 这是应该看起来像:

{"id" : 1,"name": "name","body": "JSONstring"}


package com.mgnt.stam;

public class JsonModel {
    private int id;
    private String name;
    private Object body;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Object getBody() {
        return body;
    }

    public void setBody(Object body) {
        this.body = body;
    }
}

编辑
这是我用来检查JSON字符串是否可以将其序列化到类的实例的代码:

 try {   
    JsonModel model = JsonUtils.readObjectFromJsonString("{\"id\" : 1,\"name\": \"name\",\"body\": \"JSONstring\"}", JsonModel.class);
    System.out.println(JsonUtils.writeObjectToJsonString(model));
 catch(IOException ioe) {
   ....
 }

{“ 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:

{"id" : 1,"name": "name","body": "JSONstring"}


package com.mgnt.stam;

public class JsonModel {
    private int id;
    private String name;
    private Object body;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Object getBody() {
        return body;
    }

    public void setBody(Object body) {
        this.body = body;
    }
}

Edit
Here is the code that I used to check if Json String could be de-serialized into an instance of the class:

 try {   
    JsonModel model = JsonUtils.readObjectFromJsonString("{\"id\" : 1,\"name\": \"name\",\"body\": \"JSONstring\"}", JsonModel.class);
    System.out.println(JsonUtils.writeObjectToJsonString(model));
 catch(IOException ioe) {
   ....
 }

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文