zoukankan      html  css  js  c++  java
  • Swagger--解决日期格式显示为Unix时间戳格式 UTC格式

    在swagger UI模型架构上,字段日期显示为“日期”:“2018-10-15T09:10:47.507Z”但我需要将其作为“日期”:“2018-9-26 12:18:48”。

    tips:以下这两种格式只是简单了解了一下不是很全面,有不足或不对的地方请指出。 

    问题

    首先先看一下swagger默认显示的Date类型是这样的(这里示例代码默认显示的当前日期的UTC  可能和后面演示的不一样)

    这是标准的 XML Schema的"日期型数据格式”
    T是代表后面跟着“时间”。Z代表0时区,或者叫UTC统一时间(UTC通用标准时)。

    然后运行结果返回JSON数据格式时显示成这样的

    这里字体颜色和图片搞得好恶心只能改变一下字体颜色了  将就看下哈

        这个格式没搞懂到底算是什么数据格式,找到一个叫Unix时间戳(Unix timestamp)的格式挺像的 (区别在于他在后面多添加了3个0  我的理解)

     

        为了演示我的推断再来一个时间戳

        去除后面的3个0为

    这个是测试转换链接: Unix时间戳转换工具  (可以自己测试一下)

    解决

    首先肯定是可以直接在后台转换的

    这个百度也有的,话不多说上代码

        public static void main(String[] args) throws ParseException {
            // write your code here
            String date = "2018-10-15T09:10:47.507Z";
            date = date.replace("Z", " UTC");
            System.out.println(date);
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS Z");
            Date d = format.parse(date);
            System.out.println(d);
    
    
            String dt= String.valueOf(d);
            SimpleDateFormat sdf1= new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy", Locale.ENGLISH);
            SimpleDateFormat sdf2= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            System.out.println(sdf2.format(sdf1.parse(dt)));
        }
    

      运行结果为

    @ApiModelProperty

    但是Swagger提供了一个注解可以直接搞定——@ApiModelProperty

    作用范围API使用位置
    对象属性 @ApiModelProperty 用在出入参数对象的字段上

    @ApiModelProperty()用于方法,字段; 表示对model属性的说明或者数据操作更改 

    属性说明:
          value–字段说明 
          name–重写属性名字 
          dataType–重写属性类型 
          required–是否必填 
          example–举例说明 (此示例用它)
          hidden–隐藏

    swagger的@ApiModelPreporty具有名为“example”的属性,在2.3.0之前该属性没有做任何事情。从版本2.3.0版本开始,这个“示例”开始工作。

    下面看一下效果

         private static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
    	
    	@ApiModelProperty(required = true,example = "2018-10-01 12:18:48")
    	@JsonFormat(pattern = DATE_FORMAT)
    	@Column(name="task_reality_endtime")
    	private Date taskRealityEndtime;	//实际结束时间

    将Date属性字段添加@ApiModelProperty注解

    添加之后运行   swagger显示为@ApiModelProperty的example值

    但是运行时出现请求错误

    错误信息为

    11:45:42.962 [http-nio-8080-exec-5] WARN  o.s.w.s.m.s.DefaultHandlerExceptionResolver - Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: Could not read document: Can not deserialize value of type java.util.Date from String "2018-10-01 12:18:48": not a valid representation (error: Failed to parse Date value '2018-10-01 12:18:48': Can not parse date "2018-10-01 12:18:48": while it seems to fit format 'yyyy-MM-dd'T'HH:mm:ss.SSS', parsing fails (leniency? null))
     at [Source: (PushbackInputStream); line: 24, column: 25] (through reference chain: com.cn.shrichen.web.worklist.entity.Detail["taskRealityEndtime"]); nested exception is com.fasterxml.jackson.databind.exc.InvalidFormatException: Can not deserialize value of type java.util.Date from String "2018-10-01 12:18:48": not a valid representation (error: Failed to parse Date value '2018-10-01 12:18:48': Can not parse date "2018-10-01 12:18:48": while it seems to fit format 'yyyy-MM-dd'T'HH:mm:ss.SSS', parsing fails (leniency? null))
     at [Source: (PushbackInputStream); line: 24, column: 25] (through reference chain: com.cn.shrichen.web.worklist.entity.Detail["taskRealityEndtime"])
    

      json格式为yyyy-MM-dd HH:mm:ss

      Date类型默认为yyyy-MM-dd

    解决:在Date字段上添加@JsonFormat(pattern = DATE_FORMAT)完成

    成功!

  • 相关阅读:
    存储过程分页
    SQL內置Function游标函数
    SQL 2000中的触发器使用
    使用.NET自带的功能制作简单的注册码
    在ASP.NET里轻松实现缩略图
    推荐几个用得上且免费的 .NET控件
    SQL內置Function日期和时间函数
    常用的asp代碼和javascript代碼
    SQL內置Function元数据函数
    數據庫中代@@的參數說明
  • 原文地址:https://www.cnblogs.com/yanfeiLiu/p/9792042.html
Copyright © 2011-2022 走看看