java - 如何将包含内容的 TreeMap 转换为 JSON 格式 Spring REST Controller JAVA

标签 java json spring

您好,我有一个代码,我想将我的集合公开为 JSON 格式。我以为Spring会自动完成它。这是代码:

@RestController
    public class BirthdayController  extends BaseController{

        @RequestMapping("/api/v1/birthday")
        public TreeMap<Integer, TreeMap<Integer, ArrayList<BirthdayDetails>>> getBirthdays(@RequestParam(value="startYear") Integer startYear,
                                  @RequestParam(value="startMonth") Integer startMonth,
                                  @RequestParam(value="endYear") Integer endMonth,
                                  @RequestParam(value="endMonth") Integer endYear) {
            BirthdayServiceImpl birthdayService = new BirthdayServiceImpl();
            return birthdayService.getBirthdaysBetweenDate(startMonth, startYear, endMonth, endYear);
        }
    }

访问端点时我只得到空的 {}

最佳答案

尝试,

@RequestMapping("/api/v1/birthday")
        public @ResponseBody TreeMap<Integer, TreeMap<Integer, ArrayList<BirthdayDetails>>> getBirthdays(@RequestParam(value="startYear") Integer startYear,
                                  @RequestParam(value="startMonth") Integer startMonth,
                                  @RequestParam(value="endYear") Integer endMonth,
                                  @RequestParam(value="endMonth") Integer endYear) {
            BirthdayServiceImpl birthdayService = new BirthdayServiceImpl();
            return birthdayService.getBirthdaysBetweenDate(startMonth, startYear, endMonth, endYear);
        }

关于java - 如何将包含内容的 TreeMap 转换为 JSON 格式 Spring REST Controller JAVA,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40465118/

相关文章:

java - .reversed() 无法在 blueJ 中编译

java - 如何为 @Autowire 字段提供不同的 bean,条件是拥有对象(也是 @Autowired)上的注释?

AngularJS 406 Not Acceptable http 发布请求

java - 为什么在普通 Java 文件中不会调用 InitializingBean 方法

java - 所有 Spring 管理的 Bean 的列表

spring - 由于 Spring 5 中的 Access/CORS 问题,无法访问后端的 URL

java - 在 Java 应用程序中使用 psql 导入 sql 文件

java - 如何在Java套接字编程中解析index.html文件中包含的style.css的路径

php - 在 PHP 中获取 Json 表单数据

javascript - 将 HTML 表单输入以 JSON 格式保存到本地文件(如果可能,使用 JavaScript)