博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SpringBoot使用Swagger搭建Rest服务
阅读量:6690 次
发布时间:2019-06-25

本文共 2198 字,大约阅读时间需要 7 分钟。

hot3.png

SpringBoot 搭建 Rest 服务器,Swagger 提供 API 文档支持。

Maven 添加 Swagger 支持
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.2.2</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.2.2</version>
        </dependency>
Swagger 配置:
@Configuration
@EnableConfigurationProperties(SwaggerProperties.class)
@EnableSwagger2
public class SwaggerConfig {
    @Autowired
    private SwaggerProperties swaggerProperties;
   
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage(swaggerProperties.getBasePackage()))
                .paths(PathSelectors.any())
                .build();
    }
    private ApiInfo apiInfo() {
        return new ApiInfo(swaggerProperties.getTitle(),
                swaggerProperties.getDescription(),
                swaggerProperties.getVersion(),
                swaggerProperties.getTermsOfServiceUrl(),
                swaggerProperties.getContact(),
                swaggerProperties.getLicense(),
                swaggerProperties.getLicenseUrl());
    }
}
创建 POJO:
@ApiModel("授权信息")
public class Auth {
    @ApiModelProperty("用户ID")
    private Long id;
    @ApiModelProperty("凭据")
    private String ticket;
    public Auth() {
    }
    public Auth(Long id, String ticket) {
        this.id = id;
        this.ticket = ticket;
    }
    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    public String getTicket() {
        return ticket;
    }
    public void setTicket(String ticket) {
        this.ticket = ticket;
    }
}
创建 Rest:
@Api(tags = "AUTH_AUTH", description = "授权-授权管理")
@RestController
@RequestMapping("/auths")
public class AuthController {
    @Autowired
    private AuthService authService;
    @ApiOperation("获取授权信息")
    @RequestMapping(value = "/{userId}", method = RequestMethod.GET)
    public Result<Auth> get(
            @ApiParam(value = "用户ID", required = true)
            @PathVariable Long userId)  {
        return new Result<Auth>(authService.get(userId));
    }
    @ApiOperation("删除授权信息")
    @RequestMapping(value = "/{userId}", method = RequestMethod.DELETE)
    public Result<Boolean> delete(
            @ApiParam(value = "用户ID", required = true)
            @PathVariable Long userId) {
        authService.delete(userId);
        return new Result<Boolean>(true);
    }
}
Swagger API 文档如图:
github代码:

转载于:https://my.oschina.net/u/2950586/blog/1551587

你可能感兴趣的文章
电脑必用驱动大全
查看>>
用Python实现随机验证码
查看>>
Linux网络IP配置
查看>>
用vbscript也可以刷网站
查看>>
运维自动化之 Cobbler 系统安装使用详解
查看>>
web开发敏捷之道2nd-rails进行web开发-笔记(全)
查看>>
使用Facility:EnterpriseLibrary整合进Castle
查看>>
CITRIX XenDesktop与无盘工作站
查看>>
SQL Server 2008和2008 R2评估版过期的解决办法 .
查看>>
linux shell下转换unix时间
查看>>
Exchange Server邮件存储系统(原理篇)
查看>>
【协议学习】PPPoE学习文档
查看>>
【实战】烂泥:关于佳能IR2320N网络打印机的安装域使用
查看>>
Angular2 小贴士-多级注入器
查看>>
数据恢复全解析
查看>>
处理windows 2008x64平台exchange 2010 sp1打完系统补丁后,控制台无法打开
查看>>
改良版本mysqldump来备份MYSQL数据库
查看>>
域账号加到本机管理员组和本机Power Users组
查看>>
Java多线程初学者指南(6):慎重使用volatile关键字
查看>>
dd for windows
查看>>