Swagger配置 配置knife4j 好看的ui界面

Swagger

官方文档

使用

如果不需要knife4j 不要引入他的依赖即可 建议引用

启动类加注解@EnableSwagger2

	很好看的ui界面  推荐使用  两个都可以用 看个人爱好
	访问地址
	doc: 		http://localhost:8088/doc.html
	Swagger: 	http://localhost:8088/swagger-ui.html

导入依赖

 
        <dependency>
            <groupId>io.springfoxgroupId>
            <artifactId>springfox-swagger2artifactId>
            <version>2.9.2version>
            <exclusions>
                <exclusion>
                    <artifactId>swagger-modelsartifactId>
                    <groupId>io.swaggergroupId>
                exclusion>
            exclusions>
        dependency>

        
        <dependency>
            <groupId>io.springfoxgroupId>
            <artifactId>springfox-swagger-uiartifactId>
            <version>2.9.2version>
        dependency>

        
        
        
        <dependency>
            <groupId>com.github.xiaoymingroupId>
            <artifactId>knife4j-spring-boot-starterartifactId>
            <version>2.0.2version>
            <exclusions>
                <exclusion>
                    <artifactId>swagger-annotationsartifactId>
                    <groupId>io.swaggergroupId>
                exclusion>
            exclusions>
        dependency>

配置类

package com.initialize.appapi.config;//package com.initialize.appapi.config;

import com.initialize.pub.jwt.util.JwtTokenUtil;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

/**
 * @program: demo
 * @description: Swagger配置类
 * @author: Mr.Lu
 * @create: 2020-06-05 13:56
 **/
@Configuration
@EnableSwagger2
public class SwaggerConfig {
     
    @Bean
    public Docket createRestApi() {
     

        //在配置好的配置类中增加此段代码即可
        ParameterBuilder ticketPar = new ParameterBuilder();
        List<Parameter> pars = new ArrayList<Parameter>();
        ticketPar.name("Authorization").description("令牌")//name表示名称,description表示描述
                .modelRef(new ModelRef("string")).parameterType("header")
                .required(false).defaultValue(JwtTokenUtil.TOKEN_PREFIX).build();//required表示是否必填,defaultvalue表示默认值 /*.defaultValue(TokenUtil.TOKEN_TEST_TOKEN)*/
        pars.add(ticketPar.build());//添加完此处一定要把下边的带***的也加上否则不生效

        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
//                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))   //这里采用包含注解的方式来确定要显示的接口
                .apis(RequestHandlerSelectors.basePackage("com.initialize.appapi.controller"))    //这里采用包扫描的方式来确定要显示的接口
//                .apis(RequestHandlerSelectors.basePackage("com.star.dealapi.controller"))    //这里采用包扫描的方式来确定要显示的接口
                .paths(PathSelectors.any())
                .build()
                .globalOperationParameters(pars);//************把消息头添加
    }

    private ApiInfo apiInfo() {
     
        return new ApiInfoBuilder()
                .title("初始化项目")
                .description("Api文档")
                .termsOfServiceUrl("https://gitee.com/My_underwear/initialize_project")
                .version("1.0")
                .contact(new Contact("MyLu", "https://blog.csdn.net/qq_46126559", "[email protected]"))
                .build();
    }

}

你可能感兴趣的:(java,spring,boot)