添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement . We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Demonstration API definition

From my API I'm returning a DTO which looks like below

class ResponseDTO  {
     Date CreatedOn;
     Date ModifiedOn;

If I look on the swagger UI, I see this

Here, the actual response sent by the server is in long format (number of milliseconds since 1970). But the swagger is showing Date format as "ModifiedOn": "2018-02-20T17:23:58.907Z". Which is kind of misleading.

Expected Behavior

"ModifiedOn": "1519147559685"
Or something like this

@webron, I tried that using writing annotations

	@ApiModelProperty(value = "Start time of launch", required=true, dataType = "long")
	private Date createdOn;

I'm getting the same output as above.

Am I missing something?

Bump: this is also showing up in the java-jersey2-resourceinit sample (2.0 branch).

Steps to reproduce

  • Run said sample as per instructions
  • Go to Swagger-UI at http://localhost:8002
  • Check example value for route GET /sample/store/order/{orderId}:
  • Check that schema says that the type for shipDate is a date-time string:
  • Make a sample request for order 1
  • The API returns a long value for shipDate, breaking spec:
  • Suggested fix

    In my opinion, either java.util.Date should be mapped to int64 on the spec or it should somehow be output in RFC 3339 format to be spec compatible.

    in Swagger Core 2.x there are a few alternatives (sample has been updated with option 3):

  • annotate your property with @Schema(type = "integer", format = "int64")
  • customize swagger by adding the following in any bootstrap code
  • PrimitiveType.customExcludedClasses().add(Date.class.getName());
    PrimitiveType.customClasses().put(Date.class.getName(), PrimitiveType.LONG);
    
  • depending on the environment, configure its serialization of requests and responses not to use timestamps, e.g. in jersey2:
  • @Provider
        public class ObjectMapperContextResolver implements ContextResolver<ObjectMapper> {
            private final ObjectMapper mapper;
            public ObjectMapperContextResolver() {
                this.mapper = createObjectMapper();
            @Override
            public ObjectMapper getContext(Class<?> type) {
                return mapper;
            private ObjectMapper createObjectMapper() {
                ObjectMapper mapper = new ObjectMapper();
                mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
                return mapper;
    
    register(new ObjectMapperContextResolver());
    

    closing ticket, please reopen if you're still experiencing issues