1

Using spring I have this endpoint,

@RequestMapping(value = "/time/{date}", method = RequestMethod.GET)
public ModelRunCollection getPolicyByDate(GetPolicyByDateRequest request) {
    return delegate.submit(request);
}

With the request object looking like this

@Getter
@Setter
public class GetPolicyByDateRequest {

    @DateTimeFormat(pattern = "YYYY-MM-DDTHH:mm:ss.sssZ")
    private DateTime date;

    public GetPolicyByDateRequest date(DateTime date) {
        this.date = date;
        return this;
    }
}

The url I am hitting is this

http://localhost:8080/time/2020-01-17T00:33:53.148Z

And the error I get is

Validation failed for object='getCreditPolicyByDateRequest'. Error count: 1

I haven't been able to understand why this is a problem. I looked at this solution, but seem to be doing the exact thing he mentions

Spring Validation failed for object with date field

lczapski
  • 4,026
  • 3
  • 16
  • 32
sf8193
  • 575
  • 1
  • 6
  • 25
  • It might be related to the following two questions: [how to parse 'following string '20190911T14:37:08.7770400' into date format](https://stackoverflow.com/questions/57912721/how-to-parse-following-string-20190911t143708-7770400-into-date-format) and [Java Date() giving the wrong date \[duplicate\]](https://stackoverflow.com/questions/14836004/java-date-giving-the-wrong-date). – Ole V.V. Jan 17 '20 at 02:22

1 Answers1

1

You can try do in that way. In get request pass only date. And later you can pass it in your class.

@RequestMapping(value = "/time/{date}", method = RequestMethod.GET)
public ModelRunCollection  getPolicyByDate(
        @PathVariable("date")
        @DateTimeFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")
                DateTime date) {
    return delegate.submit(new GetPolicyByDateRequest(date));
}

For additional example you can check this out.

lczapski
  • 4,026
  • 3
  • 16
  • 32
  • 1
    this causes this exception ```nested exception is java.lang.IllegalArgumentException: Invalid format: "2020-01-17T00:33:53" is too short``` for this pattern ```yyyy-MM-dd'T'HH:mm:ss.sss'Z'"``` when the input is 2020-01-17T00:33:53.148Z – sf8193 Jan 17 '20 at 17:39
  • @sf8193 I have changed the pattern for `yyyy-MM-dd'T'HH:mm:ss.SSS'Z'`, tested for ` 2020-01-17T00:33:53.148Z` and it works. – lczapski Jan 20 '20 at 05:41