概述
2019独角兽企业重金招聘Python工程师标准>>>
基于规范化的接口注释生成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
最后
以上就是酷酷小蝴蝶为你收集整理的springboot 整合 swager2的全部内容,希望文章能够帮你解决springboot 整合 swager2所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复