2

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.

6
  • 2
    Are you making sure to quote your & from your shell? Commented Feb 10, 2019 at 8:57
  • What do you mean? Commented Feb 10, 2019 at 9:04
  • 1
    Does the shell instantly return before you get the curl output and print something like [1] 12345? Commented Feb 10, 2019 at 9:25
  • Yes, I just find the API is good and works properly in the Chrome. The issue is only in the terminal. Commented Feb 10, 2019 at 9:31
  • 2
    You need to learn about the subject shell quoting, which is your problem here. Commented Feb 10, 2019 at 9:32

3 Answers 3

5

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"
Sign up to request clarification or add additional context in comments.

2 Comments

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 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.
1

You should specify the @DateTimeFormat

Here you can find more details

Comments

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

2 Comments

Yes, I have tried it after checking the other answer and this doesn't solve the issue, unfortunately.
try with this deserializer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.