带有请求参数的Spring jsonview getParameter

时间:2017-06-30 15:08:30

标签: json spring rest dao

我想通过请求参数传递给返回json属性的方法。如何解决和产生这样的东西:

    {
      "project":{
        "id": 1,
        "cashflow":[{
          date: "2014-09-27T18:30:49-0300",
          value:-1000,
          name:"telecomunication services"
        },{
          date: "2014-09-27T18:40:40-0200",
          value:-2000,
          name:"others cost services"
        }
        ]
      }
    }

我的requestcontroller 如何将日期传递给方法

    @JsonView({Views.PublicWithoutLazy.class})
    @Transactional
    @RequestMapping(value="/project", method=RequestMethod.GET)
    public @ResponseBody Project findProject(Long id, Date dt) {
        //my questions is here. how to pass date to filter cashflow?
        return projectDAO.find(id);    
    }

实体

    @Entity
    Class project{
            @JsonView(Views.PublicWithoutLazy.class)
            @Id
            Long id;
            @MapKeyTemporal(TemporalType.DATE)
            @OneToMany(mappedBy = "cashFlow", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
            @LazyCollection(LazyCollectionOption.EXTRA)
            private Map<Date, List<Cash>> cashflow= new HashMap<>();

           @JsonView(Views.PublicWithoutLazy.class)
           @Transient
           public List<Cash> getCashFlow(Date dt){
              return cashFlow.get(dt);
           }
    }

1 个答案:

答案 0 :(得分:0)

解决我创建过滤器然后在查找方法

之前设置日期
Session session = sessionFactory.getCurrentSession();
    Filter filter = session.enableFilter("dt");
    filter.setParameter("dt", dt);
    session.enableFetchProfile("dt");

return projectDAO.find(id);