简体   繁体   中英

How to add name to List in jSON Array, posting response using springboot

i am build an json data using springBoot. I want data in another way i'm getting this reponse

[
    {
        "id": "Spring",
        "name": "Spring FrameWork",
        "description": "Spring Frame work descreption"
    },
    {
        "id": "java",
        "name": "java FrameWork",
        "description": "Java Frame work descreption"
    },
    {
        "id": "javascript",
        "name": "javascript FrameWork",
        "description": "java script Frame work descreption"
    }
]

i want reponse like this

"topics":[
    {
        "id": "Spring",
        "name": "Spring FrameWork",
        "description": "Spring Frame work descreption"
    },
    {
        "id": "java",
        "name": "java FrameWork",
        "description": "Java Frame work descreption"
    },
    {
        "id": "javascript",
        "name": "javascript FrameWork",
        "description": "java script Frame work descreption"
    }
]

my spring boot code is:

@RequestMapping("/topics")
    public List<Topic> getAllTopics() { 
        return topicService.getAllTopics();

    }

my model of topic:-

public class Topic {

    private String id;
    private String name;
    private String description;

    public Topic() {

    }

    public Topic(String id, String name, String description) {
        super();
        this.id = id;
        this.name = name;
        this.description = description;
    }

    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getDescription() {
        return description;
    }
    public void setDescription(String description) {
        this.description = description;
    }

}

What i need to add in code so that JSON data should should contain the name of that List. This can be done using JSON data object. But is there any other way? Please answer my question

Create a Map and return

@RequestMapping("/topics")
public Map<String, List<Topic>> getAllTopics() { 
   Map<String, List<Topic>> response = new HashMap<String, List<Topic>>();
   response.put("topics", topicService.getAllTopics());
   return response;
}

This is not a valid json.

"topics":[
    {
        "id": "Spring",
        "name": "Spring FrameWork",
        "description": "Spring Frame work descreption"
    },
    {
        "id": "java",
        "name": "java FrameWork",
        "description": "Java Frame work descreption"
    },
    {
        "id": "javascript",
        "name": "javascript FrameWork",
        "description": "java script Frame work descreption"
    }
]

As per JSON Docs , json should start with { or [ .

Correct json would be:

{
  "topics": [
    {
      "id": "Spring",
      "name": "Spring FrameWork",
      "description": "Spring Frame work descreption"
    },
    {
      "id": "java",
      "name": "java FrameWork",
      "description": "Java Frame work descreption"
    },
    {
      "id": "javascript",
      "name": "javascript FrameWork",
      "description": "java script Frame work descreption"
    }
  ]
}

And to achieve this format in response:

  1. Create a new model class
public class TopicsData {
    List<Topic> topics;
    // getters and setters
}
  1. Set topics list in TopicsData object
  2. Change topicService or controller code to return TopicsData object instead of List<Topic> .

You can achieve by adding your Topics to another class, Eg: TopicResponse. Refer this as your controller return type:

    public class TopicResponse {
         List<Topic> topics;
        //and getter / setter of topics
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM