2.0.1 新特性介绍: date和date-time

OpenAPI 针对时间定义了两种类型 date 和 date-time。 JavaChassis 在2.0.1之前的版本只支持使用 date-time,而且必须要求使用 java.util.Date 作为运行时类型, 2.0.1 版本扩充了 date 和 date-time 的实现,开发者可以使用更加灵活的方式使用这两种类型。

使用 date

可以在 query, path, body 等参数中使用 date 类型。 date 类型对应的 Java 类型为 java.time.LocalDate。 使用 Spring MVC, 分别采用如下方式定义接口:

  1. @GetMapping(path = "/getLocalDate")
  2. public LocalDate getLocalDate(@RequestParam("date") LocalDate date) {
  3. return date;
  4. }
  5. @GetMapping(path = "/getLocalDate/{date}")
  6. public LocalDate getLocalDatePath(@PathParam("date") LocalDate date) {
  7. return date;
  8. }
  9. @PostMapping(path = "/postLocalDate")
  10. public LocalDate postLocalDate(@RequestBody LocalDate date) {
  11. return date;
  12. }

其中 getLocalDatePath 接口定义生成的 swagger 描述如下:

  1. /getLocalDate/{date}:
  2. get:
  3. operationId: "getLocalDatePath"
  4. parameters:
  5. - name: "date"
  6. in: "path"
  7. required: true
  8. type: "string"
  9. format: "date"
  10. responses:
  11. "200":
  12. description: "response of 200"
  13. schema:
  14. type: "string"
  15. format: "date"

可以看出,date 在网络上是通过 String 进行编码进行传输的, 格式是标准的格式, 比如 2020-02-20

使用 date-time

可以在 query, path, body 等参数中使用 date-time 类型。 date 类型对应的 Java 类型为 java.time.LocalDateTime, 或者 java.util.Date 。 使用 Spring MVC, 分别采用如下方式定义接口:

  1. @GetMapping(path = "/getDate")
  2. public Date getDate(@RequestParam("date") Date date) {
  3. return date;
  4. }
  5. @GetMapping(path = "/getDatePath/{date}")
  6. public Date getDatePath(@PathParam("date") Date date) {
  7. return date;
  8. }
  9. @PostMapping(path = "/postDate")
  10. public Date postDate(@RequestBody Date date) {
  11. return date;
  12. }
  13. @GetMapping(path = "/getLocalDateTime")
  14. public LocalDateTime getLocalDateTime(@RequestParam("date") LocalDateTime date) {
  15. return date;
  16. }
  17. @GetMapping(path = "/getLocalDateTime/{date}")
  18. public LocalDateTime getLocalDateTimePath(@PathParam("date") LocalDateTime date) {
  19. return date;
  20. }
  21. @PostMapping(path = "/postLocalDateTime")
  22. public LocalDateTime postLocalDateTime(@RequestBody LocalDateTime date) {
  23. return date;
  24. }

其中 getLocalDateTimePath 接口定义生成的 swagger 描述如下:

  1. /getLocalDateTime/{date}:
  2. get:
  3. operationId: "getLocalDateTimePath"
  4. parameters:
  5. - name: "date"
  6. in: "path"
  7. required: true
  8. type: "string"
  9. format: "date-time"
  10. responses:
  11. "200":
  12. description: "response of 200"
  13. schema:
  14. type: "string"
  15. format: "date-time"

可以看出,date-time 在网络上是通过 String 进行编码进行传输的, 格式是标准的格式, 比如 2017-07-21T17:32:28Z。 由于 date-time 的标准格式包含特殊字符,在作为 query 或者 path 参数的时候, 需要做好 URL 编解码, 网络上传递的实际内容为 2017-07-21T17%3A32%3A28Z, 比如: http://localhost:8082/dateTime/getLocalDateTime?date=2017-07-21T17%3A32%3A28Z, 作为 body 参数, 内容不会编解码, 是引号包含起来的 String 类型, 比如 "2017-07-21T17:32:28Z"