我正在创建一个 spring-boot 应用程序,我希望能够将 hashmap 的全部内容作为 json 字符串返回。我该怎么做?
我的 HashMap 如下所示:
private static final Map<String,Animal> animalMap= new HashMap<String,Animal>();
功能
@RequestMapping(value="/animals", method=RequestMethod.GET)
public String showAllAnimals() {
// In here I want to return the content of my hashmap as a Json String
}
最佳答案
@RequestMapping(value="/animals", method=RequestMethod.GET)
@ResponseBody
public Map<String,Animal> showAllAnimals() {
return animalMap;
}
关于java - 在 spring-boot 中将 hashmap 转换为 json 字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24737237/