java - 在 spring-boot 中将 hashmap 转换为 json 字符串

标签 java json spring spring-boot

我正在创建一个 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/

相关文章:

java - 在 spring 3 quartz 调度程序中我遇到了异常

java - 使用 Commons BeanUtils 时,为特定类的所有子类注册 Converter 的最佳方法是什么?

Java CLI : Cant parse arguments

php - 操作数据的最佳实践

javascript - 覆盖或更改 package.json "homepage"值

java - Spring JPA Hibernate 自动填充审计字段(创建 ID/时间戳等)

java - Spring数据jpa中具有复合PK的表的 native 查询

java - Fedora linux 上的 "JAVA_HOME is set to an invalid directory"gradlew 错误

java - 如何使用实例PathParam创建对象

security - 将 Spring Security 3 与 GWT 2.1 REST/JSON 应用程序集成