Apache ServiceComb Java Chassis 2.0.1 新特性重磅出击:date和date-time

cb10e0f758bd3487d4349974862b7e99.jpeg

作者 | LiuBao

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, 分别采用如下方式定义接口:

@GetMapping(path = "/getLocalDate")
public LocalDate getLocalDate(@RequestParam("date") LocalDate date) {
return date;
}


@GetMapping(path = "/getLocalDate/{date}")
public LocalDate getLocalDatePath(@PathParam("date") LocalDate date) {
return date;
}


@PostMapping(path = "/postLocalDate")
public LocalDate postLocalDate(@RequestBody LocalDate date) {
return date;
}

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

/getLocalDate/{date}:
get:
  operationId: "getLocalDatePath"
  parameters:
  - name: "date"
    in: "path"
    required: true
    type: "string"
    format: "date"
  responses:
    "200":
      description: "response of 200"
      schema:
        type: "string"
        format: "date"

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

使用 date-time

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

@GetMapping(path = "/getDate")
public Date getDate(@RequestParam("date") Date date) {
return date;
}


@GetMapping(path = "/getDatePath/{date}")
public Date getDatePath(@PathParam("date") Date date) {
return date;
}


@PostMapping(path = "/postDate")
public Date postDate(@RequestBody Date date) {
return date;
}


@GetMapping(path = "/getLocalDateTime")
public LocalDateTime getLocalDateTime(@RequestParam("date") LocalDateTime date) {
return date;
}


@GetMapping(path = "/getLocalDateTime/{date}")
public LocalDateTime getLocalDateTimePath(@PathParam("date") LocalDateTime date) {
return date;
}


@PostMapping(path = "/postLocalDateTime")
public LocalDateTime postLocalDateTime(@RequestBody LocalDateTime date) {
return date;
}

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

/getLocalDateTime/{date}:
get:
  operationId: "getLocalDateTimePath"
  parameters:
  - name: "date"
    in: "path"
    required: true
    type: "string"
    format: "date-time"
  responses:
    "200":
      description: "response of 200"
      schema:
        type: "string"
        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"

如您对开源开发、微服务感兴趣

欢迎扫描下方二维码添加

ServiceComb小助手

咱们一起做点有意思的事情~

欢迎扫码

关注精彩!

您点的每个赞,我都认真当成了喜欢

点击下方“阅读原文”查看更多ServiceComb小知识☺

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值