• swagger配置


    Swagger使用说明

    构建项目

    构建springboot项目,添加web模块

    1、依赖关系(pom文件)

    <?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.4.0</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
        <groupId>com.jp</groupId>
        <artifactId>swagger</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <name>swagger</name>
        <description>Demo project for Spring Boot</description>
    
        <properties>
            <java.version>1.8</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-swagger-ui -->
            <dependency>
                <groupId>io.springfox</groupId>
                <artifactId>springfox-swagger-ui</artifactId>
                <version>2.9.2</version>
            </dependency>
            <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
            <dependency>
                <groupId>io.springfox</groupId>
                <artifactId>springfox-swagger2</artifactId>
                <version>2.9.2</version>
            </dependency>
    
    
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    
    </project>
    
    

    2、swagger的配置类

    package com.jp.swagger.config;
    
    
    import org.springframework.beans.factory.annotation.Autowired;
    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 springfox.documentation.swagger2.annotations.EnableSwagger2;
    
    import javax.servlet.http.HttpServletRequest;
    import java.util.ArrayList;
    
    /**
     * @author jp
     * @version 1.0
     * @date 2020/12/4 0004 15:25
     */
    
    @Configuration
    @EnableSwagger2
    public class swaggerConfig {
        // 配置docket以配置Swagger具体参数
        @Autowired
        Environment environment;
        @Bean
        public Docket docket() {
            // 设置要显示swagger的环境
            Profiles of = Profiles.of("dev", "test");
            // 判断当前是处于该环境,通过 enable() 接收此参数判断是否要显示
            boolean b = environment.acceptsProfiles(of);
            return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo())
                    .ignoredParameterTypes(HttpServletRequest.class)
                    .enable(b) // 配置是否启用Swagger,如果是false,在浏览器将无法访问
                    // 通过.select()方法,去配置扫描接口
                    .select()
                    // RequestHandlerSelectors配置如何扫描接口
                    .apis(RequestHandlerSelectors.basePackage("com.jp.swagger.controller")).build();
        }
    
    
        private ApiInfo apiInfo() {
            Contact contact = new Contact("联系人名字", "http://xxx.xxx.com/联系人访问链接", "联系人邮箱");
            // public ApiInfo(String title, String description, String version, String termsOfServiceUrl, Contact contact, String ", String licenseUrl, Collection<VendorExtension> vendorExtensions) {
            return new ApiInfo("Swagger学习", // 标题
                    "学习演示如何配置Swagger", // 描述
                    "v1.0", // 版本
                    "http://terms.service.url/组织链接", // 组织链接
                    contact, // 联系人信息
                    "Apach 2.0 许可", // 许可
                    "许可链接", // 许可连接
                    new ArrayList<>()); // 扩展
        }
    
    }
    
    

    3、实体类(user)

    package com.jp.swagger.model;
    
    import io.swagger.annotations.ApiModel;
    import io.swagger.annotations.ApiModelProperty;
    
    /**
     * @author jp
     * @version 1.0
     * @date 2020/12/4 0004 16:33
     */
    @ApiModel("用户实体")
    public class User {
        @ApiModelProperty("用户名")
        private String username;
        @ApiModelProperty("密码")
        private String password;
    
        public String getUsername() {
            return username;
        }
    
        public void setUsername(String username) {
            this.username = username;
        }
    
        public String getPassword() {
            return password;
        }
    
        public void setPassword(String password) {
            this.password = password;
        }
    }
    
    

    4、controller类

    package com.jp.swagger.controller;
    
    import com.jp.swagger.model.User;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    /**
     * @author jp
     * @version 1.0
     * @date 2020/12/4 0004 16:40
     */
    @RestController
    public class UserController {
    
        @GetMapping("/user")
        public User getUser(User user){
            user.setUsername("小白");
            user.setPassword("123456");
            return user;
        }
    }
    
    
  • 相关阅读:
    objectivec随机数
    UITableView中多个UITextField时UITableView的滚动和键盘的隐藏
    objectivec用图片设置backgroundcolor
    SQL代码添加字段
    如何让第三方库暂时不用arc
    快速统计表中的记录总数
    商务报盘英语
    对话:职业健康和安全
    你适合买房还是租房?
    怎样与同事和谐相处
  • 原文地址:https://www.cnblogs.com/kutoutuo/p/14181062.html
Copyright © 2020-2023  润新知