[英]Spring boot API call with multiple @RequestParam
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, 我需要找到日期范围之间的条目,并想在Spring Boot API中进行GET调用,如下所示:
$ curl -X GET http://localhost:8080/api/v1/appointments/findWithRange?start=2018-10-01&end=2018-10-15
I write the GET call, 我写了GET通话,
@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. 似乎我无法调试,因为断点无法捕获。
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 '. &
符号告诉操作系统' 在后台运行curl -X GET http://localhost:8080/api/v1/appointments/findWithRange?start=2018-10-01
This is a reason why end
date is undefined. 这是未定义end
日期的原因。
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"
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. 如果要接收sql.Date,则需要使用自定义反序列化器。 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: SQL日期转换器:
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: 如果要全局反序列化sql.Date,请尝试仅添加此bean:
@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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.