1、修改pom.xml配置,添加依赖
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
注意:依赖可查看maven官网下载配置代码
网址:https://mvnrepository.com/search?q=springfox-swag
2、添加swagger类
package com.example.demo.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.annotations.ApiIgnore;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import java.util.ArrayList;
@Configuration
@EnableSwagger2
public class SwaggerConfig {
//配置了swagger的spring的bean实例
//其中enable为是否自动启动swagger,为false则不能访问swagger
@Bean
public Docket docket(){
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.groupName("分组1")//分组标识
.select()
//RequestHandlerSelectors配置要扫描接口的方式
//basePackage扫描包路径
//any()扫描所有
//none()不扫描
//withClassAnnotation扫描类注解
//withMethodAnnotation扫描方法注解
.apis(RequestHandlerSelectors.any())
//paths过滤什么路径
.paths(PathSelectors.ant("/groupOne/**"))
.build();
}
//配置多个分组
@Bean
public Docket docket1(){
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.groupName("分组2")
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.ant("/groupTwo/**"))
.build();
}
//配置swagger信息apiInfo
private ApiInfo apiInfo(){
//作者信息
Contact contact = new Contact("测试", "http://localhost:xxxx/", "2113232");
return new ApiInfo(
"测试接口文档",
"mybatis测试",
"v1.0",
"http://localhost:8090/",
contact,
"Apache 2.0",
"http://www.apache.org/licenses/LICENSE-2.0",
new ArrayList());
}
}
3、写控制器
package com.example.demo.controller;
import com.example.demo.pojo.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;
@RestController
@CrossOrigin//跨域中间件
@RequestMapping("/groupOne")
@Api(tags = {"分组控制器1"})
public class Hello {
@GetMapping(value = "/hello")
public String hello(){
return "hello";
}
@PostMapping(value = "/user")
public User user(){
return new User();
}
@ApiOperation("hello控制类")
@GetMapping(value = "/hello/{username}")
public String hello(@PathVariable("username") @ApiParam("用户名") String username){
return "hello "+username;
}
}
二、spring boot 2.6之后配置有所改变
1、在配置文件中修改默认规则
spring.mvc.pathmatch.matching-strategy=ant_path_matcher
2、swagger配置类
@Configuration
@EnableOpenApi
public class SwaggerConfig {
}
3、依赖
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
4、访问后面改成:/swagger-ui/index.html