swagger2接口文档的使用
1.在pom中添加
<!--swagger--> <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.创建自定义配置类
package com.jk.config; 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 createRestApi(){ return new Docket(DocumentationType.SWAGGER_2) //指定构建API文档的详细信息的方法:apiiInfo() .apiInfo(apiInfo()) .select() //指定要生成api接口的包路径,这里把controller作为包路径,生成controller中的所有接口 .apis(RequestHandlerSelectors.basePackage("com.jk.comtroller")) .paths(PathSelectors.any()) .build(); } /** * 构建api文档的详细信息 * @return */ private ApiInfo apiInfo() { return new ApiInfoBuilder() //设置页面标题 .title("Spring Boot集成Swagger2接口总览") //设置接口描述 .description("跟我一起学Spring Boot") //设置联系方式 .contact("111111111111") //设置版本 .version("1.0") //构建 .build(); } }
3.访问接口文档地址是否有信息:http://localhost:8082/swagger-ui.html
4.在实体中添加@ApiModel和@ApiModelProperty
package com.jk.entity; import com.baomidou.mybatisplus.annotation.TableName; import io.swagger.annotations.Api; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; import sun.awt.SunHints; import java.io.Serializable; @TableName("t_user") @ApiModel(value = "用户实体类") public class User implements Serializable { private static final long serialVersionUID=1L; @ApiModelProperty(value = "用户唯一标识") private Integer id; @ApiModelProperty(value = "用户账号") private String username; @ApiModelProperty(value = "用户密码") private String password; @ApiModelProperty(value = "角色id") private Integer roleId; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public Integer getRoleId() { return roleId; } public void setRoleId(Integer roleId) { this.roleId = roleId; } @Override public String toString() { return "User{" + "id=" + id + ", username='" + username + '\'' + ", password='" + password + '\'' + ", roleId=" + roleId + '}'; } }
5.在controller中添加@Api、@ApiOperation、@ApiParam
@Controller @RequestMapping("/user") @Api(value = "Swagger2 在线接口文档") public class UserController { @GetMapping("/get/{id}") @ApiOperation(value = "根据用户唯一标识获取用户信息") @ResponseBody @ApiResponses({ @ApiResponse(code = 500,message = "查询异常") }) public JsonResult getUserInfo(@PathVariable @ApiParam(value = "用户唯一标识") Integer id){ User user=new User(); user.setId(id); user.setUsername("wangwu"); user.setPassword("123"); user.setRoleId(1); return new JsonResult(user); }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?