英文:
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<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));
And I am getting this in the response:
{
    "data": {
        "empty": false,
        "mapType": "java.util.HashMap"
    },
    "data2": {
        "new key": {
            "empty": false,
            "mapType": "java.util.HashMap"
        },
        "key": "dummy value"
    },
    "extra field": "extra value"
}
答案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:
{
  "foo": "bar",
  "fooList": ["bar", "bar", "bar"],
  "fooObject": { "field": "..." }
}
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<>(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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论