给controller层生成文档
1. pom.xml
<!--swagger--> <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>
2. 写配置文件 conf层 SwaggerConfig
@EnableSwagger2 @Configuration public class SwaggerConfig { @Bean public Docket customDocket() { ApiInfo apiInfo = new ApiInfoBuilder() //文档说明 .title("欣知测试专用") //文档版本说明 .version("1.0.0") .description("欣知学习测试专用") .license("Apache 2.0") .build(); return new Docket(DocumentationType. SWAGGER_2) .apiInfo(apiInfo) .select() .apis(RequestHandlerSelectors.basePackage("com.xinzhi.studyspringboot.controller")) .build(); } }
3. 打不开swagger-ui.html
https://www.cnblogs.com/luoluocaihong/p/7106276.html
public class MvcConfig extends WebMvcConfigurationSupport 中添加
@Override protected void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("swagger-ui.html") .addResourceLocations("classpath:/META-INF/resources/"); registry.addResourceHandler("/webjars/**") .addResourceLocations("classpath:/META-INF/resources/webjars/"); super.addResourceHandlers(registry); }
成功:
4.使用swagger + 配置swagger(重点)
(1)增删改查来一套
/** * @author sr * @date 2021/1/28 */ @Controller @RequestMapping("/user") public class UserController { @Resource private IUserService userService; @GetMapping @ResponseBody public List<User> getUsers(){ return userService.queryUserList(); } @GetMapping("/{id}") @ResponseBody public User getUsersById(@PathVariable int id) { return new User(id,"sunrui","123"); } @PostMapping("/add") @ResponseBody public String addUsers(String username,String password){ return "success"; } @PutMapping @ResponseBody public String updateUsers(){ return "success"; } @DeleteMapping("/{id}") @ResponseBody public String deleteUsers(@PathVariable int id){ return "success"; } }
(2)
(3)但是这个页面也没什么说明,不太友好,添加一些说明
https://www.cnblogs.com/yichunguo/p/12665857.html