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

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

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

问题

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

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

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

  1. HashMap<String, Object> hashMap = new HashMap<>();
  2. HashMap<String, Object> hashMap2 = new HashMap<>();
  3. JSONObject jsonObject = new JSONObject();
  4. jsonObject.put("key", "dummy value");
  5. hashMap2.put("key", "dummy value");
  6. hashMap2.put("new key", jsonObject);
  7. hashMap.put("data", jsonObject);
  8. hashMap.put("data2", hashMap2);
  9. hashMap.put("extra field", "extra value");
  10. return new ResponseEntity<>(hashMap, HttpStatusCode.valueOf(200));

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

  1. {
  2. "data": {
  3. "empty": false,
  4. "mapType": "java.util.HashMap"
  5. },
  6. "data2": {
  7. "new key": {
  8. "empty": false,
  9. "mapType": "java.util.HashMap"
  10. },
  11. "key": "dummy value"
  12. },
  13. "extra field": "extra value"
  14. }
英文:

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:

  1. HashMap&lt;String, Object&gt; hashMap = new HashMap&lt;&gt;();
  2. HashMap&lt;String, Object&gt; hashMap2 = new HashMap&lt;&gt;();
  3. JSONObject jsonObject = new JSONObject();
  4. jsonObject.put(&quot;key&quot;, &quot;dummy value&quot;);
  5. hashMap2.put(&quot;key&quot;, &quot;dummy value&quot;);
  6. hashMap2.put(&quot;new key&quot;, jsonObject);
  7. hashMap.put(&quot;data&quot;, jsonObject);
  8. hashMap.put(&quot;data2&quot;, hashMap2);
  9. hashMap.put(&quot;extra field&quot;, &quot;extra value&quot;);
  10. return new ResponseEntity&lt;&gt;(hashMap, HttpStatusCode.valueOf(200));

And I am getting this in the response:

  1. {
  2. &quot;data&quot;: {
  3. &quot;empty&quot;: false,
  4. &quot;mapType&quot;: &quot;java.util.HashMap&quot;
  5. },
  6. &quot;data2&quot;: {
  7. &quot;new key&quot;: {
  8. &quot;empty&quot;: false,
  9. &quot;mapType&quot;: &quot;java.util.HashMap&quot;
  10. },
  11. &quot;key&quot;: &quot;dummy value&quot;
  12. },
  13. &quot;extra field&quot;: &quot;extra value&quot;
  14. }

答案1

得分: 2

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

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

  1. {
  2. "foo": "bar",
  3. "fooList": ["bar", "bar", "bar"],
  4. "fooObject": { "field": "..." }
  5. }

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

  1. public class MyDTO {
  2. private String foo;
  3. private String[] fooList; // 可以是列表
  4. private FooObject fooObject;
  5. // 获取字段访问权限的getter方法
  6. }
  7. public class FooObject {
  8. private Object object;
  9. // 获取字段访问权限的getter方法
  10. }

然后,要在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:

  1. {
  2. &quot;foo&quot;: &quot;bar&quot;,
  3. &quot;fooList&quot;: [&quot;bar&quot;, &quot;bar&quot;, &quot;bar&quot;],
  4. &quot;fooObject&quot;: { &quot;field&quot;: &quot;...&quot; }
  5. }

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

  1. public class MyDTO {
  2. private String foo;
  3. private String[] fooList; // Can be a list as well
  4. private FooObject fooObject;
  5. // Getters to get access to the fields here
  6. }
  7. public class FooObject {
  8. private Object object;
  9. // Getters to get access to the fields here
  10. }

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:

确定