无法在Spring Boot中正确返回ResponseEntity中的JSONObject。

huangapple go评论115阅读模式
英文:

Can't return JSONObject in the ResponseEntity properly in spring boot

问题

我正在尝试将JSON对象返回到响应实体对象的哈希映射中,以及其他字段,但对于相应值为JSON对象的字段,我得到了其他内容。

我尝试过调试,但响应流中的每个哈希映射都具有所需的响应结构。

我尝试从后端发送了这个:

HashMap<String, Object> hashMap = new HashMap<>();
HashMap<String, Object> hashMap2 = new HashMap<>();
JSONObject jsonObject = new JSONObject();
jsonObject.put("key", "dummy value");
hashMap2.put("key", "dummy value");
hashMap2.put("new key", jsonObject);
hashMap.put("data", jsonObject);
hashMap.put("data2", hashMap2);
hashMap.put("extra field", "extra value");
return new ResponseEntity<>(hashMap, HttpStatusCode.valueOf(200));

但我在响应中得到了这个:

{
    "data": {
        "empty": false,
        "mapType": "java.util.HashMap"
    },
    "data2": {
        "new key": {
            "empty": false,
            "mapType": "java.util.HashMap"
        },
        "key": "dummy value"
    },
    "extra field": "extra value"
}
英文:

I am trying to return the json object into the hashmap of response entity object along with other fields, but I getting something else for the fields where the corresponding value is a json object.

I tried debugging but every hashmap in the response flow was having the required response structure.

I tried sending this from the backend:

HashMap&lt;String, Object&gt; hashMap = new HashMap&lt;&gt;();
        HashMap&lt;String, Object&gt; hashMap2 = new HashMap&lt;&gt;();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put(&quot;key&quot;, &quot;dummy value&quot;);
        hashMap2.put(&quot;key&quot;, &quot;dummy value&quot;);
        hashMap2.put(&quot;new key&quot;, jsonObject);
        hashMap.put(&quot;data&quot;, jsonObject);
        hashMap.put(&quot;data2&quot;, hashMap2);
        hashMap.put(&quot;extra field&quot;, &quot;extra value&quot;);
        return new ResponseEntity&lt;&gt;(hashMap, HttpStatusCode.valueOf(200));

And I am getting this in the response:

{
    &quot;data&quot;: {
        &quot;empty&quot;: false,
        &quot;mapType&quot;: &quot;java.util.HashMap&quot;
    },
    &quot;data2&quot;: {
        &quot;new key&quot;: {
            &quot;empty&quot;: false,
            &quot;mapType&quot;: &quot;java.util.HashMap&quot;
        },
        &quot;key&quot;: &quot;dummy value&quot;
    },
    &quot;extra field&quot;: &quot;extra value&quot;
}

答案1

得分: 2

在Spring Boot中,你不需要将要返回为JSON的对象放在HashMap中。

相反,通常会编写一个DTO(数据传输对象)类,该类类似于你想要返回的JSON。例如,如果想返回:

{
  "foo": "bar",
  "fooList": ["bar", "bar", "bar"],
  "fooObject": { "field": "..." }
}

然后在Java中,可以编写一个DTO类如下:

public class MyDTO {
  private String foo;
  private String[] fooList; // 可以是列表
  private FooObject fooObject;

  // 获取字段访问权限的getter方法
}

public class FooObject {
  private Object object;
  // 获取字段访问权限的getter方法
}

然后,要在Spring Boot中返回这个对象,只需将其设置为ResponseEntity上的对象:return new ResponseEntity<>(new MyDTO(...), HttpStatus.valueOf(200));

我强烈建议你采用创建特定于要返回的响应的DTO类的方法。

英文:

In Spring Boot, you do not have to box the object that you are trying to return as JSON to the client inside of a HashMap.

Instead, typically one writes a DTO (Data Transfer Object) class that resembles the JSON that you want to return. For example, if I want to return:

{
  &quot;foo&quot;: &quot;bar&quot;,
  &quot;fooList&quot;: [&quot;bar&quot;, &quot;bar&quot;, &quot;bar&quot;],
  &quot;fooObject&quot;: { &quot;field&quot;: &quot;...&quot; }
}

Then in Java, I'd write a DTO class as follows:

public class MyDTO {
  private String foo;
  private String[] fooList; // Can be a list as well
  private FooObject fooObject;

  // Getters to get access to the fields here
}

public class FooObject {
  private Object object;
  // Getters to get access to the fields here
}

Then, to return this with Spring Boot, you just set it as the object on the ResponseEntity: return new ResponseEntity&lt;&gt;(new MyDTO(...), HttpStatusCode.valueOf(200));

I would highly suggest that you follow the approach of creating a DTO class specific to the response that you want to return.

huangapple
  • 本文由 发表于 2023年6月15日 01:58:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/76476360.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定