swagger--resolution date format displayed as Unix timestamp format UTC format

Source: Internet
Author: User



On the Swagger UI model schema, the field date is displayed as "date": "2018-10-15t09:10:47.507z" but I need to use it as "date": "2018-9-26 12:18:48".






Tips: The following two formats are just simple to understand a bit is not very comprehensive, there is insufficient or wrong place please point out.


Problem


First look at Swagger the date type is displayed by default (the current date that the sample code displays by default may not be the same as the one shown later)






This is the "Date data format" for the standard XML schema
T is the representative followed by "Time". Z represents the 0 time zone, or UTC unified Time (UTC Universal Standard).






The results are then returned to the JSON data format as shown in this






Here the font color and pictures made good disgusting can only change the font color, pay attention to see HA






This format does not understand exactly what the data format, find a UNIX timestamp (Unix timestamp) format very similar (the difference is that he added 3 more 0 in the back of my understanding)






To demonstrate my inference, one more time stamp.






Remove the back of the 3 x 0 for












This is the test conversion link: unix Timestamp conversion tool (you can test it yourself)


Solve


First of all, it can be converted directly in the background.



This Baidu also has, words not much to say on the code


   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)));
    }


Run the result as





@ApiModelProperty


But Swagger provides an annotation that can be done directly-@ApiModelProperty


function Range API Use location
Object Properties @ApiModelProperty Used on fields in the Access parameter object


@ApiModelProperty () for a method, a field, a description of the model property, or a change in the data operation



Property Description:
value– Field Description
name– overriding property names
Datatype– overriding property types
required– is required
example– illustration (This example uses it)
hidden– Hidden



The @apimodelpreporty of Swagger has a property named "Example", which does nothing before 2.3.0. starting with version 2.3.0, this "example" starts working.






Here's a look at the effect


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; // Actual end time


Add a Date property field to a @apimodelproperty annotation






Example values that are displayed as @apimodelproperty after you add the swagger run






But there is a request error at run time






The error message is


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"])


The JSON format is YYYY-MM-DD HH:MM:SS



The date type defaults to YYYY-MM-DD



Workaround: Add @jsonformat (pattern = date_format) on the DATE field to complete






Success!



swagger--resolution date format displayed as Unix timestamp format UTC format


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.