【问题标题】:Swagger example value not showing all attributesSwagger 示例值未显示所有属性
【发布时间】:2021-05-14 15:16:19
【问题描述】:

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 UI 中,显示示例值部分

{ "details":{} }

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

【问题讨论】:

    标签: swagger swagger-ui


    【解决方案1】:

    spring.main.lazy-initialization=false

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

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-03-06
        • 2021-10-03
        • 2022-07-08
        • 1970-01-01
        • 1970-01-01
        • 2020-08-16
        • 1970-01-01
        • 2022-06-14
        相关资源
        最近更新 更多