SpringBoot项目(七)报错和常见问题处理

作者:陆金龙    发表时间:2019-07-19 18:54   

关键词:SilentExitExceptionHandler  Perhaps you are running on a JRE rather than a JDK  Freemarker不生效  ThymeLeaf报错处理  Java Problems  listen on port 8080 failed to start  父工程视图红叉  error querying database  java.util.Date Freemarker解析错误  传递后台java.util.Date解析错误  mybatis向mysql数据库插入记录中文乱码  

1. SilentExitExceptionHandler

以debug方式启动springboot之后,都会在SilentExitExceptionHandler类中的throw new SilentExitException()处终止,虽然不影响程序运行。

解决办法 :window->preferences ->java->debug 取消“suspend execution on uncaught exceptions”选项即可。

2.maven编译报错:Perhaps you are running on a JRE rather than a JDK

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project klcms-web: Compilation failure

[ERROR] No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?

原因:eclipse默认是运行在jre上的,但是maven插件需要使用jdk。

将【Window】-->【Prefrences】-->【Java】-->【Installed JREs】的jre修改为使用jdk。

JDK(java development kit):java 开发工具,包括一系列java命令,如javac(java compiler) 等;同时jdk包含jre,因为jdk其实是面向java开发人员,开发完之后还要进行调试运行等,这就需要jre了;

JRE(java runtime environment):java 运行时环境,java文件被javac编译过后形成字节码文 件,这个文件计算机是不能理解和处理的,需要有jvm(java virtual machine)处理后形成机器语言在传递给计算机处理 (这也是为什么说java具有平台无关性)。Jvm就存在于jre 中。可见,jre其实是面向java程序的使用者

3.Freemarker不生效

@RestController

public class DemoController {

     @RequestMapping(value = "/index", method = RequestMethod.GET)

     public String index(ModelMap map) {

        map.put("name", "freemarker");

        map.put("age", "30");

       return "index";

    }
}

上述代码,访问http://localhost:8080/index,直接返回字符串index,没有使用到视图模板。请求结果如下图所示:

将注解@RestController改成@Controller,成功。

@Controller

public class DemoController {

    @RequestMapping(value = "/index", method = RequestMethod.GET)

    public String index(ModelMap map) {

        map.put("name", "freemarker");

        map.put("age", "30");

        return "index";

    }
}

改成@Controller后,如果需要返回Json格式的,可添加@ResponseBody注解。

@ResponseBody

@RequestMapping("/json")

public User json() {

    User user = new User();

   user.setName("king");

   user.setAge(30);

   return user;

}

4.ThymeLeaf报错处理

    @RequestMapping("/thymeleaf")

    public String thymeleaf(ModelMap map){

        map.put("hello","<h1>你好 hello</h1>");

        map.put("users",Arrays.asList("zhangsan","lisi","wangwu"));

        return "thymeleaf";

    }

   

javax.servlet.ServletException: Circular view path [thymeleaf]: would dispatch back to the current handler URL [/thymeleaf] again.

原因:如果你的view name和你的path是相同的字符串,根据Spring的转发规则,就等于让自己转发给自己,会陷入死循环。所以Spring会检查到这种情况,于是抛出Circular view path异常。

解决方案:将path改成thymeleafdemo,如下:

    //查出用户数据,在页面展示

    @RequestMapping("/thymeleafdemo")

    public String thymeleaf(ModelMap map){

        map.put("hello","<h1>你好 hello</h1>");

        map.put("users",Arrays.asList("zhangsan","lisi","wangwu"));

        return "thymeleaf";

}

 

提示404

  检查发现pom.xml中maven依赖项thymeleaf被注释掉了,去掉注释,OK。

   <!-- <dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-thymeleaf</artifactId>

</dependency> -->

5. Java Problems

1)SpringFramework相关jar包报错

原因:maven 使用阿里云仓库,下载springframework的jar包没成功。

解决方案:

A. 注释掉阿里云仓库配置C:\Users\kinglong\.m2\settings.xml,如下图所示:

  <mirrors>

 <!--<mirror>

      <id>alimaven</id>

      <mirrorOf>central</mirrorOf>

      <name>aliyun maven</name>

      <url>http://maven.aliyun.com/nexus/content/groups/public/</url>

    </mirror>-->

  </mirrors>

B. 把C:\Users\Administrator\.m2\repository\org\springframework下的文件夹全部删除

C. 对父工程执行maven 》Update Project,问题解决。

2)lombok插件不生效

解决方案:检查并配置Eclipse集成lombok的环境。参照 Eclipse集成lombok

重启eclipse,再clean project。之后user.setName()不再报错。

6.listen on port 8080 failed to start

报错:org.springframework.boot.context.embedded.tomcat.ConnectorStartFailedException: Connector configured to listen on port 8080 failed to start

原因:8080端口已被占用,换个端口启动。

7.父工程视图红叉

子模块编译和运行正常,maven项目没有错,但是在父项目头上有红叉。problem中提示springboot解析错误。

原因:可能之前出了错,Eclipse中有缓存的问题。

解决方案:项目右键》delete,然后重新import项目,问题解决。

8.error querying database

error querying database. Cause: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure The last packet successfully received from the server was 1,206,915 milliseconds ago. The last packet sent successfully to the server was 18,909 milliseconds ago. 

超时了,重启服务解决。

9.java.util.Date 前端Freemarker解析错误

使用java.util.Date ,前端${art.createTime},报错 Can't convert the date-like value to string because it isn't known if it's a date (no time part), time or date-time value。...Use ?date, ?time, or ?datetime to tell FreeMarker the exact type. ---- Tip: If you need a particular format only once, use ?string(pattern), like ?string('dd.MM.yyyy HH:mm:ss')

解决方案:前端改为<div >${art.createTime?string('yyyy-MM-dd HH:mm')}</div>

10.前端字符串传递给后台java.util.Date解析错误

Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'createTime'

Field error in object 'form' on field 'createTime': rejected value [2019-05-16 09:22]; codes [typeMismatch.form.createTime,typeMismatch.createTime,typeMismatch.java.util.Date,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [form.createTime,createTime]; arguments []; default message [createTime]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'createTime';

原因:前后端格式不一致

前端格式(精确到分钟):<input name="createTime" value="${article.createTime?string('yyyy-MM-dd HH:mm')}" /> 

后端格式(精确到秒): @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")

private Date createTime;

将该页面前台页面表单元素的显示格式与后台定义的格式保持一致,都精确到秒,问题解决:

<input name="createTime" value="${article.createTime?string('yyyy-MM-dd HH:mm:ss')}" /> 

11.mybatis向mysql数据库插入记录,中文乱码

为mysql数据库的连接字符串设置编码方式,之后就正常了。

url: jdbc:mysql://95.169.17.226:3306/klcms   改为

url: jdbc:mysql://95.169.17.226:3306/klcms?useUnicode=true&characterEncoding=utf8

12.MySQL时区问题

select now()  #可以通过查看当前时间的值,检查与所在时区的差别

set global time_zone = '+8:00'; ##修改mysql全局时区为北京时间,即我们所在的东8区

set time_zone = '+8:00'; ##修改当前会话时区

flush privileges; #立即生效

13.failed to convert value of type 'java.lang.String' to required type 'java.lang.Long'; nested exception is java.lang.NumberFormatException: For input string: "index"

item/255.html页面内部,母版页中侧边栏菜单的连接使用了"./index.html?",相对路径不对。

母版页中改为"/index.html?",则各路径下的页面,菜单超链接跳转路径都不会出错了。

14.Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is com.alibaba.druid.pool.DataSourceDisableException

出现以上错误原因是:数据源的错误配置:检查并修复配置,或者从正常运行的配置文件复制一份来修改。

15.数据库连接不上,报错com.alibaba.druid.pool.DruidDataSource   : create connection error, url: jdbc:mysql://klfront.com:3306/klcms?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC, errorCode 0, state 08S01

描述:

A.开发环境程序和开发机数据库OK

B.开发环境程序,服务器数据库 OK

C.云服务器部署后报错,create connection error.....errorCode 0, state 08S01,不管数据库里连接里使用域名、ip、localhost问题都重现。微服务启动后,端口是通的。分析数据库环境和java服务运行都没问题。查到服务器安装的是openjdk,怀疑是jdk对mysql驱动的支持不好

(1)jdk原因:openjdk对org.gjt.mm.mysql.Driver的支持问题 。解决方案:卸载openjdk 安装jdk1.8

卸载openjdk 安装jdk1.8

(A).查看openjdk相关文件 rpm -qa | grep jdk,内容如下

copy-jdk-configs-3.3-10.el7_5.noarch

java-1.8.0-openjdk-devel-1.8.0.312.b07-1.el7_9.x86_64

java-1.8.0-openjdk-headless-1.8.0.312.b07-1.el7_9.x86_64

java-1.8.0-openjdk-1.8.0.312.b07-1.el7_9.x86_64

(B).逐个卸载

rpm -e --nodeps java-1.8.0-openjdk-1.8.0.312.b07-1.el7_9.x86_64

rpm -e --nodeps java-1.8.0-openjdk-headless-1.8.0.312.b07-1.el7_9.x86_64

rpm -e --nodeps java-1.8.0-openjdk-devel-1.8.0.312.b07-1.el7_9.x86_64

rpm -e --nodeps copy-jdk-configs-3.3-10.el7_5.noarch

(C).下载安装jdk 1.8

下载:https://www.oracle.com/java/technologies/javase/javase8-archive-downloads.html

上传:上传到服务器上 /usr/local/

解压:tar -vzxf jdk-8u192-linux-x64.tar.gz

设置环境变量

[root@localhost ]# vi /etc/profile

末尾添加

#java_home

export JAVA_HOME=/usr/local/jdk1.8.0_192

export PATH=$PATH:$JAVA_HOME/bin

保存退出,执行: source /etc/profile

 

(2)连接字符串原因,将jdbc:mysql://klfront.com:3306/改成jdbc:mysql://localhost:3306/

(3)mysql连接驱动依赖(不确定是否有该原因)

去看了一下pom文件的mysql的相关依赖

标明为低版本 5.1.48

<dependency>

<groupId>mysql</groupId>

<artifactId>mysql-connector-java</artifactId>

<version>5.1.48</version>

</dependency>

16.禁用spring ico,使用自定义favicon.ico

1)在src/main/resources下的static文件夹中放入新的ico

解压klcms.jar,打开favicon.ico提示以下错误。

并且发现jar包中favicon.ico文件比原来大一些(由8k变为13k)。

2)解决方法与字体图标无法显示的问题相同

重新配置maven-resources-plugin,通过<nonFilteredFileExtension>排除对ico文件的过滤

修改上述配置后,重新打包部署,问题解决。

17.浏览器兼容性问题

在IE5 IE7 IE8 IE9 等文档模式下,布局都有问题。

将浏览器改到IE10或者IE11模式下访问,则正常。

在IE11以及360浏览器文档模式默认为IE7版本,可以通过设置meta标签改变为其他模式,例如以下设置:

<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />

如果存在客户端Chrome Frame并启用,那么浏览器访问页面会被Chrome内核渲染。

使用IE内核浏览器来访问,则会渲染至该浏览器的最高版本,如下图,添加上述设置,更新到服务器后,文档模式的默认值显示IE11