Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert a hashmap to a json string in spring-boot

I am creating a spring-boot application and I want to be able to return the whole content of a hashmap as a json string. How do I do that?

My hashmap looks like the following:

private static final Map<String,Animal> animalMap= new HashMap<String,Animal>();

The function

@RequestMapping(value="/animals", method=RequestMethod.GET)
    public String showAllAnimals() {

        // In here I want to return the content of my hashmap as a Json String
    }
like image 929
Filip Eriksson Avatar asked Mar 24 '26 19:03

Filip Eriksson


1 Answers

@RequestMapping(value="/animals", method=RequestMethod.GET)
@ResponseBody
public Map<String,Animal> showAllAnimals() {
    return animalMap;
}
like image 192
Dave Syer Avatar answered Mar 27 '26 09:03

Dave Syer