企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
案例代码:https://gitee.com/flymini/codes01/tree/master/springboot_/com-learn-boot12 **** 后端生成文档给前端程序员参考,方便前端程序员开发。我们可以借助 Swagger2 工具来生成文档。 <br/> 使用步骤如下: **1. 引入swagger2依赖** ```xml <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> </dependency> <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> </dependencies> ``` **2. 注册Swagger2到IoC容器** ```java @Configuration @EnableSwagger2 public class Swagger2Config { @Bean public Docket docket() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() //只能对controller层生成文档 .apis(RequestHandlerSelectors.basePackage("com.learn.boot12.controller")) .paths(PathSelectors.any()).build(); } private ApiInfo apiInfo() { return new ApiInfoBuilder() //文档标题 .title("com-learn-boot12项目文档") //作者, 地址, 邮箱 .contact(new Contact("Zhangsan", "www.baidu.com", "123@163.com")) .version("1.0") .description("com-learn-boot12项目文档").build(); } } ``` **3. 在controller层调用Swagger2相关注解说明** ```java @Controller @Api(tags = "有关用户的所有操作!") //UserController类的文档说明 public class UserController { @ApiOperation(value = "用户登录功能!", notes = "需要前端传递账号和密码!") //该方法的文档说明 @ApiImplicitParams({ //该方法的参数说明 @ApiImplicitParam(name = "userName", value = "账户名", dataType = "String", example = "zhangsan"), @ApiImplicitParam(name = "password", value = "密码", dataType = "String", example = "123456") }) @RequestMapping(value = "/loginManager", method = RequestMethod.POST) public String login(@RequestParam("userName") String userName , @RequestParam("password") String password , HttpServletRequest request) { return "/main"; } } ``` **4. 启动SpringBoot项目** 访问 http://localhost:8080/swagger-ui.html 即可看到文档。 ![](https://img.kancloud.cn/c8/3f/c83fa3df1469a925f068d728c334f5dd_780x372.png)