第一步:
配置maven项:
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.8.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.8.0</version>
<scope>compile</scope>
</dependency>
第二步:
增加swagger配置项:
/**
* Swagger2的配置类
* written by zhanggong
*/
@Configuration
@EnableSwagger2 // 是否开启swagger,正式环境可以关闭
public class SwaggerConfig {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.basePackage("xx.xx.controller")) //要扫描的Controller层
.paths(PathSelectors.any()) // and by paths
.build()
.apiInfo(apiInfo());
}
// 构建 api文档的详细信息函数,注意这里的注解引用的是哪个
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("WeatherForecastService天气预测服务")
.description("Author:zeus") // 任意,请稍微规范点
.termsOfServiceUrl("http://localhost:12121/swagger-ui.html") // 将“url”换成自己的ip:port
.version("1.0.0")
.build();
}
}
第三步:
修改对应项controller层:
@RestController
@RequestMapping("/api")
@Api(value = "/api")
// 表示整个Controller的方法返回值都是json或json对象
public class Data2CsvController {
@GetMapping("/test")
@ApiOperation(value = "测试接口", notes="无参数测试接口")
public String Hello(){
return "Test-Controller";
}
//处理请求地址映射的注解
@Autowired
private DataProcessService dpService = new DataProcessService();
private DataProcessUtil dpUtil = new DataProcessUtil();
@**ApiOperation**(value="天气预报15天数据写入CSV文件")
@**ApiImplicitParams**({
@**ApiImplicitParam**(name="lat1", value="A纬度", required = true, dataType = "String"),
@**ApiImplicitParam**(name="lon1", value="A经度", required = true, dataType = "String"),
@**ApiImplicitParam**(name="lat2", value="C纬度", required = true, dataType = "String"),
@**ApiImplicitParam**(name="lon2", value="C经度", required = true, dataType = "String")})
@RequestMapping(value = "/forecast15days", method = RequestMethod.POST)
public String Forecast15Days(@RequestParam String lat1, @RequestParam String lon1, @RequestParam String lat2, @RequestParam String lon2)throws Exception{
String[] strings = new String[]{lat1, lon1, lat2, lon2};
// String[] strings = new String[]{"22.599", "101.579", "22.800", "101.777"};
Vector<Point> points = dpUtil.GetCoordinate(dpUtil.Str2Double(strings));
ArrayList<ArrayList<String>> data = dpService.Point2Forecast15DaysData(points);
dpService.Array2CSV(data, "/Users/zhanggong/Desktop/node_TQYB15T.csv");
return "接口调用成功!";
}
第四步:
运行项目,打开网址http://localhost:12121/swagger-ui.html即可调试API