1

I am working on passing the list of from and to dates in the rest url.

For eg :

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

Here is my 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"
        }
    ]
}

Could you please guide me how to pass these parameters in postman?

Thanks in advance!

Anuja
  • 19
  • 3
  • Possible duplicate of [Passing an Array or List to @Pathvariable - Spring/Java](https://stackoverflow.com/questions/9623258/passing-an-array-or-list-to-pathvariable-spring-java) – uneq95 Oct 26 '18 at 18:11
  • What is a `DateRange` class? Is it your own class or it's from some framework? Share please its implementation – Yuriy Tsarkov Oct 26 '18 at 18:16
  • DateRange is our class which have fromDate and toDate variables of type Date. – Anuja Oct 26 '18 at 18:29

2 Answers2

0

Can you just add two dates like fromDate and toDate

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

I think a @PathVariable isn't suitable for this purpose then. @PathVariable means that the parameter is a part of the URL. Lets say there is a @PathVariable("id") and a @RequestMapping(value = "/groups/{id}"). Then the id's value will be passed as a parameter {id}. In you case the best way to achive a result is to create some Model class and add into it a list of DateRange as a property: I've just modeled the case and it works fine for me:

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

Request body:

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

Response:

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

Of course the problem of dates deserialization is out of the scene

Yuriy Tsarkov
  • 2,461
  • 2
  • 14
  • 28
  • Hey, I tried doing similar way but I am getting error in postman saying its syntactically incorrect, – Anuja Oct 29 '18 at 19:15
  • Well I think you are doing something in the wrong way. Please share a full body's request and a code of a `DateRangeModel` – Yuriy Tsarkov Oct 30 '18 at 06:35
  • I've just modeled the case and it works fine for me. What is a certain error did you get? There must be something else that is leading to the error – Yuriy Tsarkov Oct 30 '18 at 14:00
  • So it means that you do something wrong in the Postman, sorry, but the code is correct. – Yuriy Tsarkov Oct 30 '18 at 14:34