springboot 整合 swager2

基于规范化的接口注释生成API说明文档和在线接口调试

引入依赖包

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

创建swager配置类

/**
 * @Author: WuChao
 * @ClassName:com.yuuxin.wx.common
 * @Description:
 * @date 2018/7/3
 */
@Configuration
@EnableSwagger2
public class Swagger2 {
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.yuuxin.wx.web.APIServer"))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("云朵课堂api说明文档")
                .description("云朵课堂官方API说明文档")
                .termsOfServiceUrl("")
                .version("1.0")
                .build();
    }

}

实际中使用

/**
 * @Author: WuChao
 * @ClassName:com.yuuxin.wx.web
 * @Description:测试swager说明文档API
 * @date 2018/7/3
 */
@RestController
@RequestMapping(value="/api")
public class TestApiController {

    @ApiOperation(value="书本查询接口", notes="根据Book对象查询")
    @ApiImplicitParam(name = "id", value = "图书id", required = true, dataType = "Integer")
    @RequestMapping(value="queryBook", method=RequestMethod.GET)
    public String postUser(Integer id){
        System.out.println("图书查询中.....");
        return "success";
    }


}

API查看:本地查看地址

实际效果: 文档首页

接口说明页面,和接口测试

详细写法

@RestController
@RequestMapping("/message")
public class MessageServer {

    @ApiOperation(value="邮件发送WEBAPI接口", notes="未提供服务时默认选择sendCloud发送")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "subject", value = "发送邮件的主题", required = true, dataType = "String"),
            @ApiImplicitParam(name = "from", value = "发件人地址", required = true, dataType = "String"),
            @ApiImplicitParam(name = "fromName", value = "发件人名称", required = true, dataType = "String"),
            @ApiImplicitParam(name = "to", value = "收件人--多个人以' ; ' 分割", required = true, dataType = "String"),
            @ApiImplicitParam(name = "content", value = "内容(html或者txt其中一些类似<body>的标签或者错误的转移字符可能导致json转化失败,发送失败)", required = true, dataType = "String"),
            @ApiImplicitParam(name = "provider", value = "邮件服务商", required = false, dataType = "String"),
            @ApiImplicitParam(name = "template", value = "使用的模板,不填写即是不使用模板", required = false, dataType = "String"),
            @ApiImplicitParam(name = "copyTo", value = "抄送--多个地址使用';'分隔", required = false, dataType = "String"),
            @ApiImplicitParam(name = "hasfile", value = "是否含有附件 0 不包含 1包含 附件大小不得超过2M", required = false, dataType = "String"),
            @ApiImplicitParam(name = "secretTo", value = "密送人-- 多个地址使用';'分隔", required = false, dataType = "String"),
            @ApiImplicitParam(name = "singleShow", value = "群发单显(sendCloud服务暂时不支持该功能)", required = false, dataType = "String"),
    })
    @ApiResponses({
            @ApiResponse(code = APIConstant.SUCCESS_CODE,message = "请求成功"),
            @ApiResponse(code = APIConstant.ERROR_PARAMETERS_CODE,message = "参数错误"),
            @ApiResponse(code = APIConstant.NO_PERMISSIONS_CODE,message = "ip没有权限"),
            @ApiResponse(code = APIConstant.SERVER_ERROR_CODE,message = "服务异常"),
            @ApiResponse(code = APIConstant.AGENT_ERROR_CODE,message = "服务运行商接口返回异常")
    })
    @RequestMapping(value = "sendmail",produces = "application/json;charset=UTF-8",method = RequestMethod.POST)
    public APIResult sendMail(CompanyEmail companyEmail, HttpServletRequest request, MultipartFile file){
        APIResult result = new APIResult();

        result=MailCheck.CheckIp(request,result);

        if (result.getStatusCode()!=APIConstant.SUCCESS_CODE){
            return result;
        }

         result = MailCheck.CheckParams(companyEmail, file,result);

        if (result.getStatusCode()!=APIConstant.SUCCESS_CODE){
            return result;
        }
        result = MailSend.send(companyEmail, result);

        return result;
    }
	}

转载于:https://my.oschina.net/fusublog/blog/1839831

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值