Swagger--解决日期格式显示为Unix时间戳格式 UTC格式

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

 

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

问题

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

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

 

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

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

 

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

 

    为了演示个人推断再来一个时间戳字体

    去除后面的3个0为ui

 

这个是测试转换连接: Unix时间戳转换工具  (能够本身测试一下)spa

解决

首先确定是能够直接在后台转换的

这个百度也有的,话很少说上代码

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)完成

成功!

相关文章
相关标签/搜索