【问题标题】:Create pdf for Rest web services with Springfox使用 Springfox 为 Rest Web 服务创建 pdf
【发布时间】:2018-07-30 15:14:08
【问题描述】:

我使用的是 Spring(不是 Spring Boot)、Maven 和 Springfox。使用最后一个插件,我可以通过 URL /swagger-ui.html 显示 Web 服务文档,但我需要一个文档来共享。我读到了 Swagger2Markup 所以我已经将这个依赖项导入到 pom 文件中:

    <dependency>
        <groupId>io.github.swagger2markup</groupId>
        <artifactId>swagger2markup</artifactId>
        <version>1.3.1</version>
    </dependency>

还有这个仓库

<repositories>
    <repository>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
        <id>jcenter-releases</id>
        <name>jcenter</name>
        <url>http://jcenter.bintray.com</url>
    </repository>
</repositories>

此时我已经创建了一个测试类:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {AppConfig.class, SwaggerConfig.class})
@WebAppConfiguration
public class Swagger2MarkupTest {

    @Autowired
    private WebApplicationContext context;

    private MockMvc mockMvc;

    @Before
    public void setUp() {
        this.mockMvc = MockMvcBuilders.webAppContextSetup(this.context).build();
    }

    @Test
    public void convertSwaggerToAsciiDoc() throws Exception {
        String outputDir = ("C:/Users/Luca Calicchio/Downloads");
        MvcResult mvcResult = this.mockMvc.perform(get("/v2/api-docs")
                .accept(MediaType.APPLICATION_JSON))
                .andExpect(status().isOk())
                .andReturn();

        MockHttpServletResponse response = mvcResult.getResponse();
        String swaggerJson = response.getContentAsString();
        Files.createDirectories(Paths.get(outputDir));
        try (BufferedWriter writer = Files.newBufferedWriter(Paths.get(outputDir, "swagger.json"), StandardCharsets.UTF_8)){
            writer.write(swaggerJson);
        }
    }
}

这个类创建一个带有文档的 json 文件,现在我必须将此文件转换为 pdf。我没有找到做这一步的指南,只找到这个指南

  • 在单元期间生成最新的 Swagger JSON 文件或 集成测试

  • 将 Swagger JSON 文件转换为 AsciiDoc

  • 添加手写 AsciiDoc 文档

  • 将 AsciiDoc 转换为 HTML 和 PDF

  • 将 HTML 和 PDF 工件复制到可执行的 Spring Boot Jar 中 文件并将其作为静态内容提供

    你能帮帮我吗?

【问题讨论】:

    标签: java spring maven swagger springfox


    【解决方案1】:

    我在这个演示代码之后找到了解决方案:

    https://github.com/Swagger2Markup/spring-swagger2markup-demo
    

    注意pom.xml文件,其中配置了上述所有步骤

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-26
      • 1970-01-01
      • 2018-07-09
      • 1970-01-01
      • 1970-01-01
      • 2017-01-23
      相关资源
      最近更新 更多