Swagger2 快速定义API接口文档

引入依赖

<dependency>
	<groupId>io.springfox</groupId>
	<artifactId>springfox-swagger2</artifactId>
	<version>2.7.0</version>
</dependency>

<dependency>
	<groupId>io.springfox</groupId>
	<artifactId>springfox-swagger-ui</artifactId>
	<version>2.7.0</version>
</dependency>

配置

#SWAGGER
springfox.documentation.swagger.v2.path=/mmt/doc/api/notify
#扫描路径
swagger.basepackage=com.mmtvip.notifyprovider.controller
/**
 * Swagger2配置
 * @author 向振华
 * @date 2018/11/21 11:02
 */
@Configuration
@EnableSwagger2
@Profile({"dev", "test"})//在何环境显示
public class Swagger2 {

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .enable(true)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.xzh.controller"))
                .paths(PathSelectors.any())
                .build();
    }

    /**
     * 构建 api文档的详细信息函数
     *
     * @return
     */
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("标题API")
                .contact(new Contact("向振华", "网站url", "邮箱地址"))
                .version("1.0.0")
                .build();
    }

}

请求参数定义

/**
 *  请求参数
 * @author 向振华
 * @date 2018/11/21 11:13
 */
@Data
public class Model {

    @ApiModelProperty(value = "名称")
    private String name;
    @ApiModelProperty(value = "客户地区")
    private String area;

}

Controller层

/**
 * @author 向振华
 * @date 2018/11/21 11:04
 */
@Api(tags = "A服务名称")
@RequestMapping("xzh")
@Controller
public class AController {


    @ApiOperation(value = "方法名1", notes = "备注1")
    @ApiImplicitParam(name = "parm", value = "解释", paramType = "query", required = true)
    @PostMapping("/test1")
    @ResponseBody
    public String test1(Long parm) {//单个参数
        return null;
    }

    @ApiOperation(value = "方法名2")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "parm", value = "XXid数组", paramType = "query", required = true),
            @ApiImplicitParam(name = "arg", value = "ZZid数组", paramType = "query", required = true)
    })
    @PostMapping("/test2")
    @ResponseBody
    public String test2(Long[] parm, Long[] arg) {//多个参数
        return null;
    }

    @ApiOperation(value = "方法名3")
    @GetMapping("/test3")
    @ResponseBody
    public String test3(Model model) {//实体类,注解写在实体类内
        return null;
    }
}

启动服务器,访问http://localhost:8001/swagger-ui.html#/

进入页面

点开A服务

猜你喜欢

转载自blog.csdn.net/Anenan/article/details/84315242
今日推荐