Java报错:Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.http.converter.
报错内容
Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.http.converter.HttpMessageConversionException: Type definition error: [simple type, class oracle.jdbc.OracleConnection]; nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Direct self-reference leading to cycle (through reference chain: cn.newangels.common.MarsResult["data"]->java.util.HashMap["list"]->java.util.ArrayList[0]->java.util.HashMap["I_RE_MSG"]->oracle.sql.CLOB["physicalConnection"]->oracle.jdbc.driver.T4CConnection["wrapper"])] with root cause
com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Direct self-reference leading to cycle (through reference chain: cn.newangels.common.MarsResult["data"]->java.util.HashMap["list"]->java.util.ArrayList[0]->java.util.HashMap["I_RE_MSG"]->oracle.sql.CLOB["physicalConnection"]->oracle.jdbc.driver.T4CConnection["wrapper"])
截图
错误原因
从oracle获取的数据类型为CLOB类型导致输出异常
解决方式(两种方法)
- 在配置文件application.xml中添加代码:
spring: jackson: serialization:
注意:这样可以解决报错问题但输出异常,不建议使用
- 添加方法
/**
* @param clob
* @return String
* @ clobl类型的数据转换为字符串输出
* @throws SQLException
* @throws IOException
*/
public String ClobToString (Clob clob) throws SQLException, IOException{
String reString = "";
java.io.Reader is = clob.getCharacterStream();//得到流
BufferedReader br = new BufferedReader(is);
String s = br.readLine();
StringBuffer sb = new StringBuffer();
while (s != null) {
sb.append(s);
s = br.readLine();
}
reString = sb.toString();
return reString;
}
使用方法
try {
for (Map m : list) {
for (Map col : columns) {
String colName = String.valueOf(col.get("COLUMN_NAME")); //列名
String colType = String.valueOf(col.get("DATA_TYPE")); //列的类型
Object objItem = m.get(colName); //数据内容
if (colType.equals("CLOB")) {
try {
if(objItem != null) {
String str = ClobToString((Clob) objItem);
m.put(colName,str);
}
} catch (SQLException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}
} catch (Exception e) {
throw new Exception(e);
}