FasterXML / jackson-databind

General data-binding package for Jackson (2.x): works on streaming API (core) implementation(s)
Apache License 2.0
3.51k stars 1.37k forks source link

Cannot serialize object from mybatis and it has a field lazy-loaded #4681

Open formingform opened 2 weeks ago

formingform commented 2 weeks ago

Search before asking

Describe the bug

Jackson 2.13.5 Spring Boot 2.17.x Mybatis 3.5.x JDK 17

Here is my Mybatis map.xml


<resultMap id="BaseResultMap" type="User">
    <id property="id" column="id" jdbcType="INTEGER"/>
    <result property="name" column="name" jdbcType="VARCHAR"/>
    <collection property="bookNames"
                ofType="String"
                column="name"
                jdbcType="VARCHAR"
                select="BookMapper.listBookNamesByOwner" fetchType="lazy"/>
</resultMap>

......

<select id="selectByPrimaryKey" parameterType="java.lang.Long" resultMap="BaseResultMap">
    select
    <include refid="Base_Column_List" />
    from user
    where id = #{id,jdbcType=INTEGER}
</select>

jackson cannot serialize the result of selectByPrimaryKey because of fetchType="lazy", error message like this:

com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class org.apache.ibatis.executor.loader.javassist.JavassistProxyFactory$EnhancedResultObjectProxyImpl and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS)

and if set fetchType=“eager" or remove fetchType="lazy", Jackson works fine.

Version Information

Jackson verison: 2.13.5

Reproduction

yawkat commented 2 weeks ago

Not sure what you want us to do, here

pjfanning commented 2 weeks ago

We do not generally accept issues where many libs are used. Reproducible cases should generally use only Jackson libs. I believe this should be reported to the myBatis community or on stackoverflow.com.

cowtowncoder commented 2 weeks ago

Yes, "exotic" types by 3rd party libraries/frameworks tend to require special supported, provided often as Jackson datatype modules: for Hibernate, there is https://github.com/FasterXML/jackson-datatype-hibernate/ .

Such libraries would ideally be created and maintained by library/framework maintainers. But we sometimes accept contributions if users want to write implementations. That requires someone with myBatis and Jackson knowledge.

In this case it is very likely that such contribution (PR) would be needed. Or possibly someone sharing way they have used Jackson configuration and/or custom (de)serializers.