maven项目下
io.springfox
springfox-swagger2
2.4.0
io.springfox
springfox-swagger-ui
2.4.0
com.fasterxml.jackson.core
jackson-core
2.8.6
com.fasterxml.jackson.core
jackson-databind
2.8.6
com.fasterxml.jackson.core
jackson-annotations
2.8.6
自定义Swagger类
@EnableWebMvc
@EnableSwagger2 //使swagger2生效
@Configuration //配置注解,自动在本类上下文加载一些环境变量信息
@ComponentScan(basePackages = "com.xiao.controller") //需要扫描的包路径
public class SwaggerUtil extends WebMvcConfigurationSupport {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2).groupName("所有接口")//总分类标题
.enable(true)//true或false决定文档是否显示
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.xiao.controller"))
.paths(PathSelectors.any())
.build();
}
//
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("xiaoAPI") //子标题
.description("xiao的所有接口文档")
.contact(new Contact("自定义的名字","http://localhost:8080/swagger-ui.htm","邮箱"))
.version("1.1")
.build();
}
上面配置的作用为扫描所有的controller,总标题不分类
@EnableWebMvc
@EnableSwagger2 //使swagger2生效
@Configuration //配置注解,自动在本类上下文加载一些环境变量信息
@ComponentScan(basePackages = "com.xiao.controller") //需要扫描的包路径
public class SwaggerUtil extends WebMvcConfigurationSupport {
//利用maven环境隔离,决定在那个环境显示接口文档----true/false
private boolean swaggerShow = Boolean.parseBoolean(PropertiesUtil.getProperty("swagger.show"));
private static final String INDEX_PAGE = "http://localhost:8080/swagger-ui.htm";
private static final String BASE_PACKAGE = "com.xiao.controller";
@Bean
public Docket userDocket() {
ApiInfo apiInfo = new ApiInfoBuilder()
.title("用户信息操作")
.description("用户操作用户相关接口的文档")
.contact(new Contact("自定义的名字", INDEX_PAGE, "邮箱"))
.version("1.0")
.build();
return new Docket(DocumentationType.SWAGGER_2)
.groupName("userDocket")//总分类标题之一:userDocket
.pathMapping("/")
.enable(swaggerShow)
.select()
.apis(RequestHandlerSelectors.basePackage(BASE_PACKAGE))
.paths(PathSelectors.ant("/user/**"))//显示此路径下的所有接口
.build().apiInfo(apiInfo);
}
@Bean
public Docket shipDocket(){
ApiInfo apiInfo = new ApiInfoBuilder()
.title("购物车信息")
.description("用户购物车的相关接口文档")
.contact(new Contact("自定义的名字", INDEX_PAGE, "邮箱"))
.version("1.0")
.build();
return new Docket(DocumentationType.SWAGGER_2)
.groupName("shipDocket")//总分类标题之一:shipDocket
.pathMapping("/")
.select()
.apis(RequestHandlerSelectors.basePackage(BASE_PACKAGE))
.paths(PathSelectors.ant("/shipping/**")).build()
.apiInfo(apiInfo);
}
}
上面配置的作用会根据自己所给的路径分类显示
常用注解
- @Api()用于类名
- @ApiOperation()用于方法名
- @ApiImplicitParams()用于多参数说明
- @ApiImplicitParam()用于单参数说明
- @ApiModel()用于实体类
- @ApiModelProperty用于实体类属性
@Api(value = "UserController", description = "用户信息相关接口")
public class UserController {
@ApiOperation(value = "登录", httpMethod = "POST", notes = "用户登录文档说明", consumes = "application/Json")
@ApiImplicitParams({
@ApiImplicitParam(name = "username", value = "用户名", required = true, defaultValue = "xiao", paramType = "query", dataType = "String"),
@ApiImplicitParam(name = "password", value = "密码", required = true, defaultValue = "xiao", paramType = "query", dataType = "String")
})
@RequestMapping(value = "login", method = RequestMethod.POST)
@ResponseBody
//登录接口
public ServerResponse login(String username, String password, HttpSession session, HttpServletResponse httpServletResponse, HttpServletRequest request) {
}
}
http://localhost:8080/swagger-ui.html
或者为:http://ip:prot/project_name/swagger-ui.html
借鉴链接:https://blog.csdn.net/gg_and_dd/article/details/79661824
https://blog.csdn.net/yoursongs/article/details/79605116
阿里服务器,新用户(可找未注册的手机)一年最低102,点击链接去阿里官方购买:https://www.aliyun.com/minisite/goods?userCode=k84mxnzl