SpringBoot之springfox(Swagger)(ApiDoc接⼝⽂档)Springfox的前⾝是swagger-springmvc,是⼀个开源的API doc框架,可以将我们的Controller的⽅法以⽂档的形式展现,基于Swagger。官⽹地址:
1.maven依赖
<!--springfox-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.4.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.4.0</version>
</dependency>
2.配置
ample;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import t.annotation.Bean;
import t.annotation.Configuration;
import org.t.request.async.DeferredResult;
import springfox.documentation.builders.PathSelectors;
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;
@Configuration
@EnableSwagger2
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@Bean
public Docket testApi() {
return new Docket(DocumentationType.SWAGGER_2)
.groupName("test")
.genericModelSubstitutes(DeferredResult.class)
//.genericModelSubstitutes(ResponseEntity.class)
.useDefaultResponseMessages(false)
.forCodeGeneration(true)
.pathMapping("/test")//api测试请求地址
.select()
.("/common/.*"))//过滤的接⼝
.
build()
.apiInfo(testApiInfo());
}
@Bean
public Docket demoApi() {
return new Docket(DocumentationType.SWAGGER_2)
.groupName("demo")
.genericModelSubstitutes(DeferredResult.class)
// .genericModelSubstitutes(ResponseEntity.class)
.useDefaultResponseMessages(false)
.forCodeGeneration(false)
.
pathMapping("/")
.select()
.("/comm.*"))//过滤的接⼝
.build()
.apiInfo(demoApiInfo());
}
private ApiInfo testApiInfo() {
Contact contact = new Contact("王念", "my.oschina/wangnian", "2251181679@qq");
ApiInfo apiInfo = new ApiInfo("某API接⼝",//⼤标题
"REST风格API",//⼩标题
"0.1",//版本
"www.baidu",
contact,//作者
"主页",//链接显⽰⽂字
""//⽹站链接
);
return apiInfo;
}
private ApiInfo demoApiInfo() {
Contact contact = new Contact("王念", "my.oschina/wangnian", "2251181679@qq");
ApiInfo apiInfo = new ApiInfo("某API接⼝",//⼤标题
"REST风格API",//⼩标题
"0.1",//版本
"www.baidu",
contact,//作者
"主页",//链接显⽰⽂字
""//⽹站链接
);
return apiInfo;
}
}
ample.demo;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;
/**
* Created by 王念 on 2016/4/25.
*/
@RestController
@RequestMapping("comm/")
public class Controller {
/
**
* 简单接⼝描述
*
* @param userName
* @return
*/
@RequestMapping(value = "/demo1", method = RequestMethod.POST)
@ApiOperation(value = "测试接⼝1", notes = "简单接⼝描述 userName必填", code = 200, produces = "application/json")
public ModelMap getDemo(@RequestParam("userName") String userName) {
ModelMap map = new ModelMap();
map.addAttribute("userName", userName);
return map;
}
/**
* 参数描述
*
* @param userName
* @return
*/
@RequestMapping(value = "/demo2/{userName}", method = RequestMethod.POST)
@ApiOperation(value = "测试接⼝2", notes = "参数描述", code = 200, produces = "application/json")
public ModelMap getDemo2(@ApiParam(name = "userName", value = "编号", required = true) @PathVariable("userName") String userName) { ModelMap map = new ModelMap();
map.addAttribute("userName", userName);
springboot中文return map;
}
/**
* 接受对象
*
* @return
*/
@RequestMapping(value = "/demo3", method = RequestMethod.POST)
@ApiOperation(value = "测试接⼝3", notes = "接受对象", code = 200, produces = "application/json")
public ModelMap getDemoa(@RequestBody User user) {
ModelMap map = new ModelMap();
map.addAttribute("result", user);
return map;
}
@ApiIgnore //使⽤这个注解忽略这个接⼝
@RequestMapping(value = "/demo4", method = RequestMethod.POST) public ModelMap getDemob(@RequestParam String content) {
ModelMap map = new ModelMap();
map.addAttribute("result", new java.util.Date());
return map;
}
}
访问localhost:8080/swagger-ui.html 就能看到
notes⾥换⾏⽤ </br> HMLT的标签就⾏了,哈哈哈哈
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论