用swagger2创建项目的接口

项目结构图:

在这里插入图片描述在这里插入图片描述

创建接口步骤:
1、创建一个package,命名为config
创建SwaggerConfig类和WebMvcConfig类
代码如下:
SwaggerConfig.class

@Configuration //必须存在
@EnableSwagger2 //必须存在
//必须存在 扫描的API Controller包
@ComponentScan(basePackages = {"com.example.newdemo.controller"})
public class SwaggerConfig{

    public Docket customDocket() {
        return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo());
    }

    private ApiInfo apiInfo() {
        Contact contact = new Contact("LWN",
                "http://www.baidu.com/",
                "1227503249@qq.com");
        return new ApiInfoBuilder()
                .title("项目API接口")
                .description("项目API接口的描述")
                .contact(contact)
                .version("1.1.0")
                .build();
    }
}

WebMvcConfig.class

@Configuration
class WebMvcConfig implements WebMvcConfigurer {

    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");

        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
    }

}

2、在pox.xml加入如下依赖

  <!--swagger-api 依赖开始-->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.7.0</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.7.0</version>
        </dependency>
        <!--Jackson required包-->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>2.11.2</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.11.2</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
            <version>2.11.2</version>
        </dependency>
        <!--swagger-api 依赖结束-->

在启动类里面加注解(这一步我参考的教程里写了,但是我没加也没出bug)
在这里插入图片描述

3、最后一步就是运行程序,在浏览器中输入
http://localhost:8080/swagger-ui.html
呈现出如下界面
在这里插入图片描述over!!!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值