如何在Java中的剩余URL中传递from和to日期的列表

时间:2018-10-26 17:58:51

标签: java rest postman

我正在努力在其余网址中传递from和to日期的列表。

例如:

public ResponseEntity<String> periodData(
            @RequestHeader(value = "Authorization") String authorization,
            @PathVariable("partyGroupId") String partyGroupId,
            @RequestBody DateRangeModel dateRangeModel){
               return response;
           }

这是我的DateRangeModel:

public class DateRangeModel {
    @JsonProperty
    private List<DateRange> dateRanges;

    public List<DateRange> getDateRanges() {
        return dateRanges;
    }

    public void setDateRanges(List<DateRange> dateRanges) {
        this.dateRanges = dateRanges;
    }

}

RequestBody:

{
    "dateRanges": [
        {
          "fromDate": "2018-10-26",
          "toDate": "2018-10-29"
        },
        {
          "fromDate": "2018-10-21",
          "toDate": "2018-10-20"
        }
    ]
}

能否请您指导我如何在邮递员中传递这些参数?

谢谢!

2 个答案:

答案 0 :(得分:0)

您可以仅添加两个日期,例如fromDate和toDate

public ResponseEntity<String> periodData(
                @RequestHeader(value = "Authorization") String authorization,
                @PathVariable("partyGroupId") String partyGroupId,
                @PathVariable("fromDate") String fromDate,@PathVariable("toDate") String toDate){
                   return response;
               }

答案 1 :(得分:0)

我认为@PathVariable不适合该目的。 @PathVariable表示参数是URL的一部分。可以说有一个@PathVariable("id")和一个@RequestMapping(value = "/groups/{id}")。然后id的值将作为参数{id}传递。在这种情况下,获得结果的最佳方法是创建一些Model类,并将DateRange的列表作为属性添加到其中: 我刚刚为案例建模,对我来说很好用:

public class DateRange {

  private Date fromDate;
  private Date toDate;

  public Date getFromDate() {
    return fromDate;
  }

  public void setFromDate(Date fromDate) {
    this.fromDate = fromDate;
  }

  public Date getToDate() {
    return toDate;
  }

  public void setToDate(Date toDate) {
    this.toDate = toDate;
  }
}

public class DateRangeModel {
  @JsonProperty
  private List<DateRange> dateRanges;

  public List<DateRange> getDateRanges() {
    return dateRanges;
  }

  public void setDateRanges(List<DateRange> dateRanges) {
    this.dateRanges = dateRanges;
  }

  public DateRangeModel() {
  }
}

  @RequestMapping(
      value = "/testapi/test", method = RequestMethod.PUT,
      produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
  public @ResponseBody DateRangeModel test(
      @RequestBody DateRangeModel data,
      HttpServletRequest request) {
    return data;
  }

请求正文:

{
    "dateRanges": [
        {
          "fromDate": "2018-10-26",
          "toDate": "2018-10-29"
        },
        {
          "fromDate": "2018-10-21",
          "toDate": "2018-10-20"
        }
    ]
}

响应:

{
    "dateRanges": [
        {
            "fromDate": 1540501200000,
            "toDate": 1540760400000
        },
        {
            "fromDate": 1540069200000,
            "toDate": 1539982800000
        }
    ]
}

当然,日期反序列化的问题已不在现场