【问题标题】:Spring MVC REST JSON jQuery dynamic parameters for jpa query用于 jpa 查询的 Spring MVC REST JSON jQuery 动态参数
【发布时间】:2014-06-25 14:29:52
【问题描述】:

我需要一些技术输入来解决这个问题:

我想搜索具有不同参数的合约。现在我搜索五个参数:FromDate、EndDate、Season、Name 和 Category。将来应该可以搜索参数的动态方式。所有参数都是合约域对象的值。

  var contract= {fromDate:moment($('#datepickerVon').val(), 'DD-MM-YYYY').format('DD-MM-YYYY'),
           endDate:moment($('#datepickerBis').val(), 'DD-MM-YYYY').format('DD-MM-YYYY'),
           season:$('#season').val(),
           name:$('#name').val(),
           category:$('#category').val()};

      $.ajax({
          url:'/contract/search/',
          dataType: "json",
          type: "GET",
          traditional : true,
          contentType: "application/json",
          data: contract,
          success: function(data) {

          }
          });

我使用了这个控制器方法

    @RequestMapping(value = "/search/", method = RequestMethod.GET, headers = "Accept=application/json")
@ResponseBody
public ResponseEntity<String> getContractFromSearch(
        @RequestParam Map<String, String> allRequestParams, ModelMap model) {List<Vertrag> result = Contract.findAllContractsPerParameter(
            allRequestParams.get("fromDate"),
            allRequestParams.get("endDate"),
            Season.findSeason(allRequestParams.get("season").toUpperCase()),
            Name.findName(allRequestParams.get("name").toUpperCase()),
            Category.findCategory(allRequestParams.get("category").toUpperCase()));
    HttpHeaders headers = new HttpHeaders();
    headers.add("Content-Type", "application/json; charset=utf-8");
    return new ResponseEntity<String>(Contract.toJsonArray(result), headers,
            HttpStatus.OK);
}

Season、Name、Category 是 Contract 的依赖项。因此,对于 jpa 查询,我需要每个对象的完整对象。为此,我想要一种动态的方式,而不是为所有人编写类似的代码。但我很确定还有另一种更好的解决方案。 可能可以使用合同对象(域和 json)本身以及 jpa 查询来做到这一点。

感谢您的意见。

【问题讨论】:

  • 在 Spring MVC 中使用命令对象绑定
  • 感谢您的意见,您能否提供更多信息。谢谢

标签: java jquery json spring jpa


【解决方案1】:

根据您对问题的描述,我猜您可以开发名为 Contract 的命令对象

 class Contract{

   private Date fromDate;   
   private Date endDate;    
   private String season;   
   private String name; 
   private String category;

// Getters and setters 

}

然后您可以将它传递给您的 getContractFromSearch( 方法,如下所示:

 public ResponseEntity<String> getContractFromSearch(
    @ModelAttribute Contract contract, ModelMap model) {

这里的 Contract 对象将由 Spring Binder 从您的 JSON 数据中填充。您可能需要编写日期对象的附加转换逻辑。

【讨论】:

  • 是的,我已经获得了合同域类并尝试了您的示例。在我的情况下,合同没有从 json 获得任何值,但我认为它应该有还是没有?
  • @Patrick - 尝试先只发送季节、名称、类别。如果成功,我们将继续转换 Date 类型。
【解决方案2】:

我试过这个例子,它对我有用。

json 现在看起来像这样:

contract = {fromDate:moment($('#datepickerVon').val(), 'DD-MM-YYYY').format('DD-MM-YYYY'), endDate:moment($('#datepickerVon').val(), 'DD-MM-YYYY').format('DD-MM-YYYY'), Season: {
          season: "SO14"}, name: {name: "peter"}, category:{category:"SomeString"}};

          console.log(contract);
      $.ajax({
          url:'/contracts/search/',
          dataType: "json",
          type: "POST",
          mimeType: 'application/json',
          contentType: "application/json",
          data: JSON.stringify(contract),
          success: function(data) {
              console.log(data);
          }
          }); 

控制器接收如下:

@RequestMapping(value = "/search/", method = RequestMethod.POST, headers = "Accept=application/json")
public ResponseEntity<String> getVertagFromSearch(@RequestBody String json, UriComponentsBuilder uriBuilder){
Contract contract = contract.fromJsonToContract(json);
    //do Stuff
    return new ResponseEntity<String>(Contract.toJsonArray(result), headers, HttpStatus.OK);
}

反序列化在这里:

public static contract fromJsonToContract(String json) {
    return new JSONDeserializer<Contract>().use(Calendar.class, new CalendarTransformer("dd-MM-yyyy HH:mm")).use(null, Contract.class).deserialize(json);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-15
    • 2015-01-27
    • 1970-01-01
    • 2020-01-16
    • 2021-12-06
    • 2018-10-20
    相关资源
    最近更新 更多