springboot增加swagger功能

第一步:

配置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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值