如何将带有内容的TreeMap转换为JSON格式Spring REST Controller JAVA

时间:2016-11-07 12:23:46

标签: java json spring

您好我有一个代码,我希望将我的Collection公开为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);
        }
    }

我在访问端点时只获得空{}

1 个答案:

答案 0 :(得分:2)

试,

@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);
        }