在spring-boot中将哈希图转换为json字符串 [英] Convert a hashmap to a json string in spring-boot
本文介绍了在spring-boot中将哈希图转换为json字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在创建一个spring-boot应用程序,我希望能够将hashmap的全部内容作为json字符串返回.我该怎么办?
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?
我的哈希图如下所示:
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;
}
这篇关于在spring-boot中将哈希图转换为json字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文