1.在pom.xml中添加依賴包
<!-- swagger --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-boot-starter</artifactId> <version>3.0.0</version> </dependency>
2.添加swagger配置文件
package com.llltony.springboot.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.oas.annotations.EnableOpenApi; import springfox.documentation.service.ApiInfo; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; /** * 基於Swagger生成API文檔 * * @EnableOpenApi:啟動OpenApi的類 * @author: lll */ @Configuration @EnableOpenApi public class SwaggerConfiguration { @Bean public Docket createRestApi() { return new Docket(DocumentationType.OAS_30).apiInfo(apiInfo()).select() .apis(RequestHandlerSelectors.basePackage("com.llltony.springboot.controller")).paths(PathSelectors.any()) .build(); } private ApiInfo apiInfo() { return new ApiInfoBuilder().title("Tsl API文檔").description("Tsl API文檔") // .contact(new Contact("聯系人", "www.baidu.com", "286279186@qq.com")) .version("1.0").build(); } }
3.在bean文件中添加對應的swagger注解ApiModel和ApiModelProperty
package com.llltony.springboot.bean; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; import java.io.Serializable; import java.util.List; @ApiModel(value = "EmployeeVo", description = "EmployeeVo類") public class EmployeeVo implements Serializable { @ApiModelProperty(value = "員工隊列") List<Employee> employeeLst; public void setEmployeeLst(List<Employee> employeeLst) { this.employeeLst = employeeLst; } public List<Employee> getEmployeeLst() { return employeeLst; } }
4.在controller中添加swagger注解Api和ApiOperation
package com.llltony.springboot.controller; import com.llltony.springboot.bean.Employee; import com.llltony.springboot.bean.EmployeeVo; import com.llltony.springboot.bean.ResultMap; import com.llltony.springboot.service.EmployeeService; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.web.bind.annotation.*; import javax.annotation.Resource; import java.io.IOException; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.UUID; @RestController @RequestMapping("/employee") @Api(tags = "員工") public class EmployeeController { @Resource EmployeeService employeeService; @Resource RabbitTemplate rabbitTemplate; //增加學生 @RequestMapping(value = "/", method = RequestMethod.POST) @ApiOperation(value="增加學生",notes = "增加學生信息") public ResultMap saveEmp(@RequestBody EmployeeVo employeeVo) { ResultMap resultMap = new ResultMap(); try { employeeService.saveEmp(employeeVo); resultMap.setStatus("200"); resultMap.setMessage("保存成功"); } catch (Exception e) { resultMap.setStatus("500"); resultMap.setMessage("保存失敗"); } return resultMap; } //通過消息隊列異步增加學生 @RequestMapping(value = "/addRebbitMq", method = RequestMethod.POST) @ApiOperation(value="通過消息隊列異步增加學生",notes = "通過消息隊列異步增加學生") public ResultMap addRebbitMq(@RequestBody EmployeeVo employeeVo) { ResultMap resultMap = new ResultMap(); try { String messageId = String.valueOf(UUID.randomUUID()); String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")); Map<String, Object> map = new HashMap<>(); map.put("messageId", messageId); map.put("messageData", employeeVo); map.put("createTime", createTime); //將消息攜帶綁定鍵值:TestDirectRouting 發送到交換機TestDirectExchange rabbitTemplate.convertAndSend("EmployeeDirectExchange", "EmployeeDirectRouting", map); // employeeService.saveEmp(employeeVo); resultMap.setStatus("200"); resultMap.setMessage("保存成功"); } catch (Exception e) { resultMap.setStatus("500"); resultMap.setMessage("保存失敗"); } return resultMap; } //刪除學生 @DeleteMapping("/{ids}") @ApiOperation(value="刪除學生",notes = "刪除學生") public ResultMap delEmp(@PathVariable("ids") String ids) { ResultMap resultMap = new ResultMap(); try { employeeService.delEmp(ids); resultMap.setStatus("200"); resultMap.setMessage("刪除成功"); } catch (Exception e) { resultMap.setStatus("500"); resultMap.setMessage("刪除失敗"); } return resultMap; } //查詢學生 @GetMapping("/{id}") @ApiOperation(value="查詢學生",notes = "查詢學生") public Employee getEmp(@PathVariable("id") Integer id) throws IOException { return employeeService.getEmpById(id); } //查詢所有的學生 @GetMapping("/getAll") @ApiOperation(value="查詢所有的學生",notes = "查詢所有的學生") public List<Employee> getAllEmp() { return employeeService.getAllEmp(); } //修改學生 @PutMapping("/") @ApiOperation(value="修改學生",notes = "修改學生") public ResultMap updateEmp(@RequestBody Employee employee) { ResultMap resultMap = new ResultMap(); try { employeeService.updateEmp(employee); resultMap.setStatus("200"); resultMap.setMessage("保存成功"); } catch (Exception e) { resultMap.setStatus("500"); resultMap.setMessage("保存失敗"); } return resultMap; } }
5.配置完成,在瀏覽器中請求:http://127.0.0.1:8181/swagger-ui/index.html
6.注意事項
如果使用了默認的訪問靜態配置:static-locations: classpath:html,會導致訪問swagger報錯。
解決這個問題需要自己重寫靜態資源訪問方法,代碼如下:
package com.llltony.springboot.config; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport; public class WebConf extends WebMvcConfigurationSupport { @Override protected void addResourceHandlers(ResourceHandlerRegistry registry) { //映射static路徑的請求到static目錄下 registry.addResourceHandler("/static/**") .addResourceLocations("classpath:/static/"); super.addResourceHandlers(registry); } }
7.資源
源碼:https://github.com/CodingPandaLLL/tsl.git