1

Was wondering if there's a way one can handle conversion errors in a lenient way.

Given a query that returns a List[ModelObject]

If there are 5 DBObjects retrieved, one of them is throwing a ConversionException when converted to ModelObject, is there a way to return the 4 convertible objects and provide a hook for the 1 conversion failure?

1 Answer 1

1

A bit late, but this answer might help someone else's search

I agree it would be nice to do this on a policy basis - at the moment, if you get a single conversion error, you get no list at all. I think there are use cases where this is a valid need.

However, since you can't, you need to register a custom converter for your ModelObject that converts leniently...

public class DBObjectToModelObjectConverter implements Converter<DBObject,ModelObject>{

    public ModelObject convert(DBObject s) {
        ... Read from the DBObject into your ModelObject and work around the bad data
    }
}

and then register this as a custom converter in your bean definitions. Something like:

<bean id="lenientModelReadConverter" class="com.my.DBObjectToModelObjectConverter"/>

<mongo:custom-converters>
   <mongo:converter ref="lenientModelReadConverter"/>
</mongo:custom-converters>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.