ModelMapper从Map转实体,数据类型转换出错failed to convert java.lang.String to java.lang.Long

org.modelmapper.MappingException: ModelMapper mapping errors:

1) Converter org.modelmapper.internal.converter.NumberConverter@1c93b51e failed to convert java.lang.String to java.lang.Long.

1 error

    at org.modelmapper.internal.Errors.throwMappingExceptionIfErrorsExist(Errors.java:374)
    at org.modelmapper.internal.MappingEngineImpl.map(MappingEngineImpl.java:69)
    at org.modelmapper.ModelMapper.mapInternal(ModelMapper.java:497)
    at org.modelmapper.ModelMapper.map(ModelMapper.java:340)
    at com.springboot.RedisTest.testMapToPojo(RedisTest.java:61)
    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.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:675)
    at org.junit.jupiter.engine.execution.MethodInvocation.proceed(MethodInvocation.java:60)
    at org.junit.jupiter.engine.execution.InvocationInterceptorChain$ValidatingInvocation.proceed(InvocationInterceptorChain.java:125)
    at org.junit.jupiter.engine.extension.TimeoutExtension.intercept(TimeoutExtension.java:132)
    at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestableMethod(TimeoutExtension.java:124)
    at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestMethod(TimeoutExtension.java:74)
    at org.junit.jupiter.engine.execution.ExecutableInvoker$ReflectiveInterceptorCall.lambda$ofVoidMethod$0(ExecutableInvoker.java:115)
    at org.junit.jupiter.engine.execution.ExecutableInvoker.lambda$invoke$0(ExecutableInvoker.java:105)
    at org.junit.jupiter.engine.execution.InvocationInterceptorChain$InterceptedInvocation.proceed(InvocationInterceptorChain.java:104)
    at org.junit.jupiter.engine.execution.InvocationInterceptorChain.proceed(InvocationInterceptorChain.java:62)
    at org.junit.jupiter.engine.execution.InvocationInterceptorChain.chainAndInvoke(InvocationInterceptorChain.java:43)
    at org.junit.jupiter.engine.execution.InvocationInterceptorChain.invoke(InvocationInterceptorChain.java:35)
    at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:104)
    at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:98)
    at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeTestMethod$6(TestMethodTestDescriptor.java:202)
    at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
    at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeTestMethod(TestMethodTestDescriptor.java:198)
    at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:135)
    at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:69)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:135)
    at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:125)
    at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:135)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:123)
    at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:122)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:80)
    at java.util.ArrayList.forEach(ArrayList.java:1257)
    at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:139)
    at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:125)
    at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:135)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:123)
    at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:122)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:80)
    at java.util.ArrayList.forEach(ArrayList.java:1257)
    at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:139)
    at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:125)
    at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:135)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:123)
    at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:122)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:80)
    at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.submit(SameThreadHierarchicalTestExecutorService.java:32)
    at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:57)
    at org.junit.platform.engine.support.hierarchical.HierarchicalTestEngine.execute(HierarchicalTestEngine.java:51)
    at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:229)
    at org.junit.platform.launcher.core.DefaultLauncher.lambda$execute$6(DefaultLauncher.java:197)
    at org.junit.platform.launcher.core.DefaultLauncher.withInterceptedStreams(DefaultLauncher.java:211)
    at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:191)
    at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:128)
    at com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(JUnit5IdeaTestRunner.java:74)
    at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: org.modelmapper.MappingException: ModelMapper mapping errors:

1) Error mapping c350300_1 to java.lang.Long

1 error
    at org.modelmapper.internal.Errors.toMappingException(Errors.java:258)
    at org.modelmapper.internal.converter.NumberConverter.numberFor(NumberConverter.java:176)
    at org.modelmapper.internal.converter.NumberConverter.convert(NumberConverter.java:71)
    at org.modelmapper.internal.converter.NumberConverter.convert(NumberConverter.java:55)
    at org.modelmapper.internal.MappingEngineImpl.convert(MappingEngineImpl.java:337)
    at org.modelmapper.internal.MappingEngineImpl.map(MappingEngineImpl.java:96)
    at org.modelmapper.internal.MappingEngineImpl.setDestinationValue(MappingEngineImpl.java:247)
    at org.modelmapper.internal.MappingEngineImpl.propertyMap(MappingEngineImpl.java:181)
    at org.modelmapper.internal.MappingEngineImpl.typeMap(MappingEngineImpl.java:132)
    at org.modelmapper.internal.MappingEngineImpl.map(MappingEngineImpl.java:101)
    at org.modelmapper.internal.MappingEngineImpl.map(MappingEngineImpl.java:60)
    ... 66 more
Caused by: java.lang.NumberFormatException: For input string: "c350300_1"
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
    at java.lang.Long.parseLong(Long.java:589)
    at java.lang.Long.valueOf(Long.java:803)
    at org.modelmapper.internal.converter.NumberConverter.numberFor(NumberConverter.java:166)
    ... 75 more

 

以上异常很容易看懂,从c350300_1 to java.lang.Long转Long失败,

相关java类如下

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.base.Objects;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder

public class SnapShot {

    @JsonIgnore

    private Long id;

    @JsonIgnore
    private Integer distId;

    @JsonProperty("name")
    @JsonInclude(JsonInclude.Include.NON_EMPTY)
    private String distName;

    private Integer confirm;
    @JsonIgnore
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private Integer suspect;

    private Integer dead;

    private Integer heal;

    private Float weight;


    @JsonInclude(JsonInclude.Include.NON_EMPTY)
    private Integer parentId;

    @JsonIgnore

    private String level;

    @JsonInclude(JsonInclude.Include.NON_EMPTY)
    private String mapId;


    private String updateTime;

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        SnapShot snapShot = (SnapShot) o;
        return Objects.equal(distId, snapShot.distId) &&
                Objects.equal(confirm, snapShot.confirm) &&
                Objects.equal(suspect, snapShot.suspect) &&
                Objects.equal(dead, snapShot.dead) &&
                Objects.equal(heal, snapShot.heal)&&
                Objects.equal(weight,snapShot.weight)&&
                Objects.equal(level, snapShot.level)&&
                Objects.equal(mapId,snapShot.mapId);
    }

    @Override
    public int hashCode() {
        return 0;
    }
}

实体里的mapId属性是String类型的,根据笔者猜测ModelMapper可能根据属性后缀进行联想,企图把它转化整数类型(long),本地maven问题,没下载到源码,没法深入追踪代码。

 

最终在网上找到解决办法

https://github.com/modelmapper/modelmapper/issues/312

ModelMapper modelMapper = new ModelMapper();
modelMapper.getConfiguration().setMatchingStrategy(MatchingStrategies.STRICT);

map

ModelMapper从Map转实体,数据类型转换出错failed to convert java.lang.String to java.lang.Long

 

 

转换成功后:

ModelMapper从Map转实体,数据类型转换出错failed to convert java.lang.String to java.lang.Long

 

ModelMapper从Map转实体,数据类型转换出错failed to convert java.lang.String to java.lang.Long

上一篇:互联网、云计算、物联网、智慧家庭、移动互联网的区别与联系


下一篇:AndroidUI组件之TextSwitcher