如何集成swagger2构建Restful API
这篇文章主要介绍了如何集成swagger2构建RestfulAPI,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
在pom.xml中进行版本管理
2.8.0
给taosir-api的pom.xml中添加依赖配置
io.springfox springfox-swagger2 ${swagger.version} io.springfox springfox-swagger-ui ${swagger.version}
添加配置类
packagecn.taosir.api.config;
importorg.springframework.context.annotation.Bean;
importorg.springframework.context.annotation.Configuration;
importspringfox.documentation.builders.ApiInfoBuilder;
importspringfox.documentation.builders.PathSelectors;
importspringfox.documentation.builders.RequestHandlerSelectors;
importspringfox.documentation.service.ApiInfo;
importspringfox.documentation.spi.DocumentationType;
importspringfox.documentation.spring.web.plugins.Docket;
importspringfox.documentation.swagger2.annotations.EnableSwagger2;
@EnableSwagger2
@Configuration
publicclassSwaggerConfiguration{
@Bean
publicDocketcreateRestApi(){
returnnewDocket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
//控制暴露出去的路径下的实例
//如果某个接口不想暴露,可以使用以下注解
//@ApiIgnore这样,该接口就不会暴露在swagger2的页面下
.apis(RequestHandlerSelectors.basePackage("cn.taosir.api.controller"))
.paths(PathSelectors.any())
.build();
}
privateApiInfoapiInfo(){
returnnewApiInfoBuilder()
.title("涛先森系统入口业务测试")
.version("1.0")
.description("API描述")
.build();
}
}
为控制层添加相应注解
packagecn.taosir.api.controller.dreamhouse;
importorg.springframework.beans.factory.annotation.Autowired;
importorg.springframework.web.bind.annotation.RequestMapping;
importorg.springframework.web.bind.annotation.RequestMethod;
importorg.springframework.web.bind.annotation.RestController;
importcn.taosir.service.dreamHouse.UserService;
importio.swagger.annotations.Api;
importio.swagger.annotations.ApiOperation;
@RestController
@Api(value="用户管理",tags={"用户的接口"})
publicclassUserController{
@Autowired
privateUserServiceuserService;
@ApiOperation(value="测试方法",notes="测试是否成功使用服务发现")
@RequestMapping(value="/test",method=RequestMethod.GET)
publicStringtest(){
returnuserService.test();
}
}
按顺序启动
taosir-eureka注册中心
taosir-dreamHouse服务提供者
taoisr-api服务消费者
访问地址http://localhost:8765/swagger-ui.html#
以上,集成swagger2构建RestfulAPI
下面附上注解参考表
@Api:用在请求的类上,表示对类的说明 tags="说明该类的作用,可以在UI界面上看到的注解" value="该参数没什么意义,在UI界面上也看到,所以不需要配置" @ApiOperation:用在请求的方法上,说明方法的用途、作用 value="说明方法的用途、作用" notes="方法的备注说明" @ApiImplicitParams:用在请求的方法上,表示一组参数说明 @ApiImplicitParam:用在@ApiImplicitParams注解中,指定一个请求参数的各个方面 name:参数名 value:参数的汉字说明、解释 required:参数是否必须传 paramType:参数放在哪个地方 ·header-->请求参数的获取:@RequestHeader ·query-->请求参数的获取:@RequestParam ·path(用于restful接口)-->请求参数的获取:@PathVariable ·body(不常用) ·form(不常用) dataType:参数类型,默认String,其它值dataType="Integer" defaultValue:参数的默认值 @ApiResponses:用在请求的方法上,表示一组响应 @ApiResponse:用在@ApiResponses中,一般用于表达一个错误的响应信息 code:数字,例如400 message:信息,例如"请求参数没填好" response:抛出异常的类 @ApiModel:用于响应类上,表示一个返回响应数据的信息 (这种一般用在post创建的时候,使用@RequestBody这样的场景, 请求参数无法使用@ApiImplicitParam注解进行描述的时候) @ApiModelProperty:用在属性上,描述响应类的属性
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。
热门推荐
10 对患者生日祝福语简短
11 结婚祝福语简短装备
12 周岁祝福语学生文案简短
13 订婚领证祝福语简短精辟
14 导师获奖祝福语大全简短
15 新婚购房祝福语简短精辟
16 牛年祝福语简短的爱人
17 送芒果的祝福语简短
18 送给学长毕业祝福语简短