欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

swagger使用

程序员文章站 2022-07-02 21:06:19
...

添加maven依赖

	<dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger2</artifactId>
        <version>${springfox-swagger2.version}</version>
    </dependency>
    <dependency>
        <groupId>com.github.xiaoymin</groupId>
        <artifactId>swagger-bootstrap-ui</artifactId>
        <version>${swagger-bootstrap-ui.version}</version>
    </dependency>

添加swagger配置


import com.github.xiaoymin.swaggerbootstrapui.annotations.EnableSwaggerBootstrapUI;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
@EnableSwaggerBootstrapUI
/** 是否打开swagger **/
@ConditionalOnExpression("'${swagger.enable}' == 'true'")
public class Swagger2Config extends WebMvcConfigurerAdapter {

    @Bean
    public Docket createEventRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .groupName("开放平台-API")
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.wws.controller"))
                .paths(PathSelectors.any())
                .build();

    }


    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("开放平台项目")
                .description("开放平台项目API")
                .termsOfServiceUrl("http://localhost:10005/doc.html")
                .contact(new Contact("开发者", "http://localhost:10005/doc.html", "aaa@qq.com"))
                .version("1.0")
                .build();
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
    }

}

添加properties配置

swagger.enable=true
swagger.basic.enable=true
swagger.basic.username=admin
swagger.basic.password=admin

springboot启动类添加注解


@SpringBootApplication()
@EnableAutoConfiguration()
@EnableSwagger2
public class MainApplication extends SpringBootServletInitializer {
    public static void main(String[] args) {
        SpringApplication.run(MainApplication.class, args);
    }

}

使用注解

param入参注解

@Data
public class DemoParam implements Serializable {

    @ApiModelProperty(value = "姓名", example = "小明", required = true)
    private String name;
    @ApiModelProperty(value = "手机号", example = "1864567894")
    private String mobile;
    @ApiModelProperty(value = "id", example = "1")
    private Long id;
    
}

vo返回注解

@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
public class DemoVO implements Serializable {

    @ApiModelProperty(value = "手机号", example = "1864567894", required = true)
    private String mobile;
    @ApiModelProperty(value = "名字", example = "小明")
    private String name;
    @ApiModelProperty(value = "id", example = "10", required = true)
    private Long id;

}

controller注解


@Api(tags = "demo模块")
@Controller
@RequestMapping("/demo")
public class DemoController {

    @Autowired
    private IDemoService iDemoService;

    @ApiOperation(value = "数据插入", notes = "数据插入")
    @PostMapping("insert")
    @ResponseBody
    public RestResult insert(@RequestBody DemoParam demoParam) {
        return RestResult.wrapSuccessResponse(iDemoService.insert(demoParam));
    }
    
}

查看接口文档

登录http://localhost:10005/doc.html
swagger使用

在线调试

swagger使用