ConfigurationException: ModelMapper configuration errors:

2045 views
Skip to first unread message

Jeevanandam M.

unread,
Oct 27, 2014, 11:34:47 PM10/27/14
to model...@googlegroups.com
Hello Jonathan - 

I'm trying to use ModelMapper in a grails project. Its works and I need to skip certain field while mapping from Domain to DTO. So I have tried as per documentation, but I get configuration error and Null pointer exception.

I search around the solution/workaround, references indicates me; this is fixed in 0.7.2 version. But I'm using this version and still getting this exception. Need help here!

Adding Mapping:

modelMapper.addMappings(new PropertyMap<Product, ProductDto>() {

@Override

protected void configure() {

skip().notes = null

skip().steps = null

/*or 

skip().setNotes(null)

skip().setSteps(null)*/

}

})



Exception:

Caused by ConfigurationException: ModelMapper configuration errors:

1) Error reading class <removed class name while pasting>

2) Failed to configure mappings

2 errors

->>  241 | throwConfigurationExceptionIfErrorsExist in org.modelmapper.internal.Errors

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 

|    206 | build     in org.modelmapper.internal.ExplicitMappingBuilder

|     72 | addMappings in org.modelmapper.internal.TypeMapImpl

|    110 | getOrCreate in org.modelmapper.internal.TypeMapStore

|     93 | addMappings in org.modelmapper.ModelMapper

|     79 | initializeModelMapper in <removed class name while pasting>

|     50 | init . .  in     ''

|     28 | doCall    in BootStrap$_closure1

|    327 | evaluateEnvironmentSpecificBlock in grails.util.Environment

|    320 | executeForEnvironment in     ''

|    296 | executeForCurrentEnvironment in     ''

|    262 | run       in java.util.concurrent.FutureTask

|   1145 | runWorker in java.util.concurrent.ThreadPoolExecutor

|    615 | run       in java.util.concurrent.ThreadPoolExecutor$Worker

^    745 | run . . . in java.lang.Thread


Caused by NullPointerException: null

->>  236 | validateVisitedMappings in org.modelmapper.internal.ExplicitMappingBuilder

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 

|    227 | visitPropertyMap in     ''

|    380 | configure in org.modelmapper.PropertyMap

|    194 | build     in org.modelmapper.internal.ExplicitMappingBuilder

|     72 | addMappings in org.modelmapper.internal.TypeMapImpl

|    110 | getOrCreate in org.modelmapper.internal.TypeMapStore

|     93 | addMappings in org.modelmapper.ModelMapper

|     79 | initializeModelMapper in <removed class name while pasting>

|     50 | init . .  in     ''

|     28 | doCall    in BootStrap$_closure1

|    327 | evaluateEnvironmentSpecificBlock in grails.util.Environment

|    320 | executeForEnvironment in     ''

|    296 | executeForCurrentEnvironment in     ''

|    262 | run       in java.util.concurrent.FutureTask

|   1145 | runWorker in java.util.concurrent.ThreadPoolExecutor

|    615 | run       in java.util.concurrent.ThreadPoolExecutor$Worker

^    745 | run . . . in java.lang.Thread


Thanks,
Jeeva

Matas Kondratavičius

unread,
Nov 19, 2014, 6:13:28 AM11/19/14
to model...@googlegroups.com
I have exactly the same problem, doing exactly same thing as poster above, when trying to add custom mappings to my model mapper. I had it on 0.7.2 and now on 0.7.3 and i have no idea how to fix it. Any help would be appreciated.


W/System.err﹕ Caused by: java.lang.NullPointerException
W/System.err﹕ at org.modelmapper.internal.ExplicitMappingBuilder.validateVisitedMappings(ExplicitMappingBuilder.java:237)
W/System.err﹕ at org.modelmapper.internal.ExplicitMappingBuilder.visitPropertyMap(ExplicitMappingBuilder.java:228)
W/System.err﹕ at org.modelmapper.PropertyMap.configure(PropertyMap.java:380)
W/System.err﹕ at java.lang.reflect.Method.invokeNative(Native Method)
W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:515)
W/System.err﹕ at org.modelmapper.internal.ExplicitMappingBuilder.build(ExplicitMappingBuilder.java:195)

Matas Kondratavičius

unread,
Nov 19, 2014, 7:47:26 AM11/19/14
to model...@googlegroups.com
I should also add that i am doing this on Android.

Jonathan Halterman

unread,
Nov 19, 2014, 1:41:42 PM11/19/14
to model...@googlegroups.com
Right now Android and Groovy are not supported since they both emit different bytecode which ModelMapper cannot read. I'd like to get these working in the future, but don't have an ETA.

Regards,
Jonathan

Matas Kondratavičius

unread,
Nov 19, 2014, 2:11:39 PM11/19/14
to model...@googlegroups.com
Yes, i already figured it out, however it does seem to work if i do not do explicit mappings. I have simple dto and entity objects and they map without any problems and then i have complex, heavy objects which also map, just that the mapping process takes 10 minutes to finish (for 2 objects). If i try to add PropertyMap, it fails with NoClassFound exception.

Jeevanandam M.

unread,
Nov 19, 2014, 2:19:24 PM11/19/14
to model...@googlegroups.com
It works in Groovy/Grails perfectly and does fast mapping too. Currently for skipping a destination field I'm using propertyCondition approach.

But when I added addMapping(...) using PropertyMap for skipping destination field. I end up with error posted in this thread.

Regards,
Jeeva

Jonathan Halterman

unread,
Nov 19, 2014, 2:31:03 PM11/19/14
to model...@googlegroups.com
Exactly - it's where the ProperyMap is needed that things might not work with Groovy/Android since the bytecode contents of the PropertyMap are read.

--
You received this message because you are subscribed to the Google Groups "modelmapper" group.
To unsubscribe from this group and stop receiving emails from it, send an email to modelmapper...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Matas Kondratavičius

unread,
Nov 20, 2014, 2:15:28 AM11/20/14
to model...@googlegroups.com
Could you share your solution with propertyCondition?

Jeevanandam M.

unread,
Nov 20, 2014, 1:19:15 PM11/20/14
to model...@googlegroups.com
This is approach I have implemented-

Please note: below code snippet is Groovy Syntax, kindly transform it to Java.

Helper method: for typed arguments you can use <?, ?> to make it generic or leave it, you might get warning for Raw type usage

/**

* Creating a Mapper field skip condition 

* @param skipFields list of string. For eg. ['notes', 'steps']

* @return {@link org.modelmapper.Condition}

*/

static Condition createSkipCondition(def skipFields) {

new Condition() {

boolean applies(MappingContext context) {

!(context.mapping.destinationProperties[0].name in skipFields)

}

}

}


Method for type map creation with skipFields:

private void createTypeMap(Class source, Class target, String typeMapName = null, def skipFields = null) {

if (skipFields && typeMapName) {

modelMapper.createTypeMap(source, target, typeMapName).propertyCondition = Helper.createSkipCondition(skipFields)

} else if (typeMapName) {

modelMapper.createTypeMap(source, target, typeMapName)

} else if (skipFields) {

modelMapper.createTypeMap(source, target).propertyCondition = Helper.createSkipCondition(skipFields)

} else {

modelMapper.createTypeMap(source, target)

}

}


Finally using above method to achieve it: Sample

/**

* Domain To DTO Mapping

* Note: Mapping is required only if you want multiple combination of fields while mapping.

* For Example-

* ------------

* Scenario:: Product ==> ProductDto

* Combinations:

* 1. Default is skip notes and steps always

* 2. Skip only notes

* 3. Skip only steps

* 4. All data - Don't skip at all

*/

createTypeMap(Product, ProductDto, null, ['notes', 'steps'])

createTypeMap(Product, ProductDto, PRODUCT_TO_DTO_NOTES, ['steps'])

createTypeMap(Product, ProductDto, PRODUCT_TO_DTO_STEPS, ['notes'])

createTypeMap(Product, ProductDto, PRODUCT_TO_DTO_NOTES_STEPS)



Regards,
Jeeva
Reply all
Reply to author
Forward
0 new messages