How to hide a request field in Swagger API

血红的双手。 提交于 2020-08-27 08:18:19

问题


I want to hide the "id" item in the model, how to do this in java?


回答1:


to hide a request field in Swagger API just use the annotation below :)

 @ApiModelProperty(hidden = true) 
 private String id;

And it will be hive




回答2:


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

Also see: https://github.com/springfox/springfox/issues/2816




回答3:


You can use @Hidden with Swagger Core 2.X

@Hidden -- Hides a resource, an operation or a property

Example from above link : Marks a given resource, class or bean type as hidden, skipping while reading / resolving.

@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();
    }
}

Output of above

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


来源:https://stackoverflow.com/questions/53263086/how-to-hide-a-request-field-in-swagger-api

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!