Мне нужно определить карту, где ключом является день, а значением - Список сообщений.В RequestMapping я даю /{year}/{month}/{day}
, но день не обязателен.Как определить карту и вернуть ее в json, чтобы получить json, где дни от 1 до 31, а в днях у меня есть Список объектов, созданных в этот день.Код моего контроллера:
@RequestMapping(value = "/post/{year}/{month}/{day}", method = RequestMethod.GET)
public List<Post> getPostsByDate(@PathVariable("year") int year,
@PathVariable("month") int month,
@PathVariable("day") Optional<Integer> day)
{
if (day.isPresent()){
return postRepository.findAllByCreateDate(year, month, day);
} else return postRepository.findAllByMonthAndYear(year, month);
List<Post> posts = postRepository.findAllByMonthAndYear(year, month);
Map<Date, List<Post>> postMap =
}
и код почтового репозитория:
@Repository
public interface PostRepository extends JpaRepository<Post, Long> {
List<Post> findAllByUser(User user);
@Query("select p from Post p where (year(p.createDate) = ?1 and month(p.createDate) = ?2) order by p.createDate")
List<Post> findAllByMonthAndYear(int year, int month);
@Query("select p from Post p where (year(p.createDate) = ?1 and month(p.createDate) = ?2 and day(p.createDate) = ?3) order by p.createDate")
List<Post> findAllByCreateDate(int year, int month, Optional<Integer> day);
}