最好的API使用Swagger工具构建
1. springboot整合swagger2
导入swagger依赖(在maven项目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>
在主程序中添加@EnableSwagger2
@SpringBootApplication
@EnableSwagger2
public class MainApplication {
public static void main(String[] args) {
SpringApplication.run(MainApplication.class, args);
}
}
在需要api的类上面添加注解
@RestController
@Api(tags = "UserinfoCtrl", description = "用户信息相关")
public class testswaggercontroller {
@RequestMapping("/testswagger")
@ApiOperation(value = "获取用户信息", httpMethod = "GET", notes = "显示用户信息")
public Map<String, Object> fun() {
Map<String , Object> result=new HashMap<String,Object>();
result.put("test", "test");
Demo demo=new Demo("junye", "1");
result.put("Demo", demo);
System.out.println("chenggong");
return result;
}
}
测试是否生成了api:浏览器访问:localhost:8080/swagger-ui.html#
大约 3 分钟