org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; 前言中不允許有內容。


spring boot項目啟動報錯,錯誤提示如下:

nested exception is org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'sqlSessionFactory' defined in class path resource [org/mybatis/spring/boot/autoconfigure/MybatisAutoConfiguration.class]:
Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException:
Failed to instantiate [org.apache.ibatis.session.SqlSessionFactory]: Factory method 'sqlSessionFactory' threw exception;
nested exception is org.springframework.core.NestedIOException: Failed to parse config resource:
class path resource [mybatis-config.xml]; nested exception is org.apache.ibatis.builder.BuilderException:
Error parsing SQL Mapper Configuration. Cause: org.apache.ibatis.builder.BuilderException:
Error creating document instance. Cause: org.xml.sax.SAXParseException;
lineNumber: 1; columnNumber: 1; 前言中不允許有內容。;

 

仔細肉眼檢查了mybatis-config.xml和各個新提交的mapper映射文件,都看不出什么問題,百思不得其解。

經大師提點,懷疑是字符編碼問題,於是把新提交的mapper文件復制出來用notepad打開,點擊格式菜單,發現這幾個文件的編碼格式都是 “以UTF-8格式編碼”,改成 “以UTF-8無BOM格式編碼”,保存之后再復制回來,啟動項目,報錯消失。

原來這幾個mapper文件都是由模板生成,模板生成出來的文件是UTF-8帶BOM格式,java只認UTF-8無BOM格式。

 大多數記事本工具(Emedit等)默認是以utf-8 BOM方式,會在文件中加入BOM頭,導致這個錯誤:

 Cause: org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1;前言中不允許有內容。

用notepad打開,格式改成 “以UTF-8無BOM格式編碼”即可。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM