1

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

3 Answers 3

4

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;
}

In Java 9+

@RequestMapping("/topics")
public Map<String, List<Topic>> getAllTopics() { 
   return Map.of("topics", topicService.getAllTopics());
}
Sign up to request clarification or add additional context in comments.

Comments

2

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>.

Comments

0

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.