To all : 除了Group下拉分组,有对接口进行分组的方法麻烦告知下谢谢!
1.导入swagger2包
io.springfox
springfox-swagger2
2.6.1
io.springfox
springfox-swagger-ui
2.6.1
2.启动类Application加入注解@EnableSwagger2开启swagger
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@SpringBootApplication
@EnableSwagger2
public class ServiceApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceApplication.class,args);
}
}
3.新增Swagger配置类(..*请替换为对应的Controller包路径)
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.async.DeferredResult;
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;
/**
* @Description:Swaggerd配置
* @Author: lifay
* @datetime:2019/5/23 10:55
*
*/
@Configuration
public class SwaggerConfig {
/*
* 定义api分组
* */
@Bean
public Docket sysApi() {
return new Docket(DocumentationType.SWAGGER_2)
.groupName("第一个分组")
.genericModelSubstitutes(DeferredResult.class)
.useDefaultResponseMessages(false)
.apiInfo(sysApiInfo())
.forCodeGeneration(true)
.select()
.apis(RequestHandlerSelectors.basePackage("*.*.*"))
.paths(PathSelectors.any())
.build();
}
@Bean
public Docket prjApi() {
return new Docket(DocumentationType.SWAGGER_2)
.groupName("第二个分组")
.genericModelSubstitutes(DeferredResult.class)
.useDefaultResponseMessages(false)
.forCodeGeneration(false)
.select()
.apis(RequestHandlerSelectors.basePackage("*.*.*.*"))
.paths(PathSelectors.any())//过滤的接口
.build()
.apiInfo(prjApiInfo());
}
/*
* 定义对应api
* */
private ApiInfo sysApiInfo() {
return new ApiInfoBuilder()
.title("标题1")
.description("SDK-Api")
.termsOfServiceUrl("")
.contact("Java Team")
.version("1.0")
.build();
}
private ApiInfo prjApiInfo() {
return new ApiInfoBuilder()
.title("标题2")
.description("SDK2-Api")
.termsOfServiceUrl("")
.contact("Java Team")
.version("1.0")
.build();
}
}
4.控制类加上注解@Api(description = "***"),自行添加描述信息,不予添加的类请加@ApiIgnore进行过滤
5.方法上加注解(DEMO限制了get/post两个请求方式),其他信息参照成功后的样例对比一下就知道了
@RequestMapping(value = "/getTest",method = {RequestMethod.GET,RequestMethod.POST})
@ApiOperation(value = "测试接口",notes = "备注信息")
@ApiImplicitParam(name = "id", value = "参数ID",defaultValue = "1",required = true, dataType = "string",paramType = "query")
@ApiResponses({@ApiResponse(code= 0,message="请求成功",response= RetMsg.class),
@ApiResponse(code =1,message="请求失败",response=RetMsg.class)
})
public RetMsg getTest(String id){
/*RetMsg 采用了泛型,添加具体返回的实体,用来做model/example字段说明*/
System.out.println("------请求参数:" + id + "--------");
RetMsg retMsg = new RetMsg("0","请求成功");
TestItem item = new TestItem("2","华为","Note");
retMsg.setData(item);
return retMsg;
}
6.RetMsg,TestItem代码
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import java.io.Serializable;
@ApiModel
public class RetMsg implements Serializable {
@ApiModelProperty(value = "返回状态码 0-成功 其他-失败",example = "0/1")
private String status;
@ApiModelProperty(value = "返回消息 成功 失败",example = "请求成功/请求失败")
private String message;
@ApiModelProperty(value = "返回数据")
private T data;
public RetMsg() {
}
public RetMsg(String status, String message) {
this.setMessage(message);
this.setStatus(status);
}
public String getStatus() {
return this.status;
}
public void setStatus(String status) {
this.status = status;
}
public String getMessage() {
return this.message;
}
public void setMessage(String message) {
this.message = message;
}
public T getData() {
return this.data;
}
public void setData(T data) {
this.data = data;
}
}
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
/**
* @Description: 返回model信息
* @Author: lifay
* @datetime:2019/5/30 10:38
*
*/
@ApiModel
public class TestItem {
@ApiModelProperty(value = "ID",example = "1")
private String id;
@ApiModelProperty(value = "名称",example = "工程")
private String name;
@ApiModelProperty(value = "备注",example = "53834531eb")
private String memo;
public TestItem(String id, String name, String memo) {
this.id = id;
this.name = name;
this.memo = memo;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getMemo() {
return memo;
}
public void setMemo(String memo) {
this.memo = memo;
}
7.浏览器打开地址,注意自己修改端口号,可以看到示例和model及字段等注释
http://localhost:8088/swagger-ui.html
请求界面,测试参数默认加载为1
点击Try it out!