Swagger+springboot

Swagger+springboot,第1张

SpringBoot集成Swagger 一:项目搭建,配置信息 1.搭建项目

新建一个springboot的项目,在pom.xml文件,导入相关依赖

 <!--        swagger接口测试-->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-boot-starter</artifactId>
            <version>3.0.0</version>
        </dependency>
2.编写一个hello工程 3.配置一个Swagger=>config工程

@SpringBootApplication
// MapperScan注解指定当前项目中的Mapper接口路径,在项目启动时就会自动加载所有的接口
@MapperScan("com.example.store.mapper")
@EnableOpenApi
public class StoreApplication {

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

}
4.访问http://localhost:8080/swagger-ui/

如果你的swgger-ui是3.0的版本需要加,2点多的不需要添加

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-boot-starter</artifactId>
    <version>3.0.0</version>
</dependency>

swagger3.0迭代算是重大更新。
一个是添加了starter,依赖用springfox-boot-starter这一个就可以了。具体的可以alt+insert自己搜这个。
ui界面移动到了/swagger-ui/index.html,也就是可以只打/swagger-ui/。接口看起来也正常了点
configure的启动标签那边变成了@EnableOpenApi。

5.配置swagger,写出你自己的swagger-ui.html页面

Swagger的bean实例Docket

package com.example.swagger.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

import java.util.ArrayList;


@Configuration
@EnableOpenApi //开启swagger
public class SwaggerConfig {


    //配置了Swagger的Docket的bean实例
    @Bean
    public Docket docket(){
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo());
    }
    //配置Swagger信息=apiInfo
    private ApiInfo apiInfo(){
        Contact contact = new Contact(
                "王冲",
                "https://blog.csdn.net/qq_51269815?spm=1000.2115.3001.5343",
                "[email protected]");
        return new ApiInfo(
                "王冲的csdn博客",
                "事情已逐浮云散",
                "V1.0",
                "https://blog.csdn.net/qq_51269815?spm=1000.2115.3001.5343",
                contact,
                "Apache 2.0",
                "http://www.apache.org/licenses/LICENSE-2.0",
                new ArrayList<>());
    }

}

二:Swagger高级教程 1.配置扫描接口及开关

指定扫描某些接口

//配置了Swagger的Docket的bean实例
    @Bean
    public Docket docket(){
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                // RequestHandlerSelectors,配置扫描接口的方式
                //basePackage指定要扫描的包
                // any 扫描全部
                // none 都不扫描
                // withClassAnnotation :扫描类上的注解
                // withMethodAnnotation: 扫描方法上的注解
//                .apis(RequestHandlerSelectors.withMethodAnnotation(GetMapping.class))
                .apis(RequestHandlerSelectors.basePackage("com.example.swagger.controller"))
                // paths() 过滤什么路劲
                .paths(PathSelectors.ant("/chong/**"))
                .build();
    }
2.根据不同的环境决定是否开启swagger

application.yaml配置



server:
  port: 8080
//使用dev的生产环境
spring:
  profiles:
    active: dev

application-dev.yaml

server:
  port: 8086

 @Bean
    public Docket docket(Environment enviroment){

        //设置要使用的swagger环境
        Profiles profiles = Profiles.of("dev","test");

        //获取项目的环境 enviroment.acceptsProfiles(profiles)判断是否处在自己设定的环境当中
        boolean flag = enviroment.acceptsProfiles(profiles);
        System.out.println(flag);


        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                // enable是否启动swagger,如果为False,则swagger不能在游览器中访问
                .enable(flag)
                .select()
                // RequestHandlerSelectors,配置扫描接口的方式
                //basePackage指定要扫描的包
                // any 扫描全部
                // none 都不扫描
                // withClassAnnotation :扫描类上的注解
                // withMethodAnnotation: 扫描方法上的注解
//                .apis(RequestHandlerSelectors.withMethodAnnotation(GetMapping.class))
                .apis(RequestHandlerSelectors.basePackage("com.example.swagger.controller"))
                // paths() 过滤什么路劲
                .paths(PathSelectors.ant("/chong/**"))
                .build();
    }
三:使用swagger进行测试和加注释 1.给实体类加注解
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

//给生成文档加注释
@ApiModel("用户实体类")
public class User {
    @ApiModelProperty("用户名")
    public String username;
    @ApiModelProperty("密码")
    public String password;
}

只有在controller层使用才能在swagger-ui/index.html页面见到

2.给接口增加注释,使用swagger进行接口测试

controller增加注解

package com.example.swagger.controller;

import com.example.swagger.pojo.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Api(tags = "你好,明天!")
public class HelloController {
    @GetMapping(value = "/hello")
    public String hello(){
        return "hello";
    }

    @PostMapping(value = "/user")
    public User user(){
        return  new User();
    }

    @ApiOperation("hello控制类")
    @GetMapping(value = "/hello2")
    public String hello2(@ApiParam("用户名") String username){
        return "hello"+username;
    }
}

效果是

使用swagger进行接口测试

如果你在实体类中增加了@Data注解,请注意user不能为空

  @ApiOperation("Post测试类")
    @PostMapping(value = "/postt")
    public User postt(@ApiParam("用户名") User user){
        return user;
    }

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/718575.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-25
下一篇 2022-04-25

发表评论

登录后才能评论

评论列表(0条)

保存