我使用Java API,并获得GET调用的HTTP状态400。展示了该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);
}
答案 0 :(得分:1)
基本上,您不是在提供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);
}