解決:oracle+myBatis ResultMap 類型爲 map 時返回結果中存在 timestamp 時使用 jackson 轉 json 報錯

前言:最近在做一個通用查詢單表的組件,所以 sql 的寫法就是 select *,然後 resultType="map" ,然後使用 jackson @ResponseBody 返回前端報錯。

後臺報錯:

26-Sep-2018 22:18:08.209 WARNING [http-apr-8080-exec-8] org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver.handleHttpMessageNotWritable Failed to write HTTP message: org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON: No serializer found for class java.io.ByteArrayInputStream and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS); nested exception is 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: java.util.HashMap["pageData"]->java.util.ArrayList[2]->java.util.HashMap["UPDATE_TIME"]->oracle.sql.TIMESTAMP["stream"]) 

表字段(oracle):

sql (mybatis):

原因:

經測試,oracle 數據庫字段爲 Data 型的並不會報錯,只有 timestamp 類型會報錯。

從後臺報錯日誌中發現(through reference chain: java.util.HashMap["pageData"]->java.util.ArrayList[2]->java.util.HashMap["UPDATE_TIME"]->oracle.sql.TIMESTAMP["stream"]) ,

然後發現返回的 map 裏面 update_time 字段爲 oracle.sql.TIMESTAMP 類型,並不是 java.sql.Timestamp,所以 json 轉換出錯。

其實都是因爲 mybatis 當 ResultMap 爲 map 時,會把數據的原始類型原樣返回,所以得到的map裏面都是 oracle.sql.DATE、oracle.sql.TIMESTAMP 之類的。因爲 mybatis 在沒有指定類型時都會採用 ObjectTypeHandle 來處理字段。

解決方案:

自定義 typeHandle 來統一處理數據庫字段類型爲 timestamp 等特殊的字段。

這裏 typeHandle 裏面使用註解配置 JdbcType 和 JavaType。這兩個註解的定義是:

  • @MappedTypes 定義的是 JavaType 類型,可以指定哪些 Java 類型被攔截。
  • @MappedJdbcTypes 定義的是 JdbcType 類型,它需要滿足枚舉類 org.apache.ibatis.type.JdbcType 所列的枚舉類型。

代碼如下:

myBatis 的配置文件中加入:

<typeHandlers>
        <typeHandler handler="com.yule.system.typehandler.MyObjectTypeHandle"/>
    </typeHandlers>

新增新的 java 類:

package com.yule.system.typehandler;

import oracle.sql.DATE;
import oracle.sql.TIMESTAMP;
import oracle.sql.TIMESTAMPLTZ;
import oracle.sql.TIMESTAMPTZ;
import org.apache.ibatis.type.*;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;

/**
 * 模仿 ObjectTypeHandle 來處理 timestamp 報錯問題
 * @author yule
 * @date 2018/9/26 22:43
 */
@MappedTypes({Object.class})
@MappedJdbcTypes(value = {JdbcType.TIMESTAMP})
public class MyObjectTypeHandle extends BaseTypeHandler<Object> {

    public MyObjectTypeHandle() {
    }

    @Override
    public void setNonNullParameter(PreparedStatement ps, int i, Object parameter, JdbcType jdbcType) throws SQLException {
        ps.setObject(i, parameter);
    }

    @Override
    public Object getNullableResult(ResultSet rs, String columnName) throws SQLException {
        Object result = rs.getObject(columnName);
        return rs.wasNull() ? null : dealResult(result);
    }

    @Override
    public Object getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
        Object result = rs.getObject(columnIndex);
        return rs.wasNull() ? null : dealResult(result);
    }

    @Override
    public Object getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
        Object result = cs.getObject(columnIndex);
        return cs.wasNull() ? null : dealResult(result);
    }

    /**
     * 爲了解決錯誤:
     * 26-Sep-2018 14:21:06.634 WARNING [http-apr-8080-exec-6] org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver.handleHttpMessageNotWritable Failed to write HTTP message: org.springframework.http.converter.HttpMessageNotWritableException:
     * Could not write JSON: No serializer found for class java.io.ByteArrayInputStream and no properties discovered to create BeanSerializer
     * (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS);
     * nested exception is 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: java.util.HashMap["pageData"]->java.util.ArrayList[0]->java.util.HashMap["UPDATE_TIME"]->oracle.sql.TIMESTAMP["stream"])
     * @param result
     * @return
     * @throws SQLException
     */
    private Object dealResult(Object result) throws SQLException {
        if (result instanceof TIMESTAMP) {
            return new Date(((TIMESTAMP) result).dateValue().getTime());
        } else if (result instanceof DATE) {
            return new Date(((DATE) result).dateValue().getTime());
        } else if (result instanceof TIMESTAMPLTZ) {
            return new Date(((TIMESTAMPLTZ) result).dateValue().getTime());
        } else if (result instanceof TIMESTAMPTZ) {
            return new Date(((TIMESTAMPTZ) result).dateValue().getTime());
        } else{
            return result;
        }
    }
}

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章