Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
358 views
in Technique[技术] by (71.8m points)

java - swagger date field vs date-time field

I am using swagger to test my rest api, one of the property of my entity class is a date field for which I need the date in yyyy-mm-dd format , but swagger model schema is showing this field as date-time instead of date field, therefore it gives date with time and zone. How can I convert this date-time into date field ?

I have a java entity class TimeEntry.java one of its property is Date, it looks like this.

@ApiModelProperty(required = true)
@JsonFormat(pattern = DATE_FORMAT)
private Date date;

for this field, on the swagger UI model schema, the field date displays as "date": "2016-01-08T22:34:22.337Z" but I need this as "date":"2016-01-08" .

I have tried the following:

1.

@ApiModelProperty(required = true, dataType="date")  
@JsonFormat(pattern = DATE_FORMAT)   
private Date date;

2.Tried to follow along this code (override OverrideConvertor class) but could not find swagger-core 1.3 version mvn repo. Only available is 1.5 version https://github.com/swagger-api/swagger-core/wiki/overriding-models

  1. Apparently from 1.5 version they have removed the OverrideConvertor class https://groups.google.com/forum/#!topic/swagger-swaggersocket/ChiknyHZiP4

Please help.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The problem (one of the problems actually) with java.util.Date is that it's really a date-time, and swagger correctly detects it as such. I do understand that the @JsonFormat is a workaround for this as well--swagger does not support that annotation during it's type detection.

You have three options to properly handle date types.

1) Use Joda's LocalDate as the datatype. If you declared private LocalDate date, it would appear correctly.

2) Use java8's LocalDate, same as above.

3) Tell swagger to use either of the above when detecting the type in the annotation, but keep the property as type java.util.Date:

@ApiModelProperty(required = true, dataType = "org.joda.time.LocalDate")

Then, when scanning, swagger will detect this as a date formatted string.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...