具有多个@RequestParam的 Spring 启动 API 调用



我需要找到日期范围之间的条目,并希望在Spring Boot API中进行GET调用,如下所示,

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

我写了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);
    }

我收到返回响应,

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

如何正确编写调用?似乎我无法调试,因为断点没有捕获。

你的问题很简单 - 在你的电话中

$ 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/findWithRange?start=2018-10-01"。这就是end日期未定义的原因。

只需用双引号将您的 URL 括起来:

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

您应该指定@DateTimeFormat

在这里您可以找到更多详细信息

如果要接收参数作为日期,则需要定义模式。试试这个:

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

如果你想接收sql。然后日期需要使用自定义反序列化程序。试试这个:

@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 日期转换器:

public class SqlDateConverter extends JsonDeserializer<Date> {
    @Override
    public Date deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
        return Date.valueOf(p.getText());
    }
}

如果要反序列化 sql。全局日期,然后尝试仅添加此 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;
    }

最新更新