2

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
    }

1 Answer 1

12
@RequestMapping(value="/animals", method=RequestMethod.GET)
@ResponseBody
public Map<String,Animal> showAllAnimals() {
    return animalMap;
}
Sign up to request clarification or add additional context in comments.

1 Comment

I missed the @ResponseBody when I tried something similar earlier. Thanks!

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.