idea创建springboot项目_使用IDEA创建Springboot集成swagger超详细

什么是swagger?它有哪些注解?

Swagger介绍OpenAPI规范(OpenAPI Specification 简称OAS)是Linux基金会的一个项目,试图通过定义一种用来描述API格 式或API定义的语言,来规范RESTful服务开发过程,目前版本是V3.0,并且已经发布并开源在github上。(https://github.com/OAI/OpenAPI-Specification) Swagger是全球最大的OpenAPI规范(OAS)API开发工具框架,支持从设计和文档到测试和部署的整个API生命周 期的开发。(https://swagger.io/) Spring Boot 可以集成Swagger,生成Swagger接口,Spring Boot是Java领域的神器,它是Spring项目下快速构建 项目的框架。

常用Swagger注解在Java类中添加Swagger的注解即可生成Swagger接口,常用Swagger注解如下:@Api:修饰整个类,描述Controller的作用@ApiOperation:描述一个类的一个方法,或者说一个接口@ApiParam:单个参数描述@ApiModel:用对象来接收参数@ApiModelProperty:用对象接收参数时,描述对 象的一个字段@ApiResponse:HTTP响应其中1个描述@ApiResponses:HTTP响应整体描述@ApiIgnore:使用 该注解忽略这个API@ApiError :发生错误返回的信息@ApiImplicitParam:一个请求参数@ApiImplicitParams:多个请求参数@ApiImplicitParam属性:

b3adb7106fd4befc8f65bd104ea0a2ad.png

使用IDEA创建一个Springboot项目

1、首先选择file->New->Project到项目创建页面

34c1ecd9b37eb6b51df91c10666b5a82.png

2、我们选择Maven点击Next然后来到了下面这个页面

b1616edd3664e7d8984f5dff7e484bc9.png

GroupId命名规范一般以cn或者com开头,第二个公司的名称dandelioncloud,我的网站是蒲公英云域名是dandelioncloud.cn所以这里我就使用cn.dandelioncloud

ArtifactId可以填写项目名称,例如demo我这里就不写完整的demo了因为是和swagger集成所以这里只写api

3、修改项目名称以及项目存放地址

85329c25331189f4940eb9f0544ab175.png

projectname是项目的名称。这里我就用api不更改。

project location项目的存放地址。也就是说这个项目放在哪个路径下。我这里就放E盘的project下

然后直接选择完成,我这里选择new Windows 。新建一个窗口。

4、创建好的项目初始化完成后如下图

abb520a9aecf0e6384b95e919f5929b2.png

pom.xml的配置

1、Springboot的配置

adbb22cd2914b3752d1777dc639266a8.png

因为我们创建项目用的是Maven创建的所以现在只是Maven项目只有加上这段代码才是完整的Springboot项目

<parent>    <groupId>org.springframework.bootgroupId>    <artifactId>spring-boot-starter-parentartifactId>    <version>2.1.5.RELEASEversion>    <relativePath/>parent>
2、集成swagger需要的相关依赖

f624f05ab9f29b55ac7fe98a8d21e0c6.png

上图中完整的依赖

<dependencies>    <dependency>        <groupId>org.springframework.bootgroupId>        <artifactId>spring-boot-starter-webartifactId>    dependency>    <dependency>        <groupId>io.springfoxgroupId>        <artifactId>springfox-swagger2artifactId>        <version>2.6.1version>    dependency>    <dependency>        <groupId>io.springfoxgroupId>        <artifactId>springfox-swagger-uiartifactId>        <version>2.6.1version>    dependency>    <dependency>        <groupId>com.fasterxml.jackson.coregroupId>        <artifactId>jackson-coreartifactId>        <version>2.6.5version>    dependency>    <dependency>        <groupId>com.fasterxml.jackson.coregroupId>        <artifactId>jackson-databindartifactId>        <version>2.6.5version>    dependency>    <dependency>        <groupId>com.fasterxml.jackson.coregroupId>        <artifactId>jackson-annotationsartifactId>        <version>2.6.5version>    dependency>dependencies>

创建项目启动项

85554397667ecc336132a2f48011603a.png

创建APIApplication启动类

e507b23543ad0fc57ef6c1080256c56c.png

package cn.dandelioncloud.api;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublic class ApiApplication {    public static void main(String[] args) {        SpringApplication.run(ApiApplication.class,args);    }}

创建swagger配置类

e922aa26ab063fecd123e7f1fb459b38.png

配置类代码效果图

dca794a2e1e2a24deee6136add7cfac3.png

package cn.dandelioncloud.api.config;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.Configuration;import io.swagger.annotations.ApiOperation;import springfox.documentation.builders.ApiInfoBuilder;import springfox.documentation.builders.PathSelectors;import springfox.documentation.builders.RequestHandlerSelectors;import springfox.documentation.service.ApiInfo;import springfox.documentation.spi.DocumentationType;import springfox.documentation.spring.web.plugins.Docket;import springfox.documentation.swagger2.annotations.EnableSwagger2;/** * Swagger配置类 * @author Administrator */@Configuration@EnableSwagger2public class Swagger2Configuration {    @Bean    public Docket createRestApi(){        return new Docket(DocumentationType.SWAGGER_2)                .apiInfo(apiInfo())                .select()                .paths(PathSelectors.any())                .build();    }    private ApiInfo apiInfo() {        return new ApiInfoBuilder()                .title("测试swagger-蒲公英云")                .description("蒲公英云api文档")                // .termsOfServiceUrl("/")                .version("1.0")                .build();    }}

创建一个接口用来测试Swagger接口文档

253f5b90cf90efd2292b2a108272d3eb.png

ffe3e90be1be5ac83be6bdbd7ff7d789.png

package cn.dandelioncloud.api.controller;import io.swagger.annotations.Api;import io.swagger.annotations.ApiImplicitParam;import io.swagger.annotations.ApiImplicitParams;import io.swagger.annotations.ApiOperation;import org.springframework.http.ResponseEntity;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.PathVariable;@Api(value="蒲公英云页面管理接口",description = "蒲公英云页面管理接口,提供页面的增、删、改、查")public interface DandelionCloudController {    final String API_PRE = "/dandelioncloud/page";    @GetMapping(API_PRE+"/list/{page}/{size}")    @ApiOperation("分页查询页面列表")    @ApiImplicitParams({            @ApiImplicitParam(name="page",value = "页码",required=true,paramType="path",dataType="integer"),            @ApiImplicitParam(name="size",value = "每页记录数",required=true,paramType="path",dataType="integer")    })    public ResponseEntity<Object> findList(@PathVariable("page") int page, @PathVariable("size") int size) ;}

创建一个DTO

211633188f90d5c55d1fff6f94e5a2c8.png

bf490edbb66db727e3eedc46dfdb40fe.png

本来想使用lombok发现没有引入依赖那就加一个依赖吧。

2f525b19a86883567b7879b3199927cf.png

<dependency>    <groupId>org.projectlombokgroupId>    <artifactId>lombokartifactId>dependency>

DTO代碼

package cn.dandelioncloud.api.dto;import io.swagger.annotations.ApiModelProperty;import lombok.Data;import org.springframework.format.annotation.DateTimeFormat;import java.util.Date;@Datapublic class UserDTO {    @ApiModelProperty(value = "ID")    private Integer id;    @ApiModelProperty(value = "用户登录账号", required = true)    private String userNo;    @ApiModelProperty(value = "姓名", required = true)    private String userName;    @ApiModelProperty(value = "姓名拼音")    private String spellName;    @ApiModelProperty(value = "密码", required = true)    private String password;    @ApiModelProperty(value = "手机号", required = true)    private String userPhone;    @ApiModelProperty(value = "性别")    private Integer userGender;    @ApiModelProperty(value = "记录创建时间")    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")    private Date createTime;    @ApiModelProperty(value = "记录修改时间")    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")    private Date updateTime;}

这样也就算了大功告成了。业务逻辑这里不写,只写接口。

最后在启动类上加上swagger的注解就可以完成启动了

@EnableSwagger2

29f3f4b3ca3623f38a43520298fc9499.png

启动项目

3e453cddf794a4b4c4a4f8865048b535.png

启动成功。端口号8080

0f9eda5690a48363a32b744416ff0dfd.png

最后访问swagger地址

http://localhost:8080/swagger-ui.htmlhttp://localhost:端口号/swagger-ui.html

bb9472ab2333a5b389586f37fb6282d3.png

这里没有写实现类,实现类需要自己写,现在是调不通的。由于时间关系就不提供接口实现类的编写了。期待有机会下次续上。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值