【问题标题】:Swagger annotation traversing all tables in DBSwagger注解遍历DB中的所有表
【发布时间】:2019-12-20 10:57:56
【问题描述】:

我正在查看一个提供 REST API 来访问数据库的 Java 应用程序。除了 JAX-RS,它还使用 Swagger 生成文档和测试网站。在其中一条路线上,我意识到 Swagger 给出的结果示例非常长(超过 140000 行):

这条路线是用以下代码定义的:

@GET
@Path("/getUsers")
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
@Operation(
        summary = "Get available users",
        responses = {
                @ApiResponse(responseCode = "200", content = @Content(schema = @Schema(implementation = OauthClientDetailsEntity.class)), description = "Get users list"),
                @ApiResponse(responseCode = "401", content = @Content(schema = @Schema(implementation = ErrorResponse.class)), description = "Error: Unauthorized"),
                @ApiResponse(responseCode = "500", description = "Error: Internal Server Error")
        }
)
public Response getUsers() {
  // ....
}

“麻烦制造者”是idCliente字段,在实体类中是这样定义的:

@JoinColumn(name = "idCliente", referencedColumnName = "id")
@ManyToOne
private Cliente idCliente;

Cliente(英文的customer)与DB中的很多表相关,所以我认为问题在于Swagger正在遍历所有这些表,所以示例结果很长。该类有40多个字段,如下所示:

@Entity
@Table(name = "cliente")
@XmlRootElement
public class Cliente implements Serializable {
    @OneToMany(mappedBy = "idCliente")
    private List<FacturaCliente> facturaClienteList;

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "idCliente")
    private List<OauthClientDetailsEntity> oauthClientDetailsEntityList;

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "cliente")
    private List<OauthAccessTokenEntity> oauthAccessTokenEntityList;

    @Column(name = "codigoPlantillaFacturacion")
    private String codigoPlantillaFacturacion;
    // etc...
}

我是 Swagger 的新手,所以我不确定如何处理这个问题。有什么办法可以让 Swagger 不遍历所有相关表?或者,还有什么其他方法更有效?

提前致谢,

【问题讨论】:

  • User 类是什么样的?

标签: java swagger jax-rs


【解决方案1】:

您可以使用@ApiModelProperty(hidden=true) 忽略此字段 但是您必须知道,无论如何,所有这些内容都会被渲染。如果您不需要此字段,更好的方法是不渲染它(例如@JsonIgnore 注释)。最好的选择是只返回带有必要字段的 DTO,而不是返回带有层次结构的整个实体。

【讨论】:

  • 嗨!我会选择最新的建议。我将创建一个只包含必填字段的 DTO 类,尽管我必须在整个 API 中完成这一切......谢谢!
猜你喜欢
  • 2013-07-07
  • 1970-01-01
  • 2012-09-17
  • 1970-01-01
  • 1970-01-01
  • 2022-11-18
  • 1970-01-01
  • 2015-09-24
  • 1970-01-01
相关资源
最近更新 更多