0

I have a rest controller which returns JSON in some format but I want the JSON output in a different format .To implement the same I am using Spring REST.I have posted all model and controller classes below.My question is what could I do to get my expected response?

ACTUAL JSON

[
{
    "name": "CategoryName1",
    "sub": [
        {
            "name": "SubCategory1"
        },
        {
            "name": "SubCategory2"
        }
    ]
},
{
    "name": "CategoryName2",
    "sub": [
        {
            "name": "SubCategory3"
        },
        {
            "name": "SubCategory4"
        }
    ]
}

]

EXPECTED JSON

{
    "CategoryName1": ["SubCategory1", "SubCategory2"],
    "CategoryName2": ["SubCategory1", "SubCategory2"]
}

Category

public class Category{

public String name;
public List<Subcategory> sub;
public List<Subcategory> getSub() {
    return sub;
}

public void setSub(List<Subcategory> sub) {
    this.sub = sub;
}

public Category(String name) {
    super();
    this.name = name;
}

}

Subcategory

public class Subcategory{

public String name;

public Subcategory(String name) {
    super();
    this.name = name;
}

}

Controller

@RequestMapping(value = "/dropdown", method = RequestMethod.GET, produces = "application/json")
public @ResponseBody List<Category> dropdown() {
    Subcategory SubCategory1 = new  Subcategory("SubCategory1");
    Subcategory SubCategory2 = new  Subcategory("SubCategory2");
    Subcategory SubCategory3 = new  Subcategory("SubCategory3"); 
    Subcategory SubCategory4 = new  Subcategory("SubCategory4"); 

    Category CategoryName1 = new Category("CategoryName1");
    Category CategoryName2 = new Category("CategoryName2");

    List<Subcategory> subList = new ArrayList<Subcategory>();
    subList.add(SubCategory1);
    subList.add(SubCategory2);
    List<Subcategory> subList2 = new ArrayList<Subcategory>();
    subList2.add(SubCategory3);
    subList2.add(SubCategory4);

    CategoryName1.setSub(subList);
    CategoryName2.setSub(subList2);

    List<Category> cat = new ArrayList<Category>();
    cat.add(CategoryName1);
    cat.add(CategoryName2);

    return cat;
}
0

1 Answer 1

1

You can try like this :

public class Category {
    private Map<String, List<String>> response = new HashMap<>();
    Category(Map<String, List<String>> response){
    this.response = response;
   }
}

Usage: Fill data with Category object and return from rest api.

private Map<String, List<String>> response = new HashMap<>();
 List<String> list = new ArrayList<>();
 list.add("SubCategory1");
 list.add("SubCategory2");
 response.put("CategoryName1", list);
 response.put("CategoryName2", list);
 Category category = new Category(response);
Sign up to request clarification or add additional context in comments.

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.