在托管bean中使用ModelMapper库的自定义映射会导致NullPointerException

时间:2014-09-30 14:06:27

标签: java java-ee ejb jax-rs modelmapper

当我尝试通过在一些容器管理的对象中创建自定义映射器来添加映射时,我得到NullPointerException。我在@Stateless EJB服务和@RequestScoped休息服务中尝试过它。错误类似,并在行中发生:

modelMapper.addMappings(skipCdeMap);

这是一个错误吗?我假设mapper试图在托管bean上做一些反思,因为它得到了NullPointerException

这是我的代码:

PropertyMap<ObjectAbcDto, ObjectAbc> skipCdeMap =
    new PropertyMap<ObjectAbcDto, ObjectAbc>() {
        protected void configure() {
            skip().setObjectCde(null);
        }
    };

modelMapper.addMappings(skipCdeMap);

这是错误:

13:37:03,453 ERROR [stderr] (default task-12) org.modelmapper.ConfigurationException: ModelMapper configuration errors:
13:37:03,453 ERROR [stderr] (default task-12) 
13:37:03,453 ERROR [stderr] (default task-12) 1) Error reading class com.app.SomeRestEasyService$1
13:37:03,453 ERROR [stderr] (default task-12) 
13:37:03,453 ERROR [stderr] (default task-12) 2) Failed to configure mappings
13:37:03,454 ERROR [stderr] (default task-12) 
13:37:03,454 ERROR [stderr] (default task-12) 2 errors
13:37:03,454 ERROR [stderr] (default task-12) at org.modelmapper.internal.Errors.throwConfigurationExceptionIfErrorsExist(Errors.java:241)
13:37:03,454 ERROR [stderr] (default task-12) at org.modelmapper.internal.ExplicitMappingBuilder.build(ExplicitMappingBuilder.java:206)
13:37:03,454 ERROR [stderr] (default task-12) at org.modelmapper.internal.TypeMapImpl.addMappings(TypeMapImpl.java:72)
13:37:03,454 ERROR [stderr] (default task-12) at org.modelmapper.internal.TypeMapStore.getOrCreate(TypeMapStore.java:101)
13:37:03,454 ERROR [stderr] (default task-12) at org.modelmapper.ModelMapper.addMappings(ModelMapper.java:93)
13:37:03,454 ERROR [stderr] (default task-12) at com.app.SomeRestEasyService.create(SomeRestEasyService.java:129)
13:37:03,454 ERROR [stderr] (default task-12) at com.app.SomeRestEasyService$Proxy$_$$_WeldClientProxy.create(Unknown Source)        
[...]   
13:37:03,459 ERROR [stderr] (default task-12) Caused by: java.lang.NullPointerException
13:37:03,459 ERROR [stderr] (default task-12) at org.modelmapper.internal.ExplicitMappingBuilder.validateVisitedMappings(ExplicitMappingBuilder.java:236)
13:37:03,459 ERROR [stderr] (default task-12) at org.modelmapper.internal.ExplicitMappingBuilder.visitPropertyMap(ExplicitMappingBuilder.java:227)
13:37:03,459 ERROR [stderr] (default task-12) at org.modelmapper.PropertyMap.configure(PropertyMap.java:380)
13:37:03,459 ERROR [stderr] (default task-12) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
13:37:03,459 ERROR [stderr] (default task-12) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
13:37:03,459 ERROR [stderr] (default task-12) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
13:37:03,460 ERROR [stderr] (default task-12) at java.lang.reflect.Method.invoke(Method.java:606)
13:37:03,460 ERROR [stderr] (default task-12) at org.modelmapper.internal.ExplicitMappingBuilder.build(ExplicitMappingBuilder.java:194)
13:37

2 个答案:

答案 0 :(得分:3)

我可以回答我自己的问题。我的假设是正确的。当我在Pojo或自己的类中创建PropertyMap类时,专用的类映射效果很好。所以我想我之前的错误是因为ModelMapper在创建PropertyMap的类上所做的反射而发生的。如果它是一些使用NullPointerException导致的Menaged Bean。

答案 1 :(得分:2)

我遇到同样的问题(使用ModelMapper v0.7.5)尝试将String映射到UUID

例如,有这些类:

public class Car {

    private UUID id;

    // other fields

}

public class CarDto {

    private String id;

    // other fields

}

并尝试设置这样的自定义映射:

mapper.addMappings(new PropertyMap<CarDto, Car>() {
        protected void configure() {
            map().setId(UUID.fromString(source.getId()));
            // other mappings
        }
    });

我收到了相同的配置错误。

原因是UUID是最终类,不能被ModelMapper框架代理。

作为一种解决方法,我在Car类中创建了一个这样的方法:

public void setIdAsString(String id) {
    this.id = UUID.fromString(id);
}