1

I need to find the entries between a date range and would like to make a GET call in the Spring Boot API as follow,

$ curl -X GET http://localhost:8080/api/v1/appointments/findWithRange?start=2018-10-01&end=2018-10-15

I write the GET call,

@GetMapping("/findWithRange")
    public ResponseEntity<List<Appointment>> findAllWithCreationRange(@RequestParam("start")  Date start, @RequestParam("end") Date end) {

        List<Appointment> appointments = service.findAllWithCreationRange(start, end);

        if (Objects.isNull(appointments)) {
            ResponseEntity.badRequest().build();
        }

        return ResponseEntity.ok(appointments);
    }

I get the return response,

{"timestamp":"2019-02-10T07:58:22.151+0000","status":400,"error":"Bad Request","message":"Required Date parameter 'end' is not present","path":"/api/v1/appointments/findWithRange"}

How do I write the call properly? It seems I was not been able to debug as the breakpoints don't catch.

Arefe
  • 11,321
  • 18
  • 114
  • 168

3 Answers3

3

Your problem is very simple - in your call

$ curl -X GET http://localhost:8080/api/v1/appointments/findWithRange?start=2018-10-01&end=2018-10-15

& sign tell operation system 'run curl -X GET http://localhost:8080/api/v1/appointments/findWithRange?start=2018-10-01 in background'. This is a reason why end date is undefined.

Just surround your URL in double quotes:

$ curl -X GET "http://localhost:8080/api/v1/appointments/findWithRange?start=2018-10-01&end=2018-10-15"
Bor Laze
  • 2,458
  • 12
  • 20
  • Yes, simple but took me like 1.5 hours to try various options and read all the code until I put the same command in the Chrome (which worked) :| – Arefe Feb 10 '19 at 09:38
  • @Arefe In the future, pay close attention to your output. In this case, `curl -X` would have shown you that it was only passing the first request parameter, which would have been a big hint. – chrylis -cautiouslyoptimistic- Feb 10 '19 at 12:17
1

If you want to receive parameter as date then there need to define pattern. Try with this:

@GetMapping("/findWithRange")
    public ResponseEntity<List<Appointment>> findAllWithCreationRange(@RequestParam("start") @DateTimeFormat(pattern = "yyyy-MM-dd") Date start, @RequestParam("end") @DateTimeFormat(pattern = "yyyy-MM-dd") Date end) {

        List<Appointment> appointments = service.findAllWithCreationRange(start, end);

        if (Objects.isNull(appointments)) {
            ResponseEntity.badRequest().build();
        }

        return ResponseEntity.ok(appointments);
    }

If you want to recevie sql.Date then need to use custom deserializer. Try with this:

@GetMapping("/findWithRange")
        public ResponseEntity<List<Appointment>> findAllWithCreationRange(@RequestParam("start") @JsonDeserialize(using = SqlDateConverter.class) Date start, @RequestParam("end") @JsonDeserialize(using = SqlDateConverter.class) Date end) {

            List<Appointment> appointments = service.findAllWithCreationRange(start, end);

            if (Objects.isNull(appointments)) {
                ResponseEntity.badRequest().build();
            }

            return ResponseEntity.ok(appointments);
        }

Sql date converter:

public class SqlDateConverter extends JsonDeserializer<Date> {

    @Override
    public Date deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
        return Date.valueOf(p.getText());
    }
}

If you want to deserialize sql.Date globally then try with add this bean only:

@Bean
    public Jackson2ObjectMapperBuilder configureObjectMapper() {
        Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
        ObjectMapper objectMapper = new ObjectMapper();
        SimpleModule module = new SimpleModule();
        module.addDeserializer(Date.class,new SqlDateConverter());
        objectMapper.registerModule(module);
        builder.configure(objectMapper);
        return builder;
    }
GolamMazid Sajib
  • 8,698
  • 6
  • 21
  • 39
1

You should specify the @DateTimeFormat

Here you can find more details

Hakob Hakobyan
  • 1,111
  • 8
  • 15