摘要:每次写的接口说明,以前是一个一个写文档给测试,麻烦,现在整入swagger,就不用了
依赖包
maven依赖
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.8.0</version>
</dependency>
gradle依赖
compile('io.springfox:springfox-swagger2:2.8.0')
compile('io.springfox:springfox-swagger-ui:2.8.0')
swagger配置类
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
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;
import springfox.documentation.service.Contact;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
/**
* Created on 2018/7/27
*/
@Configuration
@EnableSwagger2 //此注解也可直接加在启动类上面
public class TestSwagger{
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
// 自行修改为自己的包路径
.apis(RequestHandlerSelectors.basePackage("com.yf.af.production.controller"))
.paths(PathSelectors.any())
.build();
}
//底下设置的参数会显示在页面上面,自行选择定义哪些
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("api 接口")
.description("rest服务说明")
.version("2.0")
.contact(new Contact("liu", "url", "123qq@.com"))
.build();
}
}
controller使用
具体还有哪些注解及作用,自己百度一大堆
@RestController
@Api("HelloController 相关接口说明")
public class HelloController {
/*
@Api:修饰整个类,描述Controller的作用
@ApiOperation:描述一个类的一个方法,或者说一个接口
@ApiParam:单个参数描述
@ApiModel:用对象来接收参数
@ApiProperty:用对象接收参数时,描述对象的一个字段
@ApiResponse:HTTP响应其中1个描述
@ApiResponses:HTTP响应整体描述
@ApiIgnore:使用该注解忽略这个API
@ApiError :发生错误返回的信息
@ApiImplicitParam:一个请求参数
@ApiImplicitParams:多个请求参数
*/
@Autowired
private RestTemplate restTemplate;
@Value("${rest.path}")
private String path;
@ApiOperation("测试web模块是否启动")
@GetMapping("/hello")
public String hello(){
return "hello springboot";
}
@GetMapping("/rest")
@ApiOperation(value="测试java调用rest https服务", notes = "测试java调用rest https服务")
public String rest(){
String forObject = restTemplate.getForObject("https://ip地址/cxf/oss/token", String.class);
return forObject;
}
/*
查询某个设备信息
*/
@ApiImplicitParam(name="id", value="id", required = true, dataType = "Integer" )
@ApiOperation(value="查询某个设备信息", notes = "查询某个设备信息 1 ")
@GetMapping("/getDevice/{id}")
public String getDevice(@PathVariable int id){
String forObject = restTemplate.getForObject(path+"/device/"+ id +"?at=asILk9y2JB4bxh311DIKMr2Oaif1L6pn", String.class);
return forObject;
}
@PostMapping("/addDevice")
public String addDevice(@Valid Device device){
Device body = restTemplate.postForEntity(path+"/device?at=asILk9y2JB4bxh311DIKMr2Oaif1L6pn",
device, Device.class).getBody();
return body.toString();
}
/*
修改
*/
@PutMapping("/updateDevice")
public String updateDevice(@Valid Device device){
restTemplate.put(path+"/cxf/device/"+ device.getId() +"?at=asILk9y2JB4bxh311DIKMr2Oaif1L6pn",device);
return "修改成功";
}
/*
删除
*/
@DeleteMapping("/delDevice/{id}")
public String delDevice(@PathVariable int id){
restTemplate.delete(path+"/device/"+ id +"?at=asILk9y2JB4bxh311DIKMr2Oaif1L6pn");
return "删除成功";
}
}
页面效果
启动完了之后,页面输入
http://localhost:8080/swagger-ui.html
如果springboot加了context-path属性的,把该路径拼在端口号后面即可
要测试 ,选择一个之后,点击 Try it out 按钮,参数类型,什么的都有