项目中遇到的错误

news2024/9/25 19:48:01

项目中遇到的错误

  • swagger2 和 swagger3
  • swagger 文档的注解
  • springboot 版本问题
  • SQL 关键字异常
  • Apifox 的使用
  • 集中版本管理

swagger2 和 swagger3

swagger2swagger3 需要导入的依赖

        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-boot-starter</artifactId>
            <version>3.0.0</version>
        </dependency>
        <dependency>
            <groupId>com.github.xiaoymin</groupId>
            <artifactId>swagger-bootstrap-ui</artifactId>
            <version>1.9.6</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
        <!-- commons-lang3-->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.10</version>
        </dependency>

swagger2访问地址 http://localhost/doc.html

swagger2 的配置类

@EnableSwagger2
@Configuration
public class SwaggerConfig {
    /**
     * 设置要暴露接口文档的配置环境
     * @param env 环境
     * @return Docket
     */
    @Bean
    public Docket initDocket(Environment env) {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo()) .enable(true) .select()
                .apis(RequestHandlerSelectors
                        .withMethodAnnotation(ApiOperation.class))
                .paths(PathSelectors.any()) .build(); }

    private ApiInfo apiInfo() { return new ApiInfoBuilder()
            .title("图书管理系统") .description("管理书籍和读者信息,管理书籍,借阅和归还书籍")
            .contact(new Contact("coffeemao", null, "123456@qq.com "))
            .version("1.0") .build(); }
}

swagger3访问地址 http://localhost:8080/swagger-ui/index.html

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

    public Docket docket(){
        Docket docket = new Docket(DocumentationType.OAS_30);
        docket.apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.mao.book.controller"))
                .paths(PathSelectors.any()).build();
        return docket;
    }

    private ApiInfo apiInfo(){
        Contact contact = new Contact("作者 coffeemao", "作者www.baidu.com","123456@qq.com");
        ApiInfo apiInfo = new ApiInfo(
                "Swagger3接口文档",
                "SpringBoot 整合 Swagger3 生成接口文档!",
                "1.0.0",
                "termsOfServiceUrl",
                contact,
                "license",
                "licenseUrl",
                new ArrayList());
        return apiInfo;
    }
}

或者使用以下更为灵活的自定义配置类

SwaggerProperties 的属性类

@Component
@ConfigurationProperties("swagger")
@Data
@AllArgsConstructor
@NoArgsConstructor
public class SwaggerProperties {
    /**
     * 是否开启swagger,生产环境一般关闭,所以这里定义一个变量
     */
    private Boolean enable;

    /**
     * 项目应用名
     */
    private String applicationName;

    /**
     * 项目版本信息
     */
    private String applicationVersion;

    /**
     * 项目描述信息
     */
    private String applicationDescription;

    /**
     * 接口调试地址
     */
    private String tryHost;
}

自定义的配置类

@EnableOpenApi
@Configuration
public class SwaggerConfiguration implements WebMvcConfigurer {
    private final SwaggerProperties swaggerProperties;

    public SwaggerConfiguration(SwaggerProperties swaggerProperties) {
        this.swaggerProperties = swaggerProperties;
    }

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.OAS_30).pathMapping("/")

                // 定义是否开启swagger,false为关闭,可以通过变量控制
                .enable(swaggerProperties.getEnable())

                // 将api的元信息设置为包含在json ResourceListing响应中。
                .apiInfo(apiInfo())

                // 接口调试地址
                .host(swaggerProperties.getTryHost())

                // 选择哪些接口作为swagger的doc发布
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any())
                .build()

                // 支持的通讯协议集合
                .protocols(newHashSet("https", "http"))

                // 授权信息设置,必要的header token等认证信息
                .securitySchemes(securitySchemes())

                // 授权信息全局应用
                .securityContexts(securityContexts());
    }

    /**
     * API 页面上半部分展示信息
     */
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder().title(swaggerProperties.getApplicationName() + " Api Doc")
                .description(swaggerProperties.getApplicationDescription())
                .contact(new Contact("coffeemao", null, "123456@gmail.com"))
                .version("Application Version: " + swaggerProperties.getApplicationVersion() + ", Spring Boot Version: " + SpringBootVersion.getVersion())
                .build();
    }

    /**
     * 设置授权信息
     */
    private List<SecurityScheme> securitySchemes() {
        ApiKey apiKey = new ApiKey("BASE_TOKEN", "token", In.HEADER.toValue());
        return Collections.singletonList(apiKey);
    }

    /**
     * 授权信息全局应用
     */
    private List<SecurityContext> securityContexts() {
        return Collections.singletonList(
                SecurityContext.builder()
                        .securityReferences(Collections.singletonList(new SecurityReference("BASE_TOKEN", new AuthorizationScope[]{new AuthorizationScope("global", "")})))
                        .build()
        );
    }

    @SafeVarargs
    private final <T> Set<T> newHashSet(T... ts) {
        if (ts.length > 0) {
            return new LinkedHashSet<>(Arrays.asList(ts));
        }
        return null;
    }

    /**
     * 通用拦截器排除swagger设置,所有拦截器都会自动加swagger相关的资源排除信息
     */
    @SuppressWarnings("unchecked")
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        try {
            Field registrationsField = FieldUtils.getField(InterceptorRegistry.class, "registrations", true);
            List<InterceptorRegistration> registrations = (List<InterceptorRegistration>) ReflectionUtils.getField(registrationsField, registry);
            if (registrations != null) {
                for (InterceptorRegistration interceptorRegistration : registrations) {
                    interceptorRegistration
                            .excludePathPatterns("/swagger**/**")
                            .excludePathPatterns("/webjars/**")
                            .excludePathPatterns("/v3/**")
                            .excludePathPatterns("/doc.html");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

applicaton.xmlswagger3 进行了开启,应用的应用名字,应用程序的版本信息,描述信息,访问的地址等

spring:
  application:
    name: 图书管理系统
swagger:
  enable: true
  application-name: ${spring.application.name}
  application-version: 1.0
  application-description: swagger3.0 的图书管理文档
  try-host: http://localhost:${server.port}

swagger 文档的注解

  • @Api() 用在请求的类上,表示对类的说明,在controller层的类上的注解

参数
tags:说明该类的作用,参数是个数组,可以填多个。
description = “用户基本信息操作”

  • @ApiOperation() 用于方法,表示一个http请求访问该方法的操作,在controller层类的方法上的注解

参数
value=“方法的用途和作用”
notes=“方法的注意事项和备注”

  • @ApiModel() 在实体类模块下使用,说明该实体类的作用

参数
description=“描述实体的作用”

  • @ApiModelProperty() 在实体类的属性上使用,用于描述实体类的属性

参数
value=“用户名” 描述参数的意义
name=“name” 参数的变量名
required=true 参数是否必选

  • @ApiImplicitParams() controller 层的方法上多个参数的说明

  • @ApiImplicitParam() controller 层的方法上多个参数中每一个参数的说明

参数
name=“参数ming”
value=“参数说明”
dataType=“数据类型”
paramType=“query” 表示参数放在哪里
defaultValue=“参数的默认值”
required=“true” 表示参数是否必须传

  • @ApiParam() 用于方法,参数,字段说明 表示对参数的要求和说明

参数
name=“参数名称”
value=“参数的简要说明”
defaultValue=“参数默认值”
required=“true” 表示属性是否必填,默认为false

  • @ApiResponses() 用于请求的方法上,根据响应码表示不同响应

一个@ApiResponses包含多个@ApiResponse

  • @ApiResponse() 用在请求的方法上,表示不同的响应

参数
code=“404” 表示响应码(int型),可自定义
message=“状态码对应的响应信息”

  • @ApiIgnore() 用于类或者方法上,不被显示在页面上

  • @Profile({"dev", "test"}) 用于配置类上,表示只对开发和测试环境有用

springboot 版本问题

出现问题是由于 springboot 的版本过高和 swagger 文档的不兼容

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2022-12-17 11:00:46.164 ERROR 8136 --- [  restartedMain] o.s.boot.SpringApplication               : Application run failed

org.springframework.context.ApplicationContextException: Failed to start bean 'documentationPluginsBootstrapper'; nested exception is java.lang.NullPointerException

在这里插入图片描述

	<!--标志是springboot的项目-->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.2</version>
        <relativePath/>
    </parent>

降低版本

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.2</version>
        <relativePath/>
    </parent>

SQL 关键字异常

错误如下

2022-12-17 10:35:56.067 ERROR 10652 --- [p-nio-80-exec-5] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.jdbc.BadSqlGrammarException: 
### Error updating database.  Cause: java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'desc='12121'
        where
            id=1 and deleted=0' at line 7
### The error may exist in file [E:\Code\backproject\book\target\classes\mybatis\mapper\BookMapper.xml]
### The error may involve defaultParameterMap
### The error occurred while setting parameters
### SQL: update book         set             `name`=?,             image=?,             number=?,             price=?,             desc=?         where             id=? and deleted=0
### Cause: java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'desc='12121'
        where
            id=1 and deleted=0' at line 7
; bad SQL grammar []; nested exception is java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'desc='12121'
        where
            id=1 and deleted=0' at line 7] with root cause

java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'desc='12121'
        where
            id=1 and deleted=0' at line 7

在这里插入图片描述

原因数据库的字段是MySQL的关键字,这时候就需要加上(倒引号``)

<!--    SQL中的字段和关键字同名要使用 `desc` -->
    <update id="update">
        update book
        set
            `name`=#{name},
            image=#{image},
            number=#{number},
            price=#{price},
            `desc`=#{desc}
        where
            id=#{id} and deleted=0
    </update>

SQL 的关键字

https://www.cnblogs.com/langtianya/p/4968132.html

Apifox 的使用

下载安装Apifox,https://www.apifox.cn/

使用导入数据

Apifox帮助文档

在这里插入图片描述

连接填写的是数据文档的Url

swagger2文档注释的文档数据地址

http://localhost/v2/api-docs

swagger3文档注释的文档数据地址

http://localhost/v3/api-docs

集中版本管理

采用集中的版本信息管理,方便版本信息的统一更新

    <!-- 集中控制版本 编码 -->
    <properties>
        <junit.version>4.12</junit.version>
        <lombok.version>1.18.12</lombok.version>
        <log4j.version>1.2.17</log4j.version>
        <druid.version>1.2.5</druid.version>
        <jdbc.version>8.0.25</jdbc.version>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

版本控制所对应的依赖项目

		<!-- junit 单元测试 -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>
        <!-- lombok 脚手架工具 -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>${lombok.version}</version>
        </dependency>
        <!-- log4j日志信息 -->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>${log4j.version}</version>
        </dependency>
        <!-- alibaba的druid数据源 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>${druid.version}</version>
        </dependency>
        <!-- 数据库连接驱动器 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>${jdbc.version}</version>
        </dependency>

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/97493.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

LabVIEW FPGA中可重入和非可重入子VI的区别

LabVIEW FPGA中可重入和非可重入子VI的区别 LabVIEW FPGAVI默认是可重入的。如果多次调用重入VI&#xff0c;则每个实例会占用FPGA器件的单独硬件资源。如果使用非重入VI&#xff0c;无论是并行多次调用还是仅调用一次&#xff0c;都只会创建一个硬件实例并将其用于该VI。 ​…

最常用的 9 个JavaScript 函数与示例

输出内容才能更好的理解输入的知识 前言&#x1f380; 如果你想深入图形、可视化等领域&#xff0c;那么肯定离不开 canvas、webgl、three.js 等一系列技术。在这众多技术中&#xff0c;选择canvas2d为基础来入门是一个不错的选择。 canvas在动画、可视化、图片处理等领域有着…

物联网通信技术原理 第5章

目录 5.1 移动通信的基本概念及发展历史 5.1.1 移动通信的基本概念 5.1.2 移动通信的发展历史&#xff08;理解&#xff09; 1.第一代移动通信系统(1G) 2.第二代移动通信系统(2G) 3.第三代移动通信系统(3G) 5.1.3 移动通信的发展趋势与展望 5.2 无线传播与移动信道 5.2…

哈希的应用:布隆过滤器(C++实现)

文章目录1. 布隆过滤器1.1 背景1.2 概念1.3 控制误判率2. 实现布隆过滤器2.1 布隆过滤器类2.2 Set2.3 Test2.4 删除3. 优点4. 缺陷4. 缺陷1. 布隆过滤器 1.1 背景 位图&#xff08;bitmap算法&#xff09;告诉我们&#xff0c;想判断一个元素是否存在于某个集合中&#xff0c…

c语言指针 字符 字符串

1、sizeof 某个类型或者某个变量在内存中占用字节数。 例如&#xff1a;sizeof(int) ; sizeof(i)&#xff1b;都可以使用 2、运算符& 获取变量的地址。 int i; scanf("%d",&i); 输入变量时&#xff0c;必须使用&运算符。 &操作符只能应…

机器学习100天(二):002 数据预处理之导入数据集

机器学习 100 天,今天讲的是:数据预处理之导入数据集。 首先,我们打开 spyder。新建一个 load_data.py 脚本。 第一步,导入标准库 机器学习常用的标准库有 3 个: 第一个:numpy,用于数据处理。 第二个:matplotlib.pyplot,用于画图。 第三个:pandas,用于数值分析…

python 爬虫

前言 一、什么是爬虫 爬虫&#xff1a;&#xff08;又称为网页蜘蛛&#xff0c;网络机器人&#xff0c;在 FOAF 社区中间&#xff0c;更经常地称为网页追逐者&#xff09;&#xff1b;它是一种按照一定的规则&#xff0c;自动地从互联网上抓取对于我们有价值的网络信息的程序…

最强大的布局方案——网格Grid布局万字详解

Grid 布局又称网格布局&#xff0c;是W3C提出的一个二维布局系统&#xff0c;它与 Flex 布局有一定的相似性&#xff0c;都可以指定容器内部多个项目的位置。但它们也存在重大区别。Flex 布局是轴线布局&#xff0c;只能指定"项目"针对轴线的位置&#xff0c;可以看作…

jsp+ssm计算机毕业设计大学城二手书交易网站【附源码】

项目运行 环境配置&#xff1a; Jdk1.8 Tomcat7.0 Mysql HBuilderX&#xff08;Webstorm也行&#xff09; Eclispe&#xff08;IntelliJ IDEA,Eclispe,MyEclispe,Sts都支持&#xff09;。 项目技术&#xff1a; JSPSSM mybatis Maven等等组成&#xff0c;B/S模式 Mave…

绝对神器,今天教你如何识别图片上竖排的日语文字

在文字翻译或者其他的工作中我们经常遇到竖排的日语&#xff0c;有时候我们用普通的日语识别的软件根本无法完成 这个时候我们就需要一款可以识别竖排的日语工具&#xff0c;横排的我们很容易就能找到&#xff0c;但是竖排的就无能为力了 今天我们讲下如何识别竖排日语识别&a…

ZERO-SHOT:多聚焦融合

ZERO-SHOT MULTI-FOCUS IMAGE FUSION &#xff08;零镜头多焦点图像融合&#xff09; 多聚焦图像融合 (Multi-focus image fusion (MFIF)) 是消除成像过程中产生的离焦模糊的有效方法。The difficulties in focus level estimation and the lack of real training set for su…

计算机毕业设计springboot+vue文体用品商城网站

项目介绍 在当今社会的高速发展过程中,产生的劳动力越来越大,提高人们的生活水平和质量,尤其计算机科技的进步,数据和信息以人兴化为本的目的,给人们提供优质的服务,其中网上购买商品尤其突出,使我们的购物方式发生巨大的改变。而线上购物,不仅需要在硬件上为人们提供服务网上购…

ASPICE详细介绍-3.ASPICE有多少能力等级?

目录ASPICE有多少能力等级&#xff1f;9 个过程属性过程属性评定过程能力等级模型ASPICE有多少能力等级&#xff1f; ASPICE能力等级从0级到5级共分为6个层次&#xff0c;必须满足前一级别才可晋级下一个级别的评估。 【0级】Incomplete&#xff0c;未完成。 The process is…

【YOLOv7-环境搭建】PyTorch安装后输出版本显示No module named ‘Torch’的解决方法

可能一&#xff1a;PyCharm环境导入错误 配置的解释器&#xff0c;必须为所创建的虚拟环境下的python.exe文件&#xff0c;别的路径下的python.exe文件不好使&#xff01;&#xff01; 解决方法&#xff1a;根据【YOLOv7-环境搭建③】PyCharm安装和环境、解释器配置文中配置解…

微信小程序自定义头部导航nav

1.封装自定义nav导航组件 // app.js App({globalData: {systeminfo: false, //系统信息headerBtnPosi: false //头部菜单高度} })// components/nav/nav.js const app getApp(); Component({properties: {vTitle: { // 标题type: String,value: ""},isSearch: {…

大厂频频裁员,0基础转行做IT是不是已经晚了

现在转行做程序员是不是已经晚了 转行不会晚&#xff0c;晚的是你数不清的犹豫 对于二十来岁刚毕业或者毕业没几年的人来说&#xff0c;经历过社会的“摧残”&#xff0c;面对着一眼能够望到头的工作&#xff0c;拿着也不太高的工资&#xff0c;总是会去寻求一些改变与其每天…

Ajax请求原理与数据抓取

有些时候&#xff0c;我们直接通过网络请求库请求网页地址时&#xff0c;得到的响应结果可能跟浏览器中右键查看网页源码所看到的内容不一样。例如&#xff0c;在抓取&#xff1a;https://www.feeair.com/threeCode.html &#xff08;飞啊网&#xff09;这个网页时&#xff0c;…

公司固定资产管理系统

开发工具(eclipse/idea/vscode等)&#xff1a; 数据库(sqlite/mysql/sqlserver等)&#xff1a; 功能模块(请用文字描述&#xff0c;至少200字)&#xff1a; 模块划分&#xff1a;公告类型、公告信息、员工信息、仓库信息、资产类型、资产信息、供应商信 息、采购信息、盗产调拨…

(Matlab)基于蝙蝠算法实现电力系统经济调度

目录 摘要&#xff1a; 1.蝙蝠优化算法的基本原理&#xff1a; 2.蝙蝠优化算法的流程&#xff1a; 3.仿真实验分析&#xff1a; 摘要&#xff1a; 基于Matalb平台&#xff0c;构建基于蝙蝠活动行为的蝙蝠优化算法&#xff0c;对一个含有6个火电机组的电力系统进行优化调度…

毕业设计 ESP32在线墨水屏桌面摆件 -物联网 单片机 嵌入式

0 前言 &#x1f525; 这两年开始毕业设计和毕业答辩的要求和难度不断提升&#xff0c;传统的毕设题目缺少创新和亮点&#xff0c;往往达不到毕业答辩的要求&#xff0c;这两年不断有学弟学妹告诉学长自己做的项目系统达不到老师的要求。 为了大家能够顺利以及最少的精力通过…