Springboot集成swagger和knife
點(diǎn)擊上方藍(lán)色字體,選擇“標(biāo)星公眾號(hào)”
優(yōu)質(zhì)文章,第一時(shí)間送達(dá)
? 作者?|??碼農(nóng)記事本
來源 |? urlify.cn/7r6bIz
66套java從入門到精通實(shí)戰(zhàn)課程分享
前言
knife4j是在swagger的基本上做做了一次封裝,主要體現(xiàn)在ui表現(xiàn),所有在使用前必須先搭建好swagger2,其實(shí)是swagger和knife都可以訪問,
至于哪個(gè)好用全看個(gè)人!
swagger2
依賴包
compile?group:?'io.springfox',?name:?'springfox-swagger2',?version:?'2.9.2'
compile?group:?'io.springfox',?name:?'springfox-swagger-ui',?version:?'2.9.2'
//下面這兩包是為了防止注解沒有給默認(rèn)值的時(shí)候拋出的警告日志
compile?group:?'io.swagger',?name:?'swagger-annotations',?version:?'1.5.22'
compile?group:?'io.swagger',?name:?'swagger-models',?version:?'1.5.22'
配置
@Getter
@Setter
@Configuration
@EnableSwagger2
@ConfigurationProperties("swagger")
public?class?SwaggerConfig?{
????private?String?host;//自定義host
????@Bean
????public?Docket?createRestApi()?{
????????return?new?Docket(DocumentationType.SWAGGER_2)
????????????????.apiInfo(apiInfo())
????????????????.select()
????????????????.apis(RequestHandlerSelectors.basePackage("com.only.oc.web.api"))//掃描包
????????????????.apis(RequestHandlerSelectors.withClassAnnotation(Api.class))//掃描在API注解的contorller
????????????????.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))//掃描帶ApiOperation注解的方法
????????????????.paths(PathSelectors.any())
????????????????.build().host(host);//自定義host
????}
????private?ApiInfo?apiInfo()?{
????????String?version?=?DateUtil.customFormat(new?Date(),"yyyyMMdd.HHmmss");
????????return?new?ApiInfoBuilder()
????????????????.title("昂立課堂(業(yè)務(wù)版)")
????????????????.description("昂立課堂第二版,在SDK版上升級(jí)了業(yè)務(wù)模塊,業(yè)務(wù)模塊包括:用戶、老師、學(xué)生、課程、課節(jié)、上課統(tǒng)計(jì)流水等。")
????????????????.version("v2."+version)
????????????????.license("Apache?License?Version?2.0")
????????????????.licenseUrl("https://www.apache.org/licenses/LICENSE-2.0")
????????????????.contact(new?Contact("朱增亮",?"",?"[email protected]"))
????????????????.build();
????}
}
訪問
默認(rèn)訪問路徑
http://locahost:8080/swagger.html

knife4j
依賴包
compile?group:?'com.github.xiaoymin',?name:?'knife4j-spring-boot-starter',?version:?'2.0.4'
配置
原來的SwaggerConfig類上加入注解:
@EnableKnife4j
@Import(BeanValidatorPluginsConfiguration.class)
訪問
默認(rèn)訪問路徑
http://locahost:8080/doc.html


粉絲福利:Java從入門到入土學(xué)習(xí)路線圖
???

?長(zhǎng)按上方微信二維碼?2 秒
感謝點(diǎn)贊支持下哈?
評(píng)論
圖片
表情
