@ResponseBody处理Clob数据

2023-05-16,,

数据表

create table test
(
  TASKID        NUMBER(10) not null,
  CONFIG CLOB
)

controller

	@ResponseBody
	@RequestMapping("/getTest")
	public List getTest(HttpServletRequest request, HttpServletResponse response){
		return this.Service.getTest();
	}

sql.xml

<select id="gettest"
		resultMap="hashmap">
		select t.config as config,
		       t.taskid
		from test t
	</select>

1、没有做任何处理情况,程序报错如下

Caused by:
org.codehaus.jackson.map.JsonMappingException: No serializer found for class
oracle.sql.LobDBAccessImpl and no properties discovered to create
BeanSerializer (to avoid exception, disable
SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS) ) (through reference chain:
java.util.ArrayList[0]->java.util.HashMap["CONFIG"]->oracle.sql.CLOB["dbaccess"])

2、在sql.xml中,对CLOB字段Config进行to_char()处理,开始能够解决问题,后面出现数据库报错的情况

sql.xml

<select id="gettest"
		resultMap="hashmap">
		select to_char(t.config) as config,
		       t.taskid
		from test t
	</select>

数据库报错

3、去掉@ResponseBody,将返回的结果打印出来,发现config是一个对象

controller

        //@ResponseBody
	@RequestMapping("/getTest")
	public List getTest(HttpServletRequest request, HttpServletResponse response){
	        System.out.println(this.Service.getTest());
		return this.Service.getTest();
	}

debug

[{CONFIG=oracle.sql.CLOB@16e2b70,TASKID=38}]

4、在Mybatis中采用resultMap处理,程序正常,debug时config为具体内容。

sql.xml

	<select id="gettest"
		resultMap="testMap">
		select t.config as config,
		       t.taskid
		from test t
	</select>
	
	<resultMap type="hashmap" id="testMap">
		<result property="CONFIG" column="config" javaType="String" jdbcType="CLOB"/>
		<result property="TASKID" column="taskid"/>
	</resultMap>

《@ResponseBody处理Clob数据.doc》

下载本文的Word格式文档,以方便收藏与打印。