如何在Swagger API中隐藏请求字段

时间:2018-11-12 13:21:03

标签: java swagger

我想在模型中隐藏“ id”项,如何在Java中做到这一点?

3 个答案:

答案 0 :(得分:1)

要在Swagger API中隐藏请求字段,只需使用以下注释:)

 @ApiModelProperty(hidden = true) 
 private String id;

这将是蜂巢

答案 1 :(得分:0)

@JsonProperty(access = JsonProperty.Access.READ_ONLY)
@ApiModelProperty(readOnly = true)
private String id;

另请参阅:https://github.com/springfox/springfox/issues/2816

答案 2 :(得分:0)

您可以将@HiddenSwagger Core 2.X一起使用

@Hidden-隐藏资源,操作或属性

上面链接的示例:将给定的资源,类或bean类型标记为隐藏,在读取/解析时跳过。

@Path("/user")
@Produces({"application/json", "application/xml"})
public class HiddenAnnotatedUserResourceMethodAndData {
    UserData userData = new UserData();

    @POST
    @Hidden
    @Operation(summary = "Create user",
            description = "This can only be done by the logged in user.")
    @Path("/1")
    public Response createUser(
            @Parameter(description = "Created user object", required = true) User user) {
        userData.addUser(user);
        return Response.ok().entity("").build();
    }

    @POST
    @Operation(summary = "Create user",
            description = "This can only be done by the logged in user.")
    @Path("/2")
    public Response createUserWithHiddenBeanProperty(
            @Parameter(description = "Created user object", required = true) UserResourceBean user) {
        return Response.ok().entity("").build();
    }
}

以上输出

openapi: 3.0.1
paths:
  /user/2:
    post:
      summary: Create user
      description: This can only be done by the logged in user.
      operationId: createUserWithHiddenBeanProperty
      requestBody:
        description: Created user object
        content:
          '*/*':
            schema:
              $ref: '#/components/schemas/UserResourceBean'
        required: true
      responses:
        default:
          description: default response
components:
  schemas:
    UserResourceBean:
      type: object
      properties:
        foo:
          type: string