添加swagger相關(guān)配置
1雷猪、依賴配置
<!-- swagger -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<!-- 接口API生成html文檔 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
<!--解決控制層接受參數(shù)為Integer類似的參數(shù),訪問swagger頁面報(bào)錯(cuò)-->
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-models</artifactId>
<version>1.5.21</version>
</dependency>
<dependency>
<groupId>io.github.swagger2markup</groupId>
<artifactId>swagger2markup</artifactId>
<version>1.3.1</version>
</dependency>
2、編寫config
package com.hag.opmos.config;
import io.swagger.annotations.ApiOperation;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
/**
* @Package: com.hag.opmos.config
* @ClassName: swaggerConfig
* @Author: Administrator
* @Date:2021/4/30 10:55
* @Description:
*/
@Configuration
@EnableSwagger2
public class swaggerConfig implements WebMvcConfigurer {
@Bean
public Docket createREstApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.enable(true)
.select()
.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
.apis(RequestHandlerSelectors.basePackage("com.hag.opmos.controller"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("web api")
.description("接口說明")
.version("1.0.0")
.build();
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/**")
.addResourceLocations("classpath:/static/");
registry.addResourceHandler("swagger-ui.html")
.addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/");
WebMvcConfigurer.super.addResourceHandlers(registry);
}
}
這時(shí)候可以運(yùn)行一下試試看看能出來swagger頁面不席函。沒問題繼續(xù)....
4、編寫all.adoc Test文件
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
</dependency>
package com.hag.opmos.test;
import com.hag.opmos.App;
import io.github.swagger2markup.Swagger2MarkupConfig;
import io.github.swagger2markup.Swagger2MarkupConverter;
import io.github.swagger2markup.builder.Swagger2MarkupConfigBuilder;
import io.github.swagger2markup.markup.builder.MarkupLanguage;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.net.URL;
import java.nio.file.Paths;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = App.class)
public class SwaggerTest {
@Test
public void generateAsciiDocs() throws Exception {
// 輸出Ascii格式
Swagger2MarkupConfig config = new Swagger2MarkupConfigBuilder()
.withMarkupLanguage(MarkupLanguage.ASCIIDOC)
.build();
//此處填寫swagger項(xiàng)目地址
Swagger2MarkupConverter.from(new URL("http://127.0.0.1:11003/v2/api-docs"))
.withConfig(config)
.build()
.toFile(Paths.get("src/docs/asciidoc/generated/all"));
}
}
這里運(yùn)行測(cè)試用例以前先啟用app.js,在執(zhí)行測(cè)試用例
5冈涧、插件配置
<plugin>
<groupId>org.asciidoctor</groupId>
<artifactId>asciidoctor-maven-plugin</artifactId>
<version>1.5.6</version>
<dependencies>
<dependency>
<groupId>org.asciidoctor</groupId>
<artifactId>asciidoctorj-pdf</artifactId>
<version>1.5.0-alpha.11</version>
</dependency>
</dependencies>
<executions>
<execution>
<id>output-html</id>
<phase>generate-resources</phase>
<goals>
<goal>process-asciidoc</goal>
</goals>
</execution>
</executions>
<configuration>
<sourceDirectory>src/docs/asciidoc/generated</sourceDirectory>
<outputDirectory>src/docs/asciidoc/pdf</outputDirectory>
<backend>pdf</backend>
</configuration>
</plugin>
最后執(zhí)行
pdf 格式錯(cuò)誤問題解決:
maven倉庫中的asciidoctorj-pdf jar包茂附,使用壓縮工具打開:
進(jìn)入asciidoctorj-pdf-1.5.0-alpha.16.jar\gems\asciidoctor-pdf-1.5.0.alpha.16\data\ 目錄
fonts:字體文件目錄
themes:配置文件目錄
下載字體:
字體下載:https://github.com/chloerei/asciidoctor-pdf-cjk-kai_gen_gothic/releases
注:只需下載KaiGenGothicCN-Bold.ttf、KaiGenGothicCN-Bold-Italic.ttf督弓、KaiGenGothicCN-Bold-Italic.ttf营曼、KaiGenGothicCN-Bold-Italic.ttf 即可
將字體文件放入fonts目錄中
修改配置
進(jìn)入themes目錄,修改default-theme.yml文件
修改以base:font_family屬性值對(duì)應(yīng)的字體文件配置:在font:catalog下愚隧。
base:
font_family: Noto Serif
font:
catalog:
Noto Serif:
normal: KaiGenGothicCN-Regular.ttf
bold: KaiGenGothicCN-Bold.ttf
italic: KaiGenGothicCN-Regular-Italic.ttf
bold_italic: KaiGenGothicCN-Bold-Italic.ttf
因?yàn)椴寮?duì)中文支持不好蒂阱,解決辦法:首先下載中文字庫.ttl文件,可在網(wǎng)上下載需要的字體文件奸攻。
找到maven中依賴的插件所在的本地倉庫位置:例如我的是在D:\repo\org\asciidoctor\asciidoctorj-pdf\1.5.0-alpha.11蒜危。將下載的字體文件,添加到該壓縮包中指定位置:asciidoctorj-pdf-1.5.0-alpha.11.jar\gems\asciidoctor-pdf-1.5.0.alpha.11\data\fonts
修改asciidoctorj-pdf-1.5.0-alpha.11.jar\gems\asciidoctor-pdf-1.5.0.alpha.11\data\themes下的default-theme.yml文件
修改完成后睹耐,在按照生成步驟辐赞,重新生成pdf即可。
參考博客:https://blog.csdn.net/gs_jy/article/details/111287105 https://blog.csdn.net/qq_27621651/article/details/116129152