此文章是为了记录学习,写的不好勿喷
注解
常用注解:
@Api() | 标注于Controller类上表示是一个api接口 |
@ApiOperation() | 方法描述 |
@ApiParam() | 提交的参数描述 |
@ApiModel() | 实体类描述 |
@ApiModelProperty() | 用在属性上,对属性做注释 |
@ApiIgnore() | |
@ApiImplicitParam() |
|
@ApiImplicitParams() | 用来包含API的一组参数注解,可以简单的理解为参数注解的集合声明; |
@ApiResponses | 通常用来包含接口的一组响应注解,可以简单的理解为响应注解的集合声明; |
@ApiResponse | 用在 @ApiResponses 中,一般用于表达一个错误的响应信息 |
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>
配置类
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 Swagger2Config {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select()
.apis(RequestHandlerSelectors.basePackage("com.target.springboot_mybatis.controller"))
.paths(PathSelectors.any()).build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder().title("springboot_mybatis 整合swaager2 构建api文档")
.description("swaager2 学习 ,https://blog.csdn.net/qq_31897023")
.termsOfServiceUrl("https://blog.csdn.net/qq_31897023").version("1.0").build();
}
}
Model
Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@ApiModel(value = "Admin", description = "管理员实体类")
public class Admin implements Serializable {
//....
}
Controller层 Code
@RestController
@RequestMapping("/admin")
@Api(tags="管理员")
public class AdminController {
@Autowired
private IAdminService adminService;
@GetMapping("/get")
@ResponseBody
@ApiOperation(value = "获取用户实体", notes = "根据ID获取", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@ApiImplicitParam(paramType = "query", name = "id", value = "用户编号", required = false, dataType = "Integer")
private String SelectByID(@RequestParam("id") Integer id) {
return JSON.toJSONString(adminService.GetById(id));
}
}
配置好以后启动就行了 在浏览器地址栏输入http://192.168.18.48:81/swagger-ui.html 即可