效果图
重点
1. 导入jar 包
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>swagger-bootstrap-ui</artifactId>
<version>1.9.6</version>
</dependency>
2. 配置类编写
/**
* @description : swagger 配置类
* @author : Mcally
* @time : 2021年2月1日18:00:07
*/
@Configuration
@EnableSwagger2
@EnableSwaggerBootstrapUI
public class SwaggerConfiguration {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.mcally.demo.controller"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("Mcally 测试SwaggerAPI文档")
.description("这是一个测试的项目")
.termsOfServiceUrl("http://localhost:8899/")
.contact("strive_Mcally@163.com")
.version("1.0")
.build();
}
}
3. 示例Controller
/**
* @ Author :Mcally
* @ Description :演示swagger文档使用
* @ date : 2021年2月1日18:18:32
*/
@RestController
@RequestMapping("/user")
@Api(tags = {"用户管理"}, description = "用户管理")
public class UserController {
@Autowired
UserService userService;
@GetMapping("/list")
@ApiOperation(value = "获取用户信息")
public List<User> list() {
return userService.list();
}
@PutMapping("/add")
@ApiOperation(value = "新增用户信息")
public R add(User user) {
try {
if (userService.add(user))
return R.SUCCESS();
else
return R.Fail(null);
} catch (Exception ex) {
return R.ERROR(ex.getMessage());
}
}
@DeleteMapping("/del")
@ApiOperation(value = "删除用户信息")
public R delete(User user) {
try {
if (userService.delete(user))
return R.SUCCESS();
else
return R.Fail(null);
} catch (Exception ex) {
return R.ERROR(ex.getMessage());
}
}
}
4.示例modular
/**
*
* 用户实体
*/
@Data
public class User {
@ApiModelProperty(value = "用户名称")
private String name;
@ApiModelProperty(value = "年龄")
private Integer age;
}