springboot-swagger配置扫描接口及配置

swagger要导的包

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.5</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>springboot-swagger</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springboot-swagger</name>
    <description>springboot-swagger</description>
    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>3.0.0</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>3.0.0</version>
        </dependency>


<!--        新版(3.0)的直接加入启动器-->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-boot-starter</artifactId>
            <version>3.0.0</version>
        </dependency>


    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

swagger配置config

package com.example.config;



import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
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 java.util.ArrayList;

@Configuration
public class SwaggerConfig {
//    http://localhost:8080/swagger-ui/index.html新版路径
    @Bean//配置swagger的Docket bean实例
    public Docket docket(Environment environment){

        //设置当前要显示的Swagger环境
        Profiles of = Profiles.of("dev", "text");
        //获取当前项目环境
        boolean b = environment.acceptsProfiles(of);


        return new Docket(DocumentationType.SWAGGER_2)
                .enable(b)//是否启用swagger是false 默认true
                .apiInfo(apiInfo())
                .select()
                //RequestHandlerSelectors配置要扫描接口的方式
                //any()扫描全部
                //none()不扫描
                //withClassAnnotation()扫描类上的注解,参数是一个注解的反射对象
                //withMethodAnnotation()扫描方法上的注解
                //basePackage()指定要扫描那个包
                .apis(RequestHandlerSelectors.basePackage("com.example.controller"))
                //.paths(PathSelectors.ant("/hello/**"))//过滤什么路径
                .build()
                ;
    }


    //配置swagger的一些信息 作者信息
    public ApiInfo apiInfo(){
        Contact contact = new Contact("鲁家见","","482475181@qq.com");
        return new ApiInfo(
                "L的swagger的API文档",
                "学生管理项目文档",
                "1.0",
                "urn:tos",
                contact,
                "Apache 2.0",
                "http://www.apache.org/licenses/LICENSE-2.0",
                new ArrayList<>());
    }

}

controller普通接口

package com.example.controller;


import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {
    @GetMapping("/hello")
    public String Hello(){
        return "hello";
    }
    @PostMapping("/hello1")
    public String Hello1(String name ,String password){
        return "你输入的用户名和密码分别是"+name+":"+password;
    }
}

运行主类

package com.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

@EnableWebMvc//一定记住启动类加入@EnableWebMvc注解,不然会空指针异常,用Swagger
@SpringBootApplication
public class SpringbootSwaggerApplication {

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

}

yml设置项目环境

yml

spring:
  profiles:
    active: dev

yml-pev

server:
  port: 8081

yml-pvo

server:
  port: 8082

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: SpringBoot 配置Swagger 可以参考以下步骤:1. 在pom.xml中添加Swagger2依赖;2. 在SpringBoot启动类中添加@EnableSwagger2注解;3. 创建配置类Swagger2;4. 在配置类中配置Swagger2信息;5. 启动SpringBoot项目,访问Swagger2页面。 ### 回答2: Spring Boot是一个基于Java的快速开发框架,可以简化Java应用的搭建和开发过程。Swagger是一个RESTful API文档生成工具,它可以通过注解来生成API文档,并且提供了一个可交互的UI界面,方便开发人员查看和测试API接口。 要在Spring Boot项目中配置Swagger,需要进行以下步骤: 1. 导入Swagger依赖:在项目的pom.xml文件中添加Swagger的依赖项,通常是通过引入`springfox-swagger2`和`springfox-swagger-ui`这两个依赖来实现。 2. 创建Swagger配置类:在项目中创建一个Swagger的配置类,用于配置Swagger的相关信息和接口扫描规则。可以在配置类中使用`@EnableSwagger2`注解来启用Swagger。 3. 配置Swagger的基本信息:在配置类中使用`Docket`类的实例来配置Swagger的基本信息,比如Swagger接口文档的版本、标题、描述、联系人等。可以通过`apiInfo`方法来设置这些信息。 4. 配置接口扫描规则:在配置类中使用`select`方法来设置Swagger要扫描接口规则,比如指定要扫描的包路径,或者使用`RequestHandlerSelectors`类提供的方法来选择要包含的接口。 5. 配置Swagger UI界面:在配置类中使用`apis`方法来设置Swagger要显示的接口,可以选择显示所有接口,或者只显示带有特定注解的接口。可以使用`PathSelectors`类提供的方法来选择要显示的接口。 6. 启动项目并访问Swagger UI:完成上述配置后,启动Spring Boot项目,并访问Swagger UI界面,通常是通过在浏览器中访问`http://localhost:port/swagger-ui.html`来查看API文档。 以上就是在Spring Boot项目中配置Swagger的基本步骤。配置完毕后,开发人员就可以方便地查看和测试API接口,提高开发效率。 ### 回答3: Spring Boot是一个开源的Java开发框架,可以简化Spring应用程序的配置和开发过程。Swagger是一个用于构建、文档化和调试RESTful API的工具。在Spring Boot项目中配置Swagger可以使得我们更方便地管理和测试API接口。 要配置Swagger,我们需要在Spring Boot项目的pom.xml文件中添加Swagger的依赖。可以通过以下代码完成添加: ```xml <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> ``` 添加依赖后,我们需要创建一个配置类来配置Swagger。可以创建一个名为SwaggerConfig的类,并通过@Configuration注解将其标记为配置类。在SwaggerConfig类中,我们需要使用@EnableSwagger2注解启用Swagger,并创建一个Docket bean来配置Swagger的一些参数。 以下是一个示例的Swagger配置类代码: ```java @Configuration @EnableSwagger2 public class SwaggerConfig { @Bean public Docket api() { return new Docket(DocumentationType.SWAGGER_2) .select() .apis(RequestHandlerSelectors.basePackage("com.example.api")) .paths(PathSelectors.any()) .build(); } } ``` 在上述代码中,我们通过RequestHandlerSelectors.basePackage()方法指定了API接口所在的包路径,通过PathSelectors.any()方法指定了所有路径都被扫描。这样配置后,Swagger就可以扫描到我们想要展示的API接口。 最后,在启动类中添加@EnableSwagger2注解,启用Swagger。然后运行项目,访问Swagger UI界面(默认路径为:http://localhost:8080/swagger-ui.html),就可以看到API接口的文档、测试和调试信息了。 通过以上步骤,我们就成功地配置了Swagger在Spring Boot项目中。使用Swagger可以方便地对API进行管理和测试,提高了开发效率。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值