pom.xml
<!-- Swagger -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.6.1</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.6.1</version>
</dependency>
configuration
import static com.google.common.base.Predicates.or;
import static springfox.documentation.builders.PathSelectors.regex;
import org.springframework.boot.autoconfigure.web.BasicErrorController;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.async.DeferredResult;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import com.google.common.base.Predicate;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.RequestHandler;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
/**
* SwaggerConfig
*/
@Configuration
@EnableSwagger2
public class SwaggerConfig {
/**
* 定义api组,
*/
@Bean
public Docket innerApi() {
return new Docket(DocumentationType.SWAGGER_2)
.groupName("innerApi")
.genericModelSubstitutes(DeferredResult.class)
// .genericModelSubstitutes(ResponseEntity.class)
.useDefaultResponseMessages(false)
.forCodeGeneration(true)
.select()
.apis(RequestHandlerSelectors.basePackage("com.zybros.jinlh"))
.paths(PathSelectors.any())
.build()
.apiInfo(innerApiInfo());
}
@Bean
public Docket openApi() {
Predicate<RequestHandler> predicate = new Predicate<RequestHandler>() {
@Override
public boolean apply(RequestHandler input) {
// Class<?> declaringClass = input.declaringClass();
// if (declaringClass == BasicErrorController.class)// 排除
// return false;
// if(declaringClass.isAnnotationPresent(ApiOperation.class)) // 被注解的类
// return true;
// if(input.isAnnotatedWith(ResponseBody.class)) // 被注解的方法
// return true;
if (input.isAnnotatedWith(ApiOperation.class))//只有添加了ApiOperation注解的method才在API中显示
return true;
return false;
}
};
return new Docket(DocumentationType.SWAGGER_2)
.groupName("openApi")
.genericModelSubstitutes(DeferredResult.class)
// .genericModelSubstitutes(ResponseEntity.class)
.useDefaultResponseMessages(false)
.forCodeGeneration(false)
.select()
.apis(predicate)
.paths(PathSelectors.any())//过滤的接口
.build()
.apiInfo(openApiInfo());
}
private ApiInfo innerApiInfo() {
return new ApiInfoBuilder()
.title("JinLH inner Platform API")//大标题
.description("内部api")//详细描述
.version("1.0")//版本
.termsOfServiceUrl("NO terms of service")
.contact(new Contact("stone", "https://www.jinlh.com", "787591269@qq.com"))//作者
.license("The Apache License, Version 2.0")
.licenseUrl("http://www.apache.org/licenses/LICENSE-2.0.html")
.build();
}
private ApiInfo openApiInfo() {
return new ApiInfoBuilder()
.title("JinLH Platform API")//大标题
.description("金老虎提供的OpenAPI")//详细描述
.version("1.0")//版本
.termsOfServiceUrl("NO terms of service")
.contact(new Contact("泽佑","www.zybros.com", "787591269@qq.com"))//作者
.license("The Apache License, Version 2.0")
.licenseUrl("http://www.apache.org/licenses/LICENSE-2.0.html")
.build();
}
}
注解 API
@RestController
public class ProductApi extends BaseController {
@ApiOperation(value="根据类别获取商品", notes="需要提供类别ID")
@RequestMapping(value = "/wx/ctg_product_ctgs_list", method = {RequestMethod.POST,RequestMethod.GET})
public Map<String, Object> listWithCtgs(HttpServletRequest rqs, Category ctg) throws BusinessException {
Product p = new Product();
if (ctg.getLevel() != null) {
if (ctg.getLevel() == 1) {
p.setCategoryId1(ctg.getId());
} else if (ctg.getLevel() == 2) {
p.setCategoryId2(ctg.getId());
} else if (ctg.getLevel() == 3) {
p.setCategoryId3(ctg.getId());
}
}
ProductMapper mapper = this.getMybatisBaseService().getMapper(ProductMapper.class);
p.setShopId(ctg.getShopId());
p.setStatus(Product.Status.UP);
List<Product> list = mapper.selectList(p);
Map<String, Object> m = new HashMap<String, Object>();
m.put("products", list);
CategoryMapper mapper2 = this.getMybatisBaseService().getMapper(CategoryMapper.class);
ctg.setParentId(0);
ctg.setStatus(Category.Status.UP);
List<Category> ctgs = mapper2.selectList(ctg);
m.put("categories", ctgs);
m.put("imgHttpPath", SystemConstants.PRODUCT_IMG_HTTP_PATH);
return m;
}
@ApiOperation(value="获取商品信息", notes="需要提供商品ID")
@RequestMapping(value = "/wx/product", method = {RequestMethod.POST,RequestMethod.GET})
public Map<String, Object> get(HttpServletRequest rqs, int id) throws BusinessException {
ProductMapper ccm = this.getMybatisBaseService().getMapper(ProductMapper.class);
Product product = ccm.selectByPrimaryKey(id);
Map<String, Object> m = new HashMap<String, Object>();
m.put("product", product);
m.put("imgHttpPath", SystemConstants.PRODUCT_IMG_HTTP_PATH);
return m;
}
}
swagger UI
innerAPI
openAPI