【问题标题】:How to add <a href> link to make an element clickable in java for Swagger如何为 Swagger 添加 <a href> 链接以使元素在 Java 中可点击
【发布时间】:2017-08-28 08:29:41
【问题描述】:

我想让 Capo Mastro 成为 Java 中的可点击链接,以便在 @ApiOperation 注释中大摇大摆,但我无法得到任何结果。

@ApiOperation(
        value = "This method updates an existing capo mastro."+" <a href>Capo mastro<a>"+ can be managed only by system",
        response = SaveCapoMastroResponse.class
    )

如果有人能提供帮助,我将不胜感激!谢谢!

【问题讨论】:

    标签: java annotations swagger


    【解决方案1】:

    假设您的意思是在 Swagger UI 中可点击 - 您可以使用 Markdown 语法在操作描述(notes 属性)中添加链接。操作总结(value)虽然不支持 Markdown。

    @ApiOperation(
            value = "This method updates an existing capo mastro.",
            notes = "[Capo mastro](http://example.com) can be managed only by system.",
            response = SaveCapoMastroResponse.class
        )
    

    HTML &lt;a href=""&gt; 标签也应该可以工作,因为 Markdown 支持 HTML。确保转义内部的 " 引号。

    @ApiOperation(
            value = "This method updates an existing capo mastro.",
            notes = "<a href=\"http://example.com\" target=\"_blank\">Capo mastro</a> can be managed only by system.",
            response = SaveCapoMastroResponse.class
        )
    

    【讨论】:

    • 这不起作用
    • 究竟如何这不起作用?你有任何错误吗? Swagger UI 是否不呈现链接?您使用的是哪个版本的 swagger-annotations?越详细越好。
    • 链接显示为纯文本。招摇 1.5.16
    【解决方案2】:

    您可以使用 @ExternalDocs 注释来指定 api 的链接,如下所示:

    @ExternalDocs(value="Capo mastro", url="link to Capo mastro")
    

    【讨论】:

    • 你遇到了什么错误?您使用的是哪个版本的 Swagger?
    • 它没有给我任何错误。它根本没有显示任何东西。我正在使用 swagger 1.5.16
    • 我不允许更改swagger注释版本,因为它是一个完整的项目
    • IIRC @ApiOperation 不支持 @ExternalDocs。只有标签和“根”Swagger 定义支持@ExternalDocs
    【解决方案3】:

    我在描述中添加了可点击的图像标志:

    private static ApiInfo v1ApiInfo() {
        return new ApiInfoBuilder()
                .version("1.0")
                .title("Council on Dairy Cattle Breeding (CDCB) API v1.0 Documentation"))
                .description("[![cdcb](/img/cdcb.png)](https://www.uscdcb.com/)").build();
    }
    

    /img/cdcb.png - 这是来自可用资源的图像。我使用 SpringBoot + Maven,它会自动从资源中添加公用文件夹;图片的完整路径是/src/main/recources/public/img/cdcb.png

    因此,您将获得可点击的图像,如下所示:

    【讨论】:

      猜你喜欢
      • 2012-10-06
      • 2013-05-30
      • 1970-01-01
      • 2020-07-14
      • 1970-01-01
      • 1970-01-01
      • 2018-02-17
      • 2015-06-20
      • 2012-06-18
      相关资源
      最近更新 更多