swagger-bootstrap-ui是 Swagger 的增强UI 实现,使文档更友好一点儿,同时可以提供离线的md文档。自己调试的时候可视化接口文档,自动生成的。如果加上搜索功能就更好了。
引入swagger
在pom.xml文件中引入swagger以及ui的jar包依赖
1.引入库
<dependency><!-- 文档 -->
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency><!-- 文档 -->
<groupId>com.github.xiaoymin</groupId>
<artifactId>swagger-bootstrap-ui</artifactId>
<version>1.9.6</version>
</dependency>
2.配置configuration
配置swagger的启用配置文件,关键注解 @EnableSwagger2
一下配置是支持接口分组的配置,如果没有分组配置,只需要创建一个 Docket 即可
@Configuration
@EnableSwagger2
public class SwaggerConfiguration {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.groupName("资源管理")
.select()
.apis(RequestHandlerSelectors.basePackage("com.lishiots.dc.baseinfo.ctl"))
.paths(PathSelectors.any())
.build();
}
@Bean
public Docket createMonitorRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.groupName("实时监测")
.select()
.apis(RequestHandlerSelectors.basePackage("com.lishiots.dc.monitor.ctl"))
.paths(PathSelectors.any())
.build();
}
@Bean
public Docket createActivitiRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.groupName("工作流引擎")
.select()
.apis(RequestHandlerSelectors.basePackage("com.lishiots.dc.activiti.ctl"))
.paths(PathSelectors.any())
.build();
}
@Bean
public Docket createBaseRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.groupName("kernel模块")
.select()
.apis(RequestHandlerSelectors.basePackage("com.lishiots.dc.kernel.ctl"))
.paths(PathSelectors.any())
.build();
}
@Bean
public Docket createComplaintRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.groupName("投诉管理")
.select()
.apis(RequestHandlerSelectors.basePackage("com.lishiots.dc.complaint.ctl"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("swagger RESTful APIs")
.description("swagger RESTful APIs")
.termsOfServiceUrl("http://www.test.com/")
.contact("[email protected]")
.version("1.0")
.build();
}
}
3.Controller层使用swagger注解
@Api(tags = "banner管理")
@RestController
@RequestMapping("/api/bannerInfo")
public class BannerCtl {
@ApiOperation(value = "前端注册接口")
@ApiImplicitParams({
@ApiImplicitParam(name="name",value="登录用户名",required=true),
@ApiImplicitParam(name="username",value="公司联系人",required=true),
@ApiImplicitParam(name="password",value="密码",required=true),
@ApiImplicitParam(name="mobile",value="手机号",required=true),
@ApiImplicitParam(name="company",value="公司名称,如果是非公司类型默认传 ‘公司名称’"),
@ApiImplicitParam(name="address",value="公司地址"),
@ApiImplicitParam(name="city",value="出发地",required=true),
@ApiImplicitParam(name="xian1",value="出发地id",required=true),
@ApiImplicitParam(name="type",value="类别;1物流公司,2车主,3发货企业或个人,4,配货信息部,5国际物流企业,6快递公司,7搬家公司,8物流设备企业,9物流园区,10停车场,默认为100是只注册而未填写任何信息的账户,对应数据库字段为gslx",required=true),
@ApiImplicitParam(name="code",value="验证码",required=true)
})
@PostMapping("/useregister")
@ResponseBody
public ResponseResult<Integer> useregister (String username,String name,String mobile,Integer xian1,String password,
String address,String company,String city,Integer type,String code) {
User us=new User();
//有公司的添加公司
//System.err.println(company);
String phonecode=(String) redisUtil.get("yanzhengma"+mobile);
LOGGER.info("新用户注册对比验证码{}",phonecode);
if(!code.equals(phonecode)){
throw new WarnException("验证码错误");
}else{
if(company!=null&&!company.equals("公司名称")){
company p=new company();
p.setCtoname(company);
p.setHylx(0);
p.setLianxiren(username);
p.setTel(mobile);
p.setWx(mobile);
p.setXian1(xian1);
p.setXiancn(city);
p.setXxaddress(address);
//注册人姓名
p.setName(name);
p.setGslx(type);
companyservice.saveCompany(p);
company c=companyservice.Getbyname(name);
us.setCompanyid(c.getId());
us.setCompany(company);
}
//添加用户
us.setName(name);
us.setMobile(mobile);
us.setPassword(MD5Utils.getMd5Password(password));
us.setType(type);
us.setAddress("");
//出发地 id
us.setBranchid(xian1);
us.setDelFlag(0);
us.setCity(city);
//非会员
us.setPermissionid("0");
us.setHeadimgurl("");
userService.addUser(us);
redisUtil.del("yanzhengma"+mobile);
//session.setAttribute(Constant.USER_TOKEN, us);
return new ResponseResult<Integer>(SUCCESS);
}
}
}
接口访问在浏览器输入: http:// h o s t : {host}: host:{port}/doc.html
总结
.访问不到接口文档界面白版?一般是被拦截了(shiro或springsecurity机制)或者是配置错误import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import springfox.documentation.spring.web.SpringfoxWebMvcConfiguration;
@SpringBootApplication
@ConditionalOnClass(SpringfoxWebMvcConfiguration.class)
public class SwaggerBootstrapUiDemoApplication implements WebMvcConfigurer{
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
}
}
或者
排除静态文件
registry.addResourceHandler("swagger-ui.html")
.addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/");