Swagger 整合SpringBoot 2.0
此文章是为了记录学习,写的不好勿喷
直接上代码
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>
配置类
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;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration
@EnableSwagger2
public class Swagger2Config {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select()
.apis(RequestHandlerSelectors.basePackage("com.target.springboot_mybatis.controller"))
.paths(PathSelectors.any()).build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder().title("springboot_mybatis 整合swaager2 构建api文档")
.description("swaager2 学习 ,https://blog.****.net/qq_31897023")
.termsOfServiceUrl("https://blog.****.net/qq_31897023").version("1.0").build();
}
}
Controller层 Code
@RestController
@RequestMapping("/admin")
@Api(tags="管理员")
public class AdminController {
@Autowired
private IAdminService adminService;
@ApiOperation(value="获取用户实体",notes="根据ID获取",produces=MediaType.APPLICATION_JSON_UTF8_VALUE)
@GetMapping("/get")
@ResponseBody
private String SelectByID() {
//adminService.getById("1047").toString();
return JSON.toJSONString( adminService.getById("1047"));
}
}
项目目录结构
配置好以后启动就行了 在浏览器地址栏输入http://192.168.18.48:81/swagger-ui.html 即可出现如下页面