【问题标题】:Spring jsonview getParameter with request parameter带有请求参数的Spring jsonview getParameter
【发布时间】:2017-06-30 15:08:29
【问题描述】:

我想将请求参数传递给返回 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"
        }
        ]
      }
    }

我的请求控制器如何将日期传递给方法

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

【问题讨论】:

    标签: json spring rest dao


    【解决方案1】:

    为了解决我创建一个过滤器然后在查找方法之前设置日期

    Session session = sessionFactory.getCurrentSession();
        Filter filter = session.enableFilter("dt");
        filter.setParameter("dt", dt);
        session.enableFetchProfile("dt");
    
    return projectDAO.find(id);   
    

    【讨论】:

      猜你喜欢
      • 2016-05-26
      • 2017-12-04
      • 2016-03-23
      • 1970-01-01
      • 1970-01-01
      • 2020-11-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多