其他分享
首页 > 其他分享> > Swagger Demo

Swagger Demo

作者:互联网

最近项目中用了Swagger就研究了一下,顺便记录了下来,不过个人感觉这个东西虽然方便调试接口了,但对代码的污染太严重了

 

1.导入依赖(这里是使用maven的方式)

        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.9.2</version>
        </dependency>

 

2. 创建Swagger配置类(我这里是做了分组的)和业务枚举类

package com.example.demo.config;

import com.example.demo.constant.BusinessExplantEnum;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
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 配置类
 */
@Configuration
@EnableSwagger2
public class Swagger2Config {

    @Bean
    public Docket groupName1(){
        return getDocket(BusinessExplantEnum.GROUP_1.getGroupName(), BusinessExplantEnum.GROUP_1.getGroupContent(), "Title_1");
    }

    @Bean
    public Docket groupName2(){
        return getDocket(BusinessExplantEnum.GROUP_2.getGroupName(), BusinessExplantEnum.GROUP_2.getGroupContent(), "Title_2");
    }

    public Docket getDocket(String groupName, String path, String apiName){
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo(apiName))
                .groupName(groupName)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.example.demo.controller." + path))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo(String title){
        return new ApiInfoBuilder()
                .title(title)
                .version("1.0")
                .build();
    }
}

 

业务枚举类(不是必须的,具体看自己的业务需求)

package com.example.demo.constant;

/**
 * 业务枚举类
 */
public enum BusinessExplantEnum {

    GROUP_1("分组1", "demo1"),

    GROUP_2("分组2", "demo2");

    private String groupName;

    private String groupContent;

    BusinessExplantEnum(String groupName, String groupContent){
        this.groupName = groupName;
        this.groupContent = groupContent;
    }

    public String getGroupName() {
        return groupName;
    }

    public void setGroupName(String groupName) {
        this.groupName = groupName;
    }

    public String getGroupContent() {
        return groupContent;
    }

    public void setGroupContent(String groupContent) {
        this.groupContent = groupContent;
    }

}

3.创建参数实体类

package com.example.demo.model;

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

/**
 * 参数实体类
 */
@ApiModel(value = "DemoParamDTO", description = "参数实体类")
public class DemoParamDTO {

    @ApiModelProperty(value = "用户ID")
    private String id;

    @ApiModelProperty(value = "用户名称")
    private String name;

    public String getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

 

4.创建Controller类(这里就写一个,另一个内容一样)

package com.example.demo.controller.demo1;

import com.example.demo.common.ResponseResult;
import com.example.demo.model.DemoParamDTO;
import com.example.demo.model.ResponseVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Api(tags = "测试swagger功能1")
@RestController
@RequestMapping(value = "/controller1", produces = MediaType.APPLICATION_JSON_VALUE)
public class DemoController {

    @ApiOperation(value = "swagger方法")
    @PostMapping(value = "/testMethod")
    public ResponseResult<ResponseVO> testMethod(@ApiParam(required = true, name = "paramDTO", value = "接口入参")@RequestBody DemoParamDTO paramDTO){
        System.out.println("id:" + paramDTO.getId());
        System.out.println("name:" + paramDTO.getName());
        return ResponseResult.success(new ResponseVO());
    }

}

 

5.启动Application服务

package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplicationpublic class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

}

 

下图是项目目录结构

 

 

然后输入浏览器输入http://localhost:8080/swagger-ui.html就可以看到效果了

 

 

 

标签:springfox,String,Demo,example,groupName,import,Swagger,public
来源: https://www.cnblogs.com/jadening/p/14100649.html