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

成功!

转载于:https://www.cnblogs.com/yanfeiLiu/p/9792042.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Swagger-YAML是一种用于描述RESTful API的标记语言。它使用YAML格式来编写API的各种信息,包括API的基本信息、路径、操作和其他元数据。 以下是一个Swagger-YAML格式的示例: ```yaml swagger: '2.0' info: version: 1.0.0 title: Pet Store API description: This API provides endpoints for managing a pet store paths: /pets: get: summary: Get all pets produces: - application/json responses: 200: description: Successful operation schema: type: array items: $ref: '#/definitions/Pet' post: summary: Add a new pet consumes: - application/json parameters: - in: body name: pet description: Pet object that needs to be added to the store schema: $ref: '#/definitions/Pet' responses: 201: description: Pet created /pets/{id}: get: summary: Get pet by ID produces: - application/json parameters: - name: id in: path required: true type: integer format: int64 responses: 200: description: Successful operation schema: $ref: '#/definitions/Pet' put: summary: Update pet by ID consumes: - application/json parameters: - name: id in: path required: true type: integer format: int64 - in: body name: pet description: New details of the pet schema: $ref: '#/definitions/Pet' responses: 200: description: Pet updated 404: description: Pet not found definitions: Pet: type: object properties: id: type: integer format: int64 name: type: string ``` 上述示例中,我们定义了一个名为Pet Store API的RESTful API,并定义了两个路径:/pets和/pets/{id}。每个路径下有对应的操作,如获取所有宠物、添加新宠物、通过ID获取特定宠物和更新特定宠物等。 Swagger-YAML格式使用了一些关键字来描述各种信息,比如info用于提供API的基本信息,paths用于定义各个路径下的操作,definitions用于定义数据模型等。 通过使用Swagger-YAML格式,我们可以清晰地描述API的结构和功能,并可以自动生成API文档、客户端代码和服务器模拟等。这样可以提高团队的协作效率,并方便了解和使用API。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值