Swagger的配置与使用

芊嵛 / 2024-04-30 / 原文

swagger

前言

学习

1.swagger前期准备

搜索springfox-swag引入两个jar包,springfox-swagger2和springfox-swagger-ui

<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.9.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>2.9.2</version>
</dependency>

2.简单测试

创建config包

package com.example.demo.config;

import org.springframework.context.annotation.Configuration;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2     //开启swagger2
public class SwaggerConfig {
}

创建controller包

package com.example.demo.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class testController {
    @GetMapping("/index")
    public String index(){
        return "index";
    }
}

运行程序

如果出现这个错误Failed to start bean 'documentationPluginsBootstrapper'; nested exception is java.lang.NullPointerException在配置文件中添加spring.mvc.pathmatch.matching-strategy=ANT_PATH_MATCHER

访问http://localhost:8080/swagger-ui.html,注意换成你自己的端口号

image

3.配置config


image

明天继续。。。