JAVA 项目中遇到过的异常、错误等记录与解决方法

1、ASM ClassReader failed to parse class file - probably due to a new Java class file version that isn’t supported yet: file......

  代码环境:

    Spring MVC:3.2.5.RELEASE

    JAVA:jdk 1.8

    MAVEN

  问题描述:多人协同开发,之前一直正常运行,在更新一个同事写的代码后,因为他的代码用到了jdk1.8的新特性,将原编辑版本1.7改为1.8后,编译没有问题,运行启动时报该错误。

  问题原因:Sring MVC 和jdk版本冲突

  解决方法:jdk改为1.7,springmvc升级到4以上

2、MyBatis:Parameter Maps collection does not contain value for 的问题解决

  xml配置文件中,parameterMap改为parameterType,前者已经废弃。

  参考:MyBatis:Parameter Maps collection does not contain value for 的问题解决

3、因热加载导致自定义业务异常被捕获为UndeclaredThrowableException:null 异常,重启即可

  问题描述:service层抛出一个BizException,被controller层捕获后变成了UndeclaredThrowableException异常,内部属性看得出还是业务异常。

  问题原因:service层的实现类申明了一个检查下异常(BizException)但是接口上没有申明(一般来说代码直接飘红,但是没有,是因为我接口上也有),为什么接口上明明有但是却报这错了呢,实际上是因为这异常时在调试过程中修改代码加上的,没有重新启动,只是热加载了项目,导致代码没有报错但是运行环境中的接口实际上没有该申明。

  解决办法:直接重新启动即可

4、Mybatis 报错:JDBC requires that the JdbcType must be specified for all nullable parameter

  问题描述:使用Mybatis-plus的QueryWrapper进行简单查询时报如题错误,接口没有传参数。

  问题原因:参数判断时写反了,isNotBlank写成了isBlank,导致把空的参数传入了qw对象。大意啊

     QueryWrapper<DcabDatasetItemGroup> qw = new QueryWrapper<>();
        qw.select("dataset_id","group_id","group_name","display_order","comm");
        
     //写成isBlank且datasetId为null报错
     if(StringUtils.isNotBlank(datasetId)){ qw.eq("dataset_id",datasetId); } if(StringUtils.isNotBlank(groupName)){ qw.like("group_name",groupName); } List<DcabDatasetItemGroup> list = itemGroupMapper.selectList(qw); return list;

 5、Springboot + Swagger 启动时报错:ApiModelPropertyPropertyBuilder required a bean of type 'springfox.documenta...

  问题描述:在多模块调整各模块的maven依赖后,父pom添加了swagger的版本为2.9.1,子模块为2.6.1,然后删除了父模块的依赖,之后再启动子模块时报如上错误,根据需要调整代码后又报其他bean的错误

  问题原因:虽然已经删除了2.9.1的依赖,但是因为各子模块之前install时已经把改版本打入了jar包,启动的模块依赖该jar包导致swagger版本始终为2.9.1

  解决办法:彻底删除各个模块的jar包后重新install,最终保证启动的jar包里的swagger为2.6.1的版本。

6、Springboot web项目:javax.servlet.ServletException: Circular view path [xxxx]: ....

  问题描述:在多模块调整各模块的maven依赖后,启动模块成功启动,但是访问页面时报如上错误

  问题原因:缺少thymeleaf依赖,原启动模块有thymeleaf的引用,后调整到父pom但启动模块没有显式引入。访问时spring将该路径当成了转发的地址来处理,但是该转发地址和当前mapping地址一样,导致循环。如下示例:

  

@Controller
public class GreetingController {
    //如果没有thymeleaf return的字符串不会当成视图模板解析,会循环调用greeting接口 @RequestMapping(
"/greeting") public String greeting(@RequestParam(value="name",required=false,defaultValue="World")String name, Model model) { model.addAttribute("name", name); return "greeting"; } }

       解决办法:在启动模块添加thymeleaf引用 ,如下示例:

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

 7、textarea的maxlength标签无效,需要用截取的方式

  <textarea onkeyup="this.value = this.value.slice(0, 80)"></textarea>

 8、父子模块结构中,出现无法实例化子模块的bean问题,提示 “xxxx required a bean of type 'xxx' that could not be found....”

  问题:Springboot父子模块结构中,业务功能模块引用了common模块的包,启动时无法实例化common中的配置bean,提示找不到bean

  原因:子模块的启动类所在的包在子模块的目录中,包路径无法覆盖父模块的bean所在的路径

  解决办法:修改子模块的启动类的包,提到跟到层级的目录中来。

9、Springboot打jar包运行提示“***.jar中没有主清单属性”的问题

  问题:Springboot项目,打jar包,运行时如题提示

  原因:压缩软件打开jar包,META-INF\MANIFEST.MF文件查看,发现没有SpringBoot启动相关的配置,如:Main-Class、Start-Class等

  解决办法:添加或修改springboot打jar包的插件配置

  

<plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <!--打可自行包同时打一个纯代码包用于被其他模块应用-->
                    <classifier>exec</classifier>
                    <!--包括本地jar包(只对可执行包有效)-->
                    <includeSystemScope>true</includeSystemScope>
                </configuration>
            </plugin>

10、Springboot多模块项目,被依赖的模块包含本地jar包,该模块打依赖jar包不包括本地jar包问题,启动报相关的class找不到

  问题:Springboot多模块项目,其中有comm模块被dataset模块依赖,comm模块中有本地jar包引入,打包时使用了“<classifier>exec</classifier>”配置分别打依赖包和可执行包,结果依赖包中不包含本地jar,如果不使用该配置,只打一个执行包,可正常使用,但是包括重复的jar文件,导致最终dataset的可执行包文件很大。

  原因:<classifier>exec</classifier>导致依赖包没有本地jar,<includeSystemScope>true</includeSystemScope>只对可执行jar包有效(因为classifier本来是不打任何第三方依赖的),导致最终的项目包里到处都没有相关的本地jar。

  解决办法:comm包里加<classifier>exec</classifier>,实现可执行包和依赖包都打(但依赖包不包含本地jar),dataset中加上<includeSystemScope>true</includeSystemScope>(虽然dataset没有本地jar,当时依赖传递,实际也依赖这些包)使得dataset在打可执行jar时会引入这些本地jar

 

11、Springboot 加Mybatis-plus项目,yml中的druid配置总是不生效的问题

  问题:因为需要给druid配置加commentAllow:true的功能,修改后发现怎么弄都不能生效,调试了很久,自定义DruidConfig并设置wall(wall内设置commentAllow)的方式也试了不行

  原因:yml中druid配置的路径不对,

  解决办法:

spring:
    application:
        name: dcab-dataset
    datasource:
        dynamic:
            # Mybatis-Plus 确保druid的配置路径为spring.datasource.dynamic.druid
            druid:
                initial-size: 3
                max-active: 8
                min-idle: 2
                ......
                filters: stat,wall
                wall:
                    commentAllow: true #允许sql中有注释
                    loginPassword: 123456        

 12、java.sql.SQLException: sql injection violation, double const condition,Druid数据源查询时报错

  问题:SpringBoot+Druid环境,有一个复杂的sql需要拼接条件,为不写<where>标签,sql中默认加了 ... where 1=1 ...,后为区分是否需要该条查询的结果给sql结尾判断条件给拼上了 “AND 1=0”的条件,执行时报错

  原因:Druid数据源为防止sql注入,限制了sql中出现1=1、1=0这类恒等、恒不等表达式的次数

  解决:1、修改Druid的配置 conditionDoubleConstAllowfalse,配置方法百度

     2、根据情况选择了第二种办法,把1=0的恒不等式变成参数传入的表达式

    

<!--indicatorType  肯定等于1 ,最终结果时 1=0 -->
.....
where 1=1 
<if test='indicatorType == "1"'>
    AND #{indicatorType}=0
</if>

<!--这样写在druid下会报错 -->
.....
where 1=1 
<if test='indicatorType == "1"'>
    AND 1=0
</if>

 13、Springboot+Mybatis+PageHelper 分页无效,返回了全部数据,PageHelper未分页返回全部

  问题:Springboot项目,使用PageHelper插件分页,发现返回的数据中,分页相关参数正常但是list为全部的数据。

  原因:Springboot项目使用的依赖和SpringMvc不一样,需要引入starter依赖

  解决:

<!-- SpringMvc 分页插件 --> 
<!-- 
<dependency>
      <groupId>com.github.pagehelper</groupId>
      <artifactId>pagehelper</artifactId>
      <version>4.0.0</version>
</dependency>
 --> 

<!-- SpringBoot 分页插件 --> 
 <dependency>
      <groupId>com.github.pagehelper</groupId>
      <artifactId>pagehelper-spring-boot-starter</artifactId>
      <version>1.2.5</version>
</dependency>

 14、Oracle Timestamp字段在使用透传sql查询获取List<Map<String,Object>>类型结果时,无法序列化的问题

  问题:Mybatis+Oracle+SpringBoot,Mapper查询返回List<Map<String,Object>>,接口返回报错,无法序列化TimeStamp类型字段,报错如下:

11:58:21.931 [http-nio-8080-exec-1] ERROR com.nstc.dataset.controller.AccountQueryController - No serializer found for class java.io.ByteArrayInputStream and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: com.github.pagehelper.PageInfo["list"]->com.github.pagehelper.Page[0]->java.util.HashMap["CREATE_TIME"]->oracle.sql.TIMESTAMP["stream"])
com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class java.io.ByteArrayInputStream and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: com.github.pagehelper.PageInfo["list"]->com.github.pagehelper.Page[0]->java.util.HashMap["CREATE_TIME"]->oracle.sql.TIMESTAMP["stream"])
    at com.fasterxml.jackson.databind.exc.InvalidDefinitionException.from(InvalidDefinitionException.java:77)
   ...

  解决办法:


//springBoot启动类run方法加下面配置,
解决timestamp字段无法序列化问题 System.getProperties().setProperty("oracle.jdbc.J2EE13Compliant", "true");

 

posted on 2022-01-18 11:12  parker_yu  阅读(2997)  评论(0编辑  收藏  举报

导航