本篇文章为大家展示了SpringBoot2中怎么利用swagger2构建接口管理界面,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。一、Swagger2简介1、Swagger2优点整合到Spring
本篇文章为大家展示了SpringBoot2中怎么利用swagger2构建接口管理界面,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。
整合到Spring Boot中,构建强大RESTful api文档。省去接口文档管理工作,修改代码,自动更新,Swagger2也提供了强大的页面测试功能来调试RESTful API。
Api:修饰整个类,描述Controller的作用Apioperation:描述一个类的一个方法,或者说一个接口ApiParam:单个参数描述ApiModel:用对象来接收参数ApiProperty:用对象接收参数时,描述对象的一个字段ApiResponse:Http响应其中1个描述ApiResponses:HTTP响应整体描述ApiIgnore:使用该注解忽略这个APIApiError :发生错误返回的信息ApiImplicitParam:一个请求参数ApiImplicitParams:多个请求参数
spring-boot:2.1.3.RELEASEswagger:2.6.1
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;@Configurationpublic class SwaggerConfig { @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.basePackage("com.swagger.two")) .paths(PathSelectors.any()) .build(); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("SpringBoot利用Swagger构建API文档") .description("使用RestFul风格, 创建人:知了一笑") .termsOfServiceUrl("https://GitHub.com/cicadasmile") .version("version 1.0") .build(); }}
@EnableSwagger2@SpringBootApplicationpublic class SwaggerApplication { public static void main(String[] args) { SpringApplication.run(SwaggerApplication.class,args) ; }}
(1)、代码块
@ApiOperation(value="添加用户", notes="创建新用户")@ApiImplicitParam(name = "user", value = "用户详细实体user", required = true, dataType = "User")@RequestMapping(value = "/addUser", method = RequestMethod.POST)public ResponseEntity<JSONResult> addUser (@RequestBody User user){ jsonResult result = new JsonResult(); try { users.put(user.getId(), user); result.setResult(user.getId()); result.setStatus("ok"); } catch (Exception e) { result.setResult("服务异常"); result.setStatus("500"); e.printStackTrace(); } return ResponseEntity.ok(result);}
(2)、效果图
(1)、代码块
@ApiOperation(value="用户列表", notes="查询用户列表")@RequestMapping(value = "/getUserList", method = RequestMethod.GET)public ResponseEntity<JsonResult> getUserList (){ JsonResult result = new JsonResult(); try { List<User> userList = new ArrayList<>(users.values()); result.setResult(userList); result.setStatus("200"); } catch (Exception e) { result.setResult("服务异常"); result.setStatus("500"); e.printStackTrace(); } return ResponseEntity.ok(result);}
(2)、效果图
(1)、代码块
@ApiOperation(value="用户查询", notes="根据ID查询用户")@ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Integer", paramType = "path")@RequestMapping(value = "/getUserById/{id}", method = RequestMethod.GET)public ResponseEntity<JsonResult> getUserById (@PathVariable(value = "id") Integer id){ JsonResult result = new JsonResult(); try { User user = users.get(id); result.setResult(user); result.setStatus("200"); } catch (Exception e) { result.setResult("服务异常"); result.setStatus("500"); e.printStackTrace(); } return ResponseEntity.ok(result);}
(2)、效果图
(1)、代码块
@ApiOperation(value="更新用户", notes="根据Id更新用户信息")@ApiImplicitParams({ @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Long",paramType = "path"), @ApiImplicitParam(name = "user", value = "用户对象user", required = true, dataType = "User")})@RequestMapping(value = "/updateById/{id}", method = RequestMethod.PUT)public ResponseEntity<JsonResult> updateById (@PathVariable("id") Integer id, @RequestBody User user){ JsonResult result = new JsonResult(); try { User user1 = users.get(id); user1.setUsername(user.getUsername()); user1.setAge(user.getAge()); users.put(id, user1); result.setResult(user1); result.setStatus("ok"); } catch (Exception e) { result.setResult("服务异常"); result.setStatus("500"); e.printStackTrace(); } return ResponseEntity.ok(result);}
(2)、效果图
(1)、代码块
@ApiOperation(value="删除用户", notes="根据id删除指定用户")@ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Long", paramType = "path")@RequestMapping(value = "/deleteById/{id}", method = RequestMethod.DELETE)public ResponseEntity<JsonResult> deleteById (@PathVariable(value = "id") Integer id){ JsonResult result = new JsonResult(); try { users.remove(id); result.setResult(id); result.setStatus("ok"); } catch (Exception e) { result.setResult("服务异常"); result.setStatus("500"); e.printStackTrace(); } return ResponseEntity.ok(result);}
(2)、效果图
上述内容就是SpringBoot2中怎么利用Swagger2构建接口管理界面,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注编程网精选频道。
--结束END--
本文标题: SpringBoot2中怎么利用Swagger2构建接口管理界面
本文链接: https://lsjlt.com/news/230091.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0