«status»: 400, «error»: «Bad Request», «message»: «Обязательный параметр даты end» отсутствует » - PullRequest
0 голосов
/ 10 февраля 2019

Я работаю с Java API и получаю HTTP-статус 400 для вызова GET.API представлен,

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

Я звоню cURL GET,

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

Я получаю ответ,

{"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"}

Это говорит о том, чтоПараметр end не представлен в вызове, что кажется неверным.Хранилище и класс обслуживания также предоставляются,

@Repository
public interface AppointmentRepository extends CrudRepository<Appointment, Long> {

@Query(value = "SELECT * FROM Appointment WHERE appointment_date <= :creationDateTime", nativeQuery = true)
List<Appointment> findAllWithCreationDateTimeBefore(@Param("creationDateTime") Date creationDateTime);


@Query(value = "SELECT * FROM Appointment WHERE appointment_date >= :startDate AND appointment_date <= :endDate", nativeQuery = true)
List<Appointment> findAllWithCreationRange(@Param("startDate") Date startDate, @Param("endDate") Date endDate);
}

Класс обслуживания,

@Service
public class AppointmentService {
@Autowired
private AppointmentRepository repository;

public Optional<Appointment> findById(Long id) {
    return repository.findById(id);
}

public List<Appointment> findAll() {
    return (List<Appointment>) repository.findAll();
}

public List<Appointment> findAllWithCreationDateTimeBefore(Date date) {
    return (List<Appointment>) repository.findAllWithCreationDateTimeBefore(date);
}

public List<Appointment> findAllWithCreationRange(Date start, Date end) {
    return (List<Appointment>) repository.findAllWithCreationRange(start, end);
}

public Appointment save(Appointment appointment) {
    return repository.save(appointment);
}

public void deleteById(Long id) {
    repository.deleteById(id);
}

public void deleteAll() {
    repository.deleteAll();
}
}

Я также пытался сделать вызов с помощью &&,

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

и это возвращает тот же ответ и не помогает.Что мне здесь не хватает и чтобы правильно бегать?

Примечание: у меня есть аналогичная конечная точка, которая отлично работает, что сделало меня более любопытным.

// curl -X GET http://localhost:8080/api/v1/appointments/creationDateTime?start=2018-10-01 | jq

    @GetMapping("/creationDateTime")
    public ResponseEntity<List<Appointment>> findAllWithCreationDateTimeBefore(@RequestParam("start") Date date) {

        List<Appointment> appointment = service.findAllWithCreationDateTimeBefore(date);

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

        return ResponseEntity.ok(appointment);
    }

1 Ответ

0 голосов
/ 10 февраля 2019

Как правило, вы не поддерживаете DateFormat, без которого String не может быть преобразована в дату.

Используйте @DateTimeFormat("yyyy-MM-dd"), например:

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

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

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

    return ResponseEntity.ok(appointments);
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...