簡體   English   中英

帶有多個@RequestParam的Spring Boot API調用

[英]Spring boot API call with multiple @RequestParam

我需要找到日期范圍之間的條目,並想在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日期的原因。

只需將您的網址用雙引號引起來即可:

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

如果要接收參數作為日期,則需要定義模式。 試試這個:

@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.Date,則需要使用自定義反序列化器。 試試這個:

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

您應該指定@DateTimeFormat

在這里您可以找到更多詳細信息

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM