springboot swagger starter

自定义starter不会的可以点此链接查看

下面介绍starter的实现

添加依赖

<!-- doc -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.6.1</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.6.1</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.plugin</groupId>
            <artifactId>spring-plugin-core</artifactId>
            <version>1.2.0.RELEASE</version>
            <exclusions>
                <exclusion>
                    <groupId>*</groupId>
                    <artifactId>*</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-spring-web</artifactId>
            <version>2.6.1</version>
            <exclusions>
                <exclusion>
                    <groupId>*</groupId>
                    <artifactId>*</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-schema</artifactId>
            <version>2.6.1</version>
            <exclusions>
                <exclusion>
                    <groupId>*</groupId>
                    <artifactId>*</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.plugin</groupId>
            <artifactId>spring-plugin-metadata</artifactId>
            <version>1.2.0.RELEASE</version>
            <exclusions>
                <exclusion>
                    <groupId>*</groupId>
                    <artifactId>*</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>io.github.robwin</groupId>
            <artifactId>swagger2markup</artifactId>
            <version>0.9.2</version>
            <exclusions>
                <exclusion>
                    <groupId>*</groupId>
                    <artifactId>*</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

定义一个SwaggerConfig类




import springfox.documentation.swagger2.annotations.EnableSwagger2;

import org.springframework.context.annotation.Bean;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

@EnableSwagger2
@Configuration
public class SwaggerConfig extends WebMvcConfigurerAdapter {

    @Value("${sop.swagger.enable}")
    private boolean enable;
    @Value("${sop.swagger.package.scan}")
    private String  packageScan;
    @Value("${sop.swagger.title}")
    private String  title;
    @Value("${sop.swagger.description}")
    private String  description;
    @Value("${sop.swagger.version}")
    private String  version;

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        if (enable) {
            registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
            registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
        }
    }

    @Bean
    public Docket createRestApi() {
        ApiInfo apiInfo = new ApiInfoBuilder().title(title)//大标题
                .description(description).version(version)//版本
                .build();
        return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo).enable(enable)
                .useDefaultResponseMessages(false).select().apis(RequestHandlerSelectors.basePackage(packageScan))
                .build();
    }
}

在resources下创建META-INF/spring.factories

    org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.swagger.SwaggerConfig

然后starter就可以直接打包放在私服上去了

使用方式

在项目中添加starter依赖

在application.properties
#swagger
sop.swagger.enable=true 
sop.swagger.package.scan=com.demo
sop.swagger.title=标题
sop.swagger.description=描述
sop.swagger.version=3.0
### 回答1: Spring Boot 是一个用于快速构建Java应用程序的框架,而Swagger UI 是一个用于展示和测试RESTful API的工具。 当我们在使用Spring Boot集成Swagger UI时,可能会遇到404错误。这种错误通常是由于Swagger UI配置不正确或缺少必要的依赖导致的。 要解决这个问题,首先确保在项目的pom.xml文件中添加了swagger相关的依赖,如 springfox-boot-starterspringfox-swagger-ui。 接下来,确保在Spring Boot的配置类上添加 @EnableSwagger2 注解,以启用Swagger。 设置好依赖和配置后,重新编译和启动项目。然后,在浏览器中输入要访问的Swagger UI的地址,通常是 "http://localhost:8080/swagger-ui.html"。 如果你仍然遇到404错误,可以尝试以下解决方法: 1. 检查控制器类和方法上是否添加了Swagger的注解,例如 @ApiOperation 或 @Api。 2. 检查Swagger配置类中的包扫描路径是否正确,确保它可以扫描到你的控制器类。 3. 确认项目的启动类上是否添加了 @ComponentScan 注解,以扫描到Swagger配置类。 如果上述步骤都正确配置,但仍然遇到404错误,可能是由于其他因素引起的。你可以查看项目的日志文件或尝试重启项目进行排查。你也可以在Swagger官方文档或社区中查找关于解决404错误的更多信息和解决方案。 总结:要解决Spring Boot集成Swagger UI出现404错误的问题,需要正确配置Swagger的依赖、注解和包扫描路径,并确保启动项目后访问的URL正确。如果问题仍然存在,可以查看日志文件或在Swagger社区中寻求帮助。 ### 回答2: 当我们使用Spring Boot集成Swagger UI时,出现404错误通常是由于以下几个常见原因导致的: 1. Swagger配置错误:首先,我们需要在Spring Boot项目的配置类上添加`@EnableSwagger2`注解以启用Swagger,然后在配置类中创建一个`Docket` Bean。在创建Docket Bean时,我们需要设置API文档的基本信息,如标题、描述、版本等,并指定要扫描的API包路径。确认我们的配置正确无误。 2. 缺少Swagger依赖:我们需要在项目的pom.xml文件中添加Swagger的相关依赖,如`springfox-swagger2`和`springfox-swagger-ui`。确认我们的项目中已经正确添加了这些依赖。 3. 项目访问路径错误:默认情况下,Swagger UI页面的访问路径是`/swagger-ui.html`。请确保我们在浏览器中访问的路径与这个路径一致。如果我们想要修改访问路径,可以在Docket Bean的配置中设置`path`属性。 4. 项目的访问权限限制:如果我们的Spring Boot项目中设置了访问权限限制,例如使用了Spring Security,我们可能需要手动配置允许访问Swagger UI的路径。可以在Spring Security的配置类中添加一个针对Swagger UI路径的免认证配置。 总结一下,Spring Boot集成Swagger UI出现404错误通常是由于Swagger的配置错误、缺少相关依赖、访问路径设置不正确或者访问权限限制等问题引起的。我们需要仔细检查这些方面,并做出相应的调整和修正。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值