springboot中使用swagger
2021-01-27 本文已影响0人
LeslieFind
参考:https://www.cnblogs.com/xifengxiaoma/p/11022146.html
一、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>
二、配置类
2.1、创建config目录,创建SwaggerConfig类:
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration //添加到配置
@EnableSwagger2 // 使用Swagger2
public class SwaggerConfig {
@Bean // 添加到bean
public Docket createRestApi(){
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo(){
return new ApiInfoBuilder()
.title("swaggettest title")
.description("this is a restful api document of ld")
.version("1.0")
.build();
}
}
三、controller中使用
import com.loadtestrecord.api.common.CommonResult;
import com.loadtestrecord.api.mapper.RecordDataMapper;
import com.loadtestrecord.api.mapper.TestRecordMapper;
import com.loadtestrecord.api.pojo.RecordData;
import com.loadtestrecord.api.pojo.TestRecord;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.List;
@RestController
@RequestMapping("api")
@Api(value = "desc of class")
public class ApiContraller {
@Autowired
private TestRecordMapper testRecordMapper;
@Autowired
private RecordDataMapper recordDataMapper;
/**
* 获取所有的record列表
* @return
*/
@GetMapping("/getRecordList")
@ApiOperation(value = "desc of method",notes = "this is method notes")
public CommonResult getRecordList(){
return CommonResult.success(testRecordMapper.getTestRecordAll());
}
}
四、访问swaggerUI:http://localhost:8081/swagger-ui.html