You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Javers对比Protocol Buffer对象时遇映射错误求助

Fixing Javers MANAGED_CLASS_MAPPING_ERROR with Protobuf's UnmodifiableLazyStringList

This error pops up because Javers doesn’t recognize Protobuf’s UnmodifiableLazyStringList (generated from repeated string fields) as a collection type—instead, it tries to treat it as a managed entity/value object, which it’s not. Here are two reliable solutions to resolve this:

Solution 1: Custom JsonTypeAdapter for UnmodifiableLazyStringList

Create a type adapter to convert the Protobuf-specific list into a standard List<String> that Javers can handle natively:

Step 1: Implement the JsonTypeAdapter

import com.google.protobuf.UnmodifiableLazyStringList;
import org.javers.core.json.JsonTypeAdapter;
import java.util.ArrayList;
import java.util.List;

public class UnmodifiableLazyStringListAdapter implements JsonTypeAdapter<UnmodifiableLazyStringList> {
    @Override
    public List<String> serialize(UnmodifiableLazyStringList source) {
        // Convert the lazy list to a standard ArrayList Javers understands
        return new ArrayList<>(source);
    }

    @Override
    public UnmodifiableLazyStringList deserialize(List<String> serialized) {
        // Deserialization isn't necessary for comparison scenarios
        throw new UnsupportedOperationException("Deserialization not required for object comparison");
    }

    @Override
    public Class<UnmodifiableLazyStringList> getValueType() {
        return UnmodifiableLazyStringList.class;
    }
}

Step 2: Register the Adapter with Javers

Add this adapter when building your Javers instance:

Javers javers = JaversBuilder.javers()
    .registerJsonTypeAdapter(new UnmodifiableLazyStringListAdapter())
    .build();

Solution 2: Configure TypeMapper to Recognize the List Type

Tell Javers explicitly that UnmodifiableLazyStringList is a collection type by extending the default TypeMapper:

import com.google.protobuf.UnmodifiableLazyStringList;
import org.javers.core.JaversBuilder;
import org.javers.core.metamodel.type.ListType;
import org.javers.core.metamodel.type.TypeMapper;

Javers javers = JaversBuilder.javers()
    .withTypeMapper(new TypeMapper(JaversBuilder.javers().getCoreConfiguration()) {
        @Override
        public JaversType getJaversManagedType(Class<?> javaClass) {
            if (UnmodifiableLazyStringList.class.isAssignableFrom(javaClass)) {
                return new ListType(javaClass, this);
            }
            return super.getJaversManagedType(javaClass);
        }
    })
    .build();

Alternative: Ignore the Problematic Field (If Comparison Isn't Needed)

If you don’t need to compare the repeated string field at all, you can tell Javers to ignore it entirely:

Javers javers = JaversBuilder.javers()
    .registerIgnoredProperty(Foret.class, "info")
    .build();

Why This Error Occurs

Javers categorizes types into two main groups:

  • ManagedType: Entities or value objects that Javers tracks as individual objects
  • CollectionType: Lists, sets, maps, etc., that Javers handles as containers of values

Protobuf’s UnmodifiableLazyStringList is a subclass of List, but Javers’ default type mapping doesn’t recognize it as a collection. Instead, it attempts to map it as a ManagedType, leading to the MANAGED_CLASS_MAPPING_ERROR you encountered.

内容的提问来源于stack exchange,提问作者flxdns

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.11 09:12:38