解决:oracle+myBatis ResultMap 类型为 map 时返回结果中存在 timestamp 时使用 jackson 转 json 报错

前言:最近在做一个通用查询单表的组件,所以 sql 的写法就是 select *,然后 resultType="map" ,然后使用 jackson @ResponseBody 返回前端报错。 转载请注明出处:https://www.cnblogs.com/yuxiaole/p/9708485.html

后台报错:

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;
        }
    }
}

 

转载请注明出处:https://www.cnblogs.com/yuxiaole/p/9708485.html 

 

文章来自:https://www.cnblogs.com/yuxiaole/p/9708485.html
© 2021 jiaocheng.bubufx.com  联系我们
ICP备案:鲁ICP备09046678号-3