关于Java:使用多个@RequestParam的Spring Boot API调用

Spring boot API call with multiple @RequestParam

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

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

我写了GET通话,

1
2
3
4
5
6
7
8
9
10
11
@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);
    }

我得到了回复,

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

如何正确拨打电话? 似乎我无法调试,因为断点无法捕获。


您的问题很简单-在通话中

1
$ 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:

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


您应该指定@DateTimeFormat

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


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

1
2
3
4
5
6
7
8
9
10
11
@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,则需要使用自定义反序列化器。 试试这个:

1
2
3
4
5
6
7
8
9
10
11
@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日期转换器:

1
2
3
4
5
6
7
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:

1
2
3
4
5
6
7
8
9
10
@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;
    }