SpringBoot 集成swagger 曾经终败给现在 2022-05-14 16:13 348阅读 0赞 ### Swagger ### Swagger的功能:一款可以生成,调用,可视化api的框架。让你不再忘记接口长啥样的工具,让不再为测试而烦恼,因为Swagger集成了这些功能,而且你还可以给api接口加上功能描述,注意事项等等。 结果是这样子的,你可以给接口加描述,调用,测试。 ![watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQwMDA4NTM1_size_27_color_FFFFFF_t_70][] 一:添加依赖 <!-- swagge依赖 --> <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> 二:添加swagger配置类 @Configuration @EnableSwagger2//启用swagger public class swaggerConfig { @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() //扫描的包 .apis(RequestHandlerSelectors.basePackage("com.tom.controller")) .paths(PathSelectors.any()) .build(); } private ApiInfo apiInfo() { return new ApiInfoBuilder() //标题 .title("springboot利用swagger构建api文档") //描述 .description("简单优雅的restfun风格") //版本号 .version("1.0") .build(); } } 三:描述Controller 其实第二步,就已经配好了,也可以正常使用swagger的功能了,当然你的controller包下面必须有可用的Controller。 链接:[http://localhost:8080/swagger-ui.html][http_localhost_8080_swagger-ui.html] @RestController @RequestMapping("/teacher") public class teachController { @Autowired private teachService ts; @GetMapping("/{id}") public teacher get(@PathVariable("id") int id) { return ts.getById(id); } @PostMapping("/save") public String save(teacher t) { ts.save(t); return "保存成功"; } @GetMapping public List<teacher> getAll() { return ts.getAll(); } @GetMapping("/del/{id}") public void delete(@PathVariable("id") int id) { ts.delete(id); } } 我的controller上什么都没有加,也可以正常使用。如果你需要加描述的话,可以用下列的注解,这些为常用的 @Api:描述controller @ApiOperation:描述一个接口/方法的作用 @ApiParam:描述一个参数 @ApiModel:用对象类进行接收参数 @ApiProperty:描述对象的一个字段 [watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQwMDA4NTM1_size_27_color_FFFFFF_t_70]: /images/20220503/96f05622fd9049e4b59dc5bba2d429e1.png [http_localhost_8080_swagger-ui.html]: http://localhost:8080/swagger-ui.html#/
还没有评论,来说两句吧...