我使用ModelMapper的方式如下:
我有几个转换器类是Spring组件,它们注册自定义ModelMapper映射
@Component
public class FooConverter {
@Autowired
private ModelMapper modelMapper;
public static final PropertyMap<Foo, FooModel> FOO_TO_FOO_MODEL_MAP = new PropertyMap<Foo, FooModel>() {
@Override
protected void configure() {
map().setTimeZone(source.getTimeZone().getID());
}
};
@PostConstruct
public void init() {
modelMapper.addMappings(FOO_TO_FOO_MODEL_MAP);
}
}
但是当Spring启动时,由于调用了configure函数,并且源代码为null,所以我得到了以下错误。
这怎么可能呢?
我用错了吗?
1 error
at org.modelmapper.internal.Errors.throwConfigurationExceptionIfErrorsExist(Errors.java:241)
at org.modelmapper.internal.ExplicitMappingBuilder.build(ExplicitMappingBuilder.java:244)
at org.modelmapper.internal.ExplicitMappingBuilder.build(ExplicitMappingBuilder.java:96)
at org.modelmapper.internal.TypeMapImpl.addMappings(TypeMapImpl.java:92)
at org.modelmapper.internal.TypeMapStore.getOrCreate(TypeMapStore.java:124)
at org.modelmapper.ModelMapper.addMappings(ModelMapper.java:113)
at com.agilitypr.neptune.account.api.v1.controllers.PreferenceController.getUserAccountPreferences(PreferenceController.java:63)
at com.agilitypr.neptune.account.api.v1.controllers.PreferenceController$$FastClassBySpringCGLIB$$3559fcbb.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:746)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
at org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint.proceed(MethodInvocationProceedingJoinPoint.java:88)
at com.agilitypr.neptune.account.api.v1.filters.AuthorizationFilter.doAuthorize(AuthorizationFilter.java:52)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethodWithGivenArgs(AbstractAspectJAdvice.java:644)
at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethod(AbstractAspectJAdvice.java:633)
at org.springframework.aop.aspectj.AspectJAroundAdvice.invoke(AspectJAroundAdvice.java:70)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:174)
at org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint.proceed(MethodInvocationProceedingJoinPoint.java:88)
at com.agilitypr.neptune.account.api.v1.filters.AuthenticationFilter.doAuthenticate(AuthenticationFilter.java:48)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethodWithGivenArgs(AbstractAspectJAdvice.java:644)
at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethod(AbstractAspectJAdvice.java:633)
at org.springframework.aop.aspectj.AspectJAroundAdvice.invoke(AspectJAroundAdvice.java:70)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:174)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:688)
at com.agilitypr.neptune.account.api.v1.controllers.PreferenceController$$EnhancerBySpringCGLIB$$a4d3aeda.getUserAccountPreferences(<generated>)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:209)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:136)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:102)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:877)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:783)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:991)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:925)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:974)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:866)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:687)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:851)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:865)
at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1655)
at org.eclipse.jetty.websocket.server.WebSocketUpgradeFilter.doFilter(WebSocketUpgradeFilter.java:215)
at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1642)
at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1642)
at org.springframework.web.filter.HttpPutFormContentFilter.doFilterInternal(HttpPutFormContentFilter.java:109)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1642)
at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:93)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1642)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:200)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1642)
at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:533)
at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:146)
at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:548)
at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:132)
at org.eclipse.jetty.server.handler.ScopedHandler.nextHandle(ScopedHandler.java:257)
at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:1595)
at org.eclipse.jetty.server.handler.ScopedHandler.nextHandle(ScopedHandler.java:255)
at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1317)
at org.eclipse.jetty.server.handler.ScopedHandler.nextScope(ScopedHandler.java:203)
at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:473)
at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:1564)
at org.eclipse.jetty.server.handler.ScopedHandler.nextScope(ScopedHandler.java:201)
at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1219)
at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:144)
at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:132)
at org.eclipse.jetty.server.Server.handle(Server.java:531)
at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:352)
at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:260)
at org.eclipse.jetty.io.AbstractConnection$ReadCallback.succeeded(AbstractConnection.java:281)
at org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:102)
at org.eclipse.jetty.io.ChannelEndPoint$2.run(ChannelEndPoint.java:118)
at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:762)
at org.eclipse.jetty.util.thread.QueuedThreadPool$2.run(QueuedThreadPool.java:680)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.NullPointerException
at org.modelmapper.internal.ExplicitMappingBuilder$ExplicitMappingInterceptor.access$000(ExplicitMappingBuilder.java:304)
at org.modelmapper.internal.ExplicitMappingBuilder.createAccessorProxies(ExplicitMappingBuilder.java:287)
at org.modelmapper.internal.ExplicitMappingBuilder.createProxies(ExplicitMappingBuilder.java:277)
at org.modelmapper.internal.ExplicitMappingBuilder.visitPropertyMap(ExplicitMappingBuilder.java:266)
at org.modelmapper.PropertyMap.configure(PropertyMap.java:386)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.modelmapper.internal.ExplicitMappingBuilder.build(ExplicitMappingBuilder.java:227)
... 90 more
bean是这样声明的,Foo和FooModel不是最终的
@Configuration
public class FooConfiguration {
@Bean
public ModelMapper modelMapper() {
return new ModelMapper();
}
}
重要编辑
实际上,即使在主类内的函数中显式创建映射,也会得到相同的NullPointerException。
public class main {
public static void main(String[] args) {
ModelMapper mapper = new ModelMapper();
final PropertyMap<Foo, FooModel> FOO_TO_FOO_MODEL_MAP = new PropertyMap<Foo, FooModel>() {
@Override
protected void configure() {
map().setTimeZone(source.getTimeZone().getID());
}
};
mapper.addMappings(FOO_TO_FOO_MODEL_MAP );
}
}
什么是错的?
我在他们的文档中遵循这个例子。
编辑
深度映射正在处理这些问题
public class InnerFoo {
private int prop3;
public int getProp3() {
return prop3;
}
public void setProp3(int prop3) {
this.prop3 = prop3;
}
}
public class Foo {
private InnerFoo innerFoo;
public InnerFoo getInnerFoo() {
return innerFoo;
}
public void setInnerFoo(InnerFoo innerFoo) {
this.innerFoo = innerFoo;
}
}
public class FooModel {
private int prop2;
public int getProp2() {
return prop2;
}
public void setProp2(int prop2) {
this.prop2 = prop2;
}
}
问题只出在TimeZone对象上,所以我必须使用转换器,这并不理想。
map().setTimeZone(source.getTimeZone().getID());
public class Foo {
private TimeZone timeZone;
//Setters//Getters
}
public class FooModel {
private String timeZoneId;
//Setters//Getters
}
使用类似构造函数的Autowire
public FooConverter(ModelMapper modelMapper){
this.modelMapper = modelMapper;
}
还要确保ModelMapper bean已在spring配置类中定义,如下所示:
@Bean
public ModelMapper modelMapper() {
return new ModelMapper();
}
我认为这最终是由于在配置映射器时,ModelMapper不能实例化TimeZone
对象(也不能LocalDateTime
等)。
实际上你不需要配置任何东西。
ModelMapper mapper = new ModelMapper();
Foo foo = new Foo();
foo.setTimeZone(TimeZone.getDefault());
FooModel model = mapper.map(foo, FooModel.class);
System.out.println(model.getTimeZoneId()); // "Europe/Berlin" here
这对我有用。ModelMapper发现您想要将TimeZone
的属性ID
映射到foModel
的属性timeZoneId
。
尽管如此,以防您想手动执行此操作:快速跟随文档,我发现了转换器的概念。使用将时区
转换为字符串
的转换器
,可以执行以下操作:
ModelMapper mapper = new ModelMapper();
TypeMap<Foo, FooModel> typeMap = mapper.createTypeMap(Foo.class, FooModel.class);
Converter<TimeZone, String> tzConverter = ctx -> ctx.getSource().getID() + "!!!";
typeMap.addMappings(map -> {
map.using(tzConverter).map(Foo::getTimeZone, FooModel::setTimeZoneId);
});
Foo foo = new Foo();
foo.setTimeZone(TimeZone.getDefault());
FooModel model = mapper.map(foo, FooModel.class);
System.out.println(model.getTimeZoneId()); // "Europe/Berlin!!!" here
对于遇到相同问题的人:我的问题是我使用了带有匿名实现的PropertyMap,就像spring配置中建议的文档一样。这在ExplicitMappingBuilder中搞砸了。我现在有以下资料:
@Configuration
public class ApplicationConfig {
@Bean
public ModelMapper modelMapper() {
ModelMapper modelMapper = new ModelMapper();
modelMapper.addMappings(new UserPropertyMap());
return modelMapper;
}
}
我的UserPropertyMap如下所示:
public class UserPropertyMap extends PropertyMap<UserRepresentation, UserDTO> {
@Override
protected void configure() {
map().setUserName(source.getUsername());
}
}
这在《春靴2》中很有魅力。
我从一个xml模式生成java类,对于一个复杂类型,我希望jaxb使用一个现有的类,我有一个外部绑定定制文件。自定义类被解组为正确的,除了该类型的单个属性,该属性从未在java类中填充。 下面是类型/类问题的演示。 模式中定义的内容是: 读取匹配xml文件的代码段是: 在这个xml中阅读: 使用JAXB生成的Thing类(不使用自定义xjb),输出符合预期: 使用只有getters的自定义Thin
问题内容: 我以下列方式使用ModelMapper: 我有一些转换器类是Spring组件,它们注册了自定义ModelMapper映射 但是当Spring启动时出现以下错误,因为配置函数被调用并且源为null。 那应该如何工作? 我使用错了吗? 像这样声明bean,并且Foo和FooModel不是最终的 重要编辑 实际上,即使当我在主类中的函数中显式创建映射时,我也会得到相同的NullPointer
以下是我的上下文:我使用byteBuddy动态生成一个类,该类根据外部配置将一个对象转换为另一个对象。我遇到了一些问题,我想找到一个替代方案,这就是我发现MapStruct的方式。 因此,我试图构建简单的映射器,我想知道是否有可能自定义注释以添加转换函数。例如,我想要: 在mapper实现中,我会有如下内容: 如果有人能帮我做到这一点,我将不胜感激,这将节省我很多时间。 提前谢谢。
本文向大家介绍C#自定义属性,包括了C#自定义属性的使用技巧和注意事项,需要的朋友参考一下 示例 查找具有自定义属性的属性-MyAttribute 查找给定属性上的所有自定义属性 枚举具有自定义属性的所有类-MyAttribute 在运行时读取自定义属性的值 用法
自定义标签的意义在于方便管理,可以给SIM卡增加一个标签,并且针对某个标签进行统计、查询及管理。支持批量操作。 设置号码自定义标签 支持针对订单、针对iccid进行自定义标签的设置。 号码详情页查询/设置自定义标签 号码详情页,可以查看当前号码已设置的自定义标签,并且可以针对其添加、修改。 自定义标签管理 可以增加、修改、删除自定义属性及其值。
试图向OpenLDAP添加一个新属性,但总是碰壁。我正在尝试向架构添加ipPhone属性,因为我不能在默认的telephoneNumber属性中包含*数字。 下面是我的LDIF文件,用于创建新属性并将其与objectClass类似。 我已经测试和谷歌了几个小时,但一直无法解决这个问题或找出我错过了什么!