简单搭建Swagger与Spring boot项目

1、配置Spring boot的项目
http://start.spring.io/
2 、导入IDEA中
3、导入有关于Swagger的mavenjar包

<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>       

4、配置Swagger的config

import java.util.ArrayList;
import java.util.List;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import springfox.documentation.service.Parameter;

@Configuration
@ComponentScan(basePackages = {"com.citydo.springDemo"})//配置controller路径
@EnableSwagger2
public class SwaggerConfig {
     @Bean
     public Docket createRestApi() {
     return new Docket(DocumentationType.SWAGGER_2)
            .apiInfo(apiInfo())
            .select()
            .apis(RequestHandlerSelectors.basePackage("com.citydo"))//扫描com路径下的api文档
            .paths(PathSelectors.any())//路径判断,这里是任何路径
           .build();
      }
      private ApiInfo apiInfo() {
      return  new  ApiInfo("测试接口-JAVA版",//大标题
            "测试",//小标题
            "1.0",//版本
            "1",
            "点点",//作者
            "官方网址",//链接显示文字
            "http://www.citydo.com"//网站链接
             );
     }
}

5、在实体类上加入注解

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

@ApiModel(value="testUser实体",reference ="参考" )//必须要
public class TestUser {
    @ApiModelProperty(value="姓名",dataType = "String",required = true,example = "熊")//必须要
    private String name;
    @ApiModelProperty(value="主键",dataType = "String",required = true,example = "id")
    private String id;
    @ApiModelProperty(value="年龄",dataType = "int",required = true,example = "12")
    private Integer age;
    @ApiModelProperty(value="测试",dataType = "String",example = "测试")
    private String test;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String getTest() {
        return test;
    }

    public void setTest(String test) {
        this.test = test;
    }
}

6、在方法上加入注解

import com.citydo.springDemo.entity.TestUser;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("test")
@Api(value = "测试", description = "测试Controller", tags = "test")//必须要
public class TestController {
    @ApiOperation(value="获取用户列表", notes="获取用户列表", httpMethod = "POST", response = TestUser.class, produces = "application/json")//必须要
    @RequestMapping("getUserList")
    public List<TestUser> getUserList(@RequestBody @ApiParam TestUser tu) {
        List<TestUser> r = new ArrayList<TestUser>();
        return r;
    }
}

7、在static的中加入html与css、js文件
下载地址: https://github.com/863473007/swagger-ui
将dist文件内容拷贝到static文件夹内
这里写图片描述
并将index.html中一段代码修改为url: "http://127.0.0.1:8082/v2/api-docs/",
这里写图片描述
然后运行输入地址就可以看看Swagger的界面与接口。

这里写图片描述
8、完美运行成功。

猜你喜欢

转载自blog.csdn.net/qq_32447301/article/details/81137817