波动示例值未显示所有属性

时间:2019-02-08 01:23:27

标签: swagger swagger-ui

TLDR Swagger不会在UI的“示例值”部分中显示所有属性,无论是否对其进行了注释。

我可能做错了,任何帮助将不胜感激!

import com.codahale.metrics.annotation.ResponseMetered
import com.codahale.metrics.annotation.Timed
import io.swagger.annotations.Api
import io.swagger.annotations.ApiOperation
import javax.validation.Valid
import javax.ws.rs.POST
import javax.ws.rs.Path
import javax.ws.rs.Produces
import javax.ws.rs.core.MediaType
import javax.ws.rs.core.Response

@Api(
        tags = ["foobar"],
        description = "foobar")
@Path("/v1/user")
class FooResource {

    @POST
    @Path("v1/user")
    @Produces(MediaType.APPLICATION_JSON)
    @Timed
    @ResponseMetered
    @ApiOperation("Foo bar")
    fun lisApiUser(@Valid user: User): Response {
        throw RuntimeException("foo")
    }

}
import com.fasterxml.jackson.annotation.JsonCreator
import com.fasterxml.jackson.annotation.JsonIgnoreProperties
import com.fasterxml.jackson.annotation.JsonProperty
import io.swagger.annotations.ApiModel
import io.swagger.annotations.ApiModelProperty

@JsonIgnoreProperties(ignoreUnknown = true)
//@ApiModel makes no difference
data class User @JsonCreator constructor(

        @JsonProperty("name")
//        @ApiModelProperty(name = "name", value = "John Smith", example = "John Smith") makes no difference
//        @field:ApiModelProperty(name = "name", value = "John Smith", example = "John Smith") makes no difference
        val name: String, //doesn't show in Swagger UI

        @JsonProperty("details")
        val details: Details //shows in Swagger UI

)
import com.fasterxml.jackson.annotation.JsonCreator
import com.fasterxml.jackson.annotation.JsonIgnoreProperties
import com.fasterxml.jackson.annotation.JsonProperty
import io.swagger.annotations.ApiModel
import io.swagger.annotations.ApiModelProperty

@JsonIgnoreProperties(ignoreUnknown = true)
//@ApiModel makes no difference
data class Details @JsonCreator constructor(

        @JsonProperty("email")
//        @ApiModelProperty(name = "email", value = "foo@example.com") makes no difference
//        @field:ApiModelProperty(name = "email", value = "foo@example.com", example = "foo@example.com") makes no difference
        val email: String //doesn't show in Swagger UI

)

https://imgur.com/a/JRgMVh4

在Swagger用户界面中,示例值部分显示

{ "details":{} }

ie名称和电子邮件属性不会显示。奇怪的是,单击Swagger UI的Model部分确实会显示所有属性。

2 个答案:

答案 0 :(得分:1)

spring.main.lazy-initialization=false

在 application.properties 中,将延迟初始化设为 false 对我有用。

答案 1 :(得分:0)

只需使用jackson-module-kotlin并从数据类中删除所有注释,构造函数等,即可将Jackson和Swagger都“正常工作”

https://github.com/FasterXML/jackson-module-kotlin