SpringBoot+Swagger2
Swagger2在集成SpringBoot中的时候 3.0版本是个分割线,大于3.0.0包括3.0.0版本和小于3.0.0版本的jar以及书写配置文件有很大的区别
一、小于3.0.0版本
1.增加jar在pom.xml
<!--swagger2 -->
<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.创建Swagger2配置类
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
public class SwaggerConfig {
@Bean
public Docket docket() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(restApiInfo())
.select()
// 指定包名
.apis(RequestHandlerSelectors.basePackage("com.swagger.controller"))
.paths(PathSelectors.any())
.build()
.enable(true);
}
private ApiInfo restApiInfo() {
return new ApiInfoBuilder()
.title("springboot利用swagger2构建api文档")
.description("简单优雅的restful风格")
.termsOfServiceUrl("no terms of serviceUrl")
.version("1.0")
.build();
}
}
3.创建Controller类
package com.swagger.controller;
import com.swagger.entity.Company;
import com.swagger.util.Result;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.*;
/**
* @Author Administrator
* @Date 2021/2/26 13:39
* @Version
**/
@Api("Swagger示例CRUD操作")
@RestController
@RequestMapping("/company")
public class CompanyController {
//查询单一
@ApiOperation(value = "根据id获取信息",notes = "id必须是数字")
@ApiImplicitParams({@ApiImplicitParam(name = "id",value = "标识",required = true,paramType = "path"),
@ApiImplicitParam(name = "name",value = "姓名",required = true)})
@ApiResponses({@ApiResponse(code=400,message="id不能为空")})
@GetMapping("/{id}")
public String findById(@PathVariable("id") String id,String name){
return id+"名称为:"+name;
}
//添加
@ApiOperation("新增信息")
@ApiImplicitParam(name = "company", value = "单个信息", dataType = "Company")
@PostMapping("/add")
public Result add(){
try {
return new Result(true,"新增成功");
} catch (Exception e) {
e.printStackTrace();
return new Result(false,"新增失败");
}
}
//修改
@ApiOperation("修改公司信息")
@ApiImplicitParam(name = "company", value = "单个公司信息", dataType = "Company")
@PutMapping("/update")
public Result update(@RequestBody Company company){
try {
return new Result(true,company);
} catch (Exception e) {
e.printStackTrace();
return new Result(false,"修改失败");
}
}
//删除
@ApiOperation("删除用户")
@ApiImplicitParam(name = "id", value = "单个用户信息", dataType = "String")
@RequestMapping(value = "/{id}",method = RequestMethod.DELETE)
public Result delete(@PathVariable("id") String id){
try {
return new Result(true,"删除成功");
} catch (Exception e) {
e.printStackTrace();
return new Result(false,"删除失败");
}
}
}
4.访问路径
http://localhost:7899/swagger-ui.html
二、大于3.0.0版本(包含3.0)
1.增加jar在pom.xml
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
2.创建Swagger2配置类
这里就不用写配置了,直接使用api文档就行了
3.创建Controller类
package com.swagger.controller;
import com.swagger.entity.Company;
import com.swagger.util.Result;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.*;
/**
* @Author Administrator
* @Date 2021/2/26 13:39
* @Version
**/
@Api("Swagger示例CRUD操作")
@RestController
@RequestMapping("/company")
public class CompanyController {
//查询单一
@ApiOperation(value = "根据id获取信息",notes = "id必须是数字")
@ApiImplicitParams({@ApiImplicitParam(name = "id",value = "标识",required = true,paramType = "path"),
@ApiImplicitParam(name = "name",value = "姓名",required = true)})
@ApiResponses({@ApiResponse(code=400,message="id不能为空")})
@GetMapping("/{id}")
public String findById(@PathVariable("id") String id,String name){
return id+"名称为:"+name;
}
//添加
@ApiOperation("新增信息")
@ApiImplicitParam(name = "company", value = "单个信息", dataType = "Company")
@PostMapping("/add")
public Result add(){
try {
return new Result(true,"新增成功");
} catch (Exception e) {
e.printStackTrace();
return new Result(false,"新增失败");
}
}
//修改
@ApiOperation("修改公司信息")
@ApiImplicitParam(name = "company", value = "单个公司信息", dataType = "Company")
@PutMapping("/update")
public Result update(@RequestBody Company company){
try {
return new Result(true,company);
} catch (Exception e) {
e.printStackTrace();
return new Result(false,"修改失败");
}
}
//删除
@ApiOperation("删除用户")
@ApiImplicitParam(name = "id", value = "单个用户信息", dataType = "String")
@RequestMapping(value = "/{id}",method = RequestMethod.DELETE)
public Result delete(@PathVariable("id") String id){
try {
return new Result(true,"删除成功");
} catch (Exception e) {
e.printStackTrace();
return new Result(false,"删除失败");
}
}
}
4.访问路径
http://localhost:7899/swagger-ui/index.html