Spring-Boot使用swagger生成API说明文档(非原著,整理许多大佬然后结合的)
1.配置Spring Boot (这里就不描述)
2.导入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>
3.启动类配置Mapper路径
4.配置SwaggerConfig类
1.我是把它和启动类放在同一目录下
2.SwaggerConfig类
import io.swagger.annotations.Api;
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;
/**
-
Swagger使用的配置文件
-
@author DELL
*/
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
.paths(PathSelectors.any())
.build();
}/**
- 基本信息的配置,信息会在api文档上显示
*/
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title(“导航栏接口文档”)
.description(“导航相关接口的文档”)
.termsOfServiceUrl(“http://localhost:8080/api/v1/orders”)
.version(“1.0”)
.build();
}
}
- 基本信息的配置,信息会在api文档上显示
5.控制层类(逻辑层一些的就懒得写了)
import com.example.demo.entity.Workshop;
import com.example.demo.service.RestFulService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.Map;
@RestController
@RequestMapping("/api/v1/orders")
@Api(value="User的相关信息接口")
public class RestFulController {
//新增 POST 修改PUT 删除DELETE 查询 GET
@Autowired
private RestFulService restFulService;
@GetMapping("/RestFul")
@ApiOperation(value="获取WorkshopName", httpMethod = "GET")
public Map<String, Object> getWorkshop(String WorkshopName, int page, int limit) {
Map<String, Object> map = restFulService.getWorkshop(WorkshopName, page, limit);
return map;
}
/**
* 删除
*
* @param id
* @return
*/
@DeleteMapping("/deleteRestFul")
@ApiOperation(value="获取单个id",notes="获取单个,需参数", httpMethod = "DELETE")
public int deleteWorkshop(@ApiParam(value = "ID", required = true)String id) {
Integer integer = restFulService.deleteWorkshop(id);
return integer;
}
/**
* 修改
*
* @param workshop
* @return
*/
@PutMapping("/updateRestFul")
@ApiOperation(value="获取workshop",notes="获取workshop,需参数", httpMethod = "PUT")
public Integer updateWorkshop(Workshop workshop) {
Integer integer = restFulService.updateWorkshop(workshop);
return integer;
}
/**
* 添加
*
* @param workshop
* @return
*/
@PostMapping("/addRestFul")
@ApiOperation(value="获取workshop",notes="获取workshop,需参数", httpMethod = "POST")
public Integer addWorkshop(Workshop workshop) {
Integer integer = restFulService.addWorkshop(workshop);
return integer;
}
}
写完之后进入这个地址查看文档: http://localhost:8080/swagger-ui.html#/.
1.结果图