blob: 3269b665f9261b647620b081965451b5a8e41d78 [file] [log] [blame]
<!DOCTYPE html>
<html class="writer-html5" lang="en" >
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="shortcut icon" href="../../img/favicon.ico" />
<title>2.0.1 新特性介绍: date和date-time - ServiceComb Java Chassis 开发指南</title>
<link rel="stylesheet" href="../../css/theme.css" />
<link rel="stylesheet" href="../../css/theme_extra.css" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.5.0/styles/github.min.css" />
<script>
// Current page data
var mkdocs_page_name = "2.0.1 \u65b0\u7279\u6027\u4ecb\u7ecd\uff1a date\u548cdate-time";
var mkdocs_page_input_path = "featured-topics/features/date-time.md";
var mkdocs_page_url = null;
</script>
<script src="../../js/jquery-3.6.0.min.js" defer></script>
<!--[if lt IE 9]>
<script src="../../js/html5shiv.min.js"></script>
<![endif]-->
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.5.0/highlight.min.js"></script>
<script>hljs.initHighlightingOnLoad();</script>
</head>
<body class="wy-body-for-nav" role="document">
<div class="wy-grid-for-nav">
<nav data-toggle="wy-nav-shift" class="wy-nav-side stickynav">
<div class="wy-side-scroll">
<div class="wy-side-nav-search">
<a href="../../index.html" class="icon icon-home"> ServiceComb Java Chassis 开发指南
</a>
</div>
<div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="Navigation menu">
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../toc.html">目录</a>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../index.html">概述</a>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../start/catalog.html">快速入门</a>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../start/design.html">设计选型参考</a>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../build-provider/definition/service-definition.html">微服务定义</a>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../build-provider/catalog.html">开发服务提供者</a>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../build-consumer/catalog.html">开发服务消费者</a>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../general-development/catalog.html">通用功能开发</a>
</li>
</ul>
<p class="caption"><span class="caption-text">多样化的通信协议功能参考</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../transports/introduction.html">多协议介绍</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../transports/rest-over-servlet.html">REST over Servlet</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../transports/rest-over-vertx.html">REST over Vertx</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../transports/http2.html">REST over HTTP2</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../transports/highway-rpc.html">Highway</a>
</li>
</ul>
<p class="caption"><span class="caption-text">多样化的服务注册与发现功能参考</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../registry/introduction.html">注册发现说明</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../registry/service-center.html">使用服务中心</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../registry/local-registry.html">本地注册发现</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../registry/distributed.html">去中心化注册发现</a>
</li>
</ul>
<p class="caption"><span class="caption-text">管理服务配置</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../config/general-config.html">通用配置说明</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../config/read-config.html">在程序中读取配置信息</a>
</li>
</ul>
<p class="caption"><span class="caption-text">服务治理功能参考</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../references-handlers/intruduction.html">处理链介绍</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../references-handlers/loadbalance.html">负载均衡</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../references-handlers/ratelimit.html">限流</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../references-handlers/router.html">灰度发布</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../references-handlers/fault-injection.html">故障注入</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../references-handlers/governance.html">流量特征治理</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../references-handlers/fail-retry.html">快速失败和重试</a>
</li>
</ul>
<p class="caption"><span class="caption-text">网关功能参考</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../edge/open-service.html">介绍</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../edge/by-servicecomb-sdk.html">使用 Edge Service 做网关</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../edge/zuul.html">使用 `zuul` 和 `spring cloud gateway` 做网关</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../edge/nginx.html">nginx 网关简单介绍</a>
</li>
</ul>
<p class="caption"><span class="caption-text">安全特性参考</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../references-handlers/publickey.html">公钥认证</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../security/tls.html">使用TLS通信</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../security/shi-yong-rsa-ren-zheng.html">使用RSA认证</a>
</li>
</ul>
<p class="caption"><span class="caption-text">服务打包和运行</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../packaging/standalone.html">以standalone模式打包</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../packaging/web-container.html">以WEB容器模式打包</a>
</li>
</ul>
<p class="caption"><span class="caption-text">专题文章</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../using-java-chassis-in-spring-boot/using-java-chassis-in-spring-boot.html">在Spring Boot中使用java chassis</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../features.html">新功能介绍系列文章</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../compatibility.html">兼容问题和兼容性策略</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../upgrading.html">升级指导系列文章</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../performance.html">性能问题分析和调优</a>
</li>
</ul>
<p class="caption"><span class="caption-text">常用配置项参考</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../config-reference/rest-transport-client.html">REST Transport Client 配置项</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../config-reference/config-center-client.html">Config Center Client 配置项</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../config-reference/service-center-client.html">Service Center Client 配置项</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../config-reference/kie-client.html">ServiceComb Kie Client 配置项</a>
</li>
</ul>
<p class="caption"><span class="caption-text">常见问题</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../question-and-answer/faq.html">FAQ</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../question-and-answer/question_answer.html">Q & A</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../question-and-answer/interface-compatibility.html">微服务接口兼容常见问题</a>
</li>
</ul>
</div>
</div>
</nav>
<section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">
<nav class="wy-nav-top" role="navigation" aria-label="Mobile navigation menu">
<i data-toggle="wy-nav-top" class="fa fa-bars"></i>
<a href="../../index.html">ServiceComb Java Chassis 开发指南</a>
</nav>
<div class="wy-nav-content">
<div class="rst-content"><div role="navigation" aria-label="breadcrumbs navigation">
<ul class="wy-breadcrumbs">
<li><a href="../../index.html" class="icon icon-home" alt="Docs"></a> &raquo;</li>
<li>2.0.1 新特性介绍: date和date-time</li>
<li class="wy-breadcrumbs-aside">
</li>
</ul>
<hr/>
</div>
<div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
<div class="section" itemprop="articleBody">
<h1 id="201-datedate-time">2.0.1 新特性介绍: date和date-time</h1>
<p>OpenAPI 针对时间定义了两种类型 <a href="https://swagger.io/docs/specification/data-models/data-types/#string">date 和 date-time</a>
JavaChassis 在2.0.1之前的版本只支持使用 date-time,而且必须要求使用 <code>java.util.Date</code> 作为运行时类型,
2.0.1 版本扩充了 date 和 date-time 的实现,开发者可以使用更加灵活的方式使用这两种类型。</p>
<h2 id="date">使用 date</h2>
<p>可以在 query, path, body 等参数中使用 date 类型。 date 类型对应的 Java 类型为 <code>java.time.LocalDate</code>
使用 Spring MVC, 分别采用如下方式定义接口:</p>
<pre><code class="language-java">@GetMapping(path = &quot;/getLocalDate&quot;)
public LocalDate getLocalDate(@RequestParam(&quot;date&quot;) LocalDate date) {
return date;
}
@GetMapping(path = &quot;/getLocalDate/{date}&quot;)
public LocalDate getLocalDatePath(@PathParam(&quot;date&quot;) LocalDate date) {
return date;
}
@PostMapping(path = &quot;/postLocalDate&quot;)
public LocalDate postLocalDate(@RequestBody LocalDate date) {
return date;
}
</code></pre>
<p>其中 <code>getLocalDatePath</code> 接口定义生成的 swagger 描述如下:</p>
<pre><code class="language-yaml">/getLocalDate/{date}:
get:
operationId: &quot;getLocalDatePath&quot;
parameters:
- name: &quot;date&quot;
in: &quot;path&quot;
required: true
type: &quot;string&quot;
format: &quot;date&quot;
responses:
&quot;200&quot;:
description: &quot;response of 200&quot;
schema:
type: &quot;string&quot;
format: &quot;date&quot;
</code></pre>
<p>可以看出,date 在网络上是通过 String 进行编码进行传输的, 格式是标准的格式, 比如 <code>2020-02-20</code></p>
<h2 id="date-time">使用 date-time</h2>
<p>可以在 query, path, body 等参数中使用 date-time 类型。 date 类型对应的 Java 类型为 <code>java.time.LocalDateTime</code>
或者 <code>java.util.Date</code>
使用 Spring MVC, 分别采用如下方式定义接口:</p>
<pre><code class="language-java">@GetMapping(path = &quot;/getDate&quot;)
public Date getDate(@RequestParam(&quot;date&quot;) Date date) {
return date;
}
@GetMapping(path = &quot;/getDatePath/{date}&quot;)
public Date getDatePath(@PathParam(&quot;date&quot;) Date date) {
return date;
}
@PostMapping(path = &quot;/postDate&quot;)
public Date postDate(@RequestBody Date date) {
return date;
}
@GetMapping(path = &quot;/getLocalDateTime&quot;)
public LocalDateTime getLocalDateTime(@RequestParam(&quot;date&quot;) LocalDateTime date) {
return date;
}
@GetMapping(path = &quot;/getLocalDateTime/{date}&quot;)
public LocalDateTime getLocalDateTimePath(@PathParam(&quot;date&quot;) LocalDateTime date) {
return date;
}
@PostMapping(path = &quot;/postLocalDateTime&quot;)
public LocalDateTime postLocalDateTime(@RequestBody LocalDateTime date) {
return date;
}
</code></pre>
<p>其中 <code>getLocalDateTimePath</code> 接口定义生成的 swagger 描述如下:</p>
<pre><code class="language-yaml">/getLocalDateTime/{date}:
get:
operationId: &quot;getLocalDateTimePath&quot;
parameters:
- name: &quot;date&quot;
in: &quot;path&quot;
required: true
type: &quot;string&quot;
format: &quot;date-time&quot;
responses:
&quot;200&quot;:
description: &quot;response of 200&quot;
schema:
type: &quot;string&quot;
format: &quot;date-time&quot;
</code></pre>
<p>可以看出,date-time 在网络上是通过 String 进行编码进行传输的, 格式是标准的格式, 比如 <code>2017-07-21T17:32:28Z</code>
由于 date-time 的标准格式包含特殊字符,在作为 query 或者 path 参数的时候, 需要做好 URL 编解码,
网络上传递的实际内容为 <code>2017-07-21T17%3A32%3A28Z</code>, 比如: <code>http://localhost:8082/dateTime/getLocalDateTime?date=2017-07-21T17%3A32%3A28Z</code>,
作为 body 参数, 内容不会编解码, 是引号包含起来的 String 类型, 比如 <code>"2017-07-21T17:32:28Z"</code></p>
</div>
</div><footer>
<hr/>
<div role="contentinfo">
<!-- Copyright etc -->
</div>
Built with <a href="https://www.mkdocs.org/">MkDocs</a> using a <a href="https://github.com/readthedocs/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>.
</footer>
</div>
</div>
</section>
</div>
<div class="rst-versions" role="note" aria-label="Versions">
<span class="rst-current-version" data-toggle="rst-current-version">
</span>
</div>
<script>var base_url = '../..';</script>
<script src="../../js/theme_extra.js" defer></script>
<script src="../../js/theme.js" defer></script>
<script src="../../search/main.js" defer></script>
<script defer>
window.onload = function () {
SphinxRtdTheme.Navigation.enable(true);
};
</script>
</body>
</html>