NyeSass
NyeSass

Reputation: 393

ModelMapper Configuration error (failed to configure Mappings)

The problem i have right now is, that my configure method is throwing an exception saying that there is a configuration error, but i dont know what the problem is.

Here is my configure method:

public class ModelMapperEmb {

    public CasinoDto modelToDto(Casino casino){
        ModelMapper modelMapper = new ModelMapper();
        modelMapper.addMappings(new PropertyMap<Casino, CasinoDto>() {
            @Override
            protected void configure() {
                map(source.getAddress().getCountry(), destination.getCountry());
                map(source.getAddress().getHouseNumber(), destination.getHouseNumber());
                map(source.getAddress().getPlace(), destination.getPlace());
                map(source.getAddress().getStreet(), destination.getStreet());
            }
        });
        modelMapper.getConfiguration().setMatchingStrategy(MatchingStrategies.LOOSE);
        return modelMapper.map(casino, CasinoDto.class);
    }
}

I did this method because the modelmapper cant map an embedded resource so all the attributes that are in the embedable class are configured above.

Exception:


1) Failed to configure mappings

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 at.privas.casino.service.ModelMapperEmb.modelToDto(ModelMapperEmb.java:13)
    at at.privas.casino.persistence.ModelMapperTest.verifyCasinoToDtoMappingCasino(ModelMapperTest.java:29)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:567)
    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.base/java.util.ArrayList.forEach(ArrayList.java:1507)
    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.base/java.util.ArrayList.forEach(ArrayList.java:1507)
    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:69)
    at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
    at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:230)
    at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:58)
Caused by: java.lang.IllegalArgumentException: Unsupported class file major version 57
    at org.modelmapper.internal.asm.ClassReader.<init>(ClassReader.java:176)
    at org.modelmapper.internal.asm.ClassReader.<init>(ClassReader.java:158)
    at org.modelmapper.internal.asm.ClassReader.<init>(ClassReader.java:146)
    at org.modelmapper.internal.asm.ClassReader.<init>(ClassReader.java:273)
    at org.modelmapper.internal.ExplicitMappingBuilder.visitPropertyMap(ExplicitMappingBuilder.java:255)
    at org.modelmapper.PropertyMap.configure(PropertyMap.java:386)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:567)
    at org.modelmapper.internal.ExplicitMappingBuilder.build(ExplicitMappingBuilder.java:227)
    ... 69 more

Here is the Code of CasinoDTO:

@Entity
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor(access = AccessLevel.PRIVATE)
@Builder
@Getter
@Setter
public class Casino extends AbstractPersistable<Long> {

    @Embedded
    private Address address;
    private Float sales;
    @OneToMany(mappedBy = "casino", cascade = CascadeType.PERSIST, fetch = FetchType.LAZY)
    private List<Slot> slots = new ArrayList<Slot>();

    public void addAll(ArrayList<Slot> slots){
        slots.stream()
                .forEach(this::addSlot);
    }

    public void addSlot(Slot slot) {
        slots.add(slot);
        if(!this.equals(slot.getCasino())){
            slot.setCasino(this);
        }
    }

    public List<Slot> getSlots(){
        return Collections.unmodifiableList(slots);
    }

    public boolean containsSlot(Slot slot) {
        return slots.contains(slot);
    }
}

Code of Slot:

@Entity
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor(access = AccessLevel.PRIVATE)
@Builder
@Getter
@Setter
public class Slot extends AbstractPersistable<Long> {

    private String name;
    private Float maxBet;
    private Float jackpot;
    @ManyToOne(cascade = CascadeType.PERSIST, fetch = FetchType.LAZY)
    @JsonIgnore
    private Casino casino;


    public void setCasino(Casino casino){
        this.casino = casino;
        if(!casino.containsSlot(this)){
            casino.addSlot(this);
        }
    }
}

Here is the Code of Address:

@Embeddable
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor(access = AccessLevel.PRIVATE)
@Builder
@Getter
@Setter
public class Address {
    private String country;
    private String place;
    private String street;
    private String houseNumber;
}

Code of CasinoDto:

@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode
@Getter
@Setter
public class CasinoDto extends RepresentationModel<CasinoDto> {

    private Long id;

    private Float sales;

    private String country;
    private String place;
    private String street;
    private String houseNumber;
}

And here is also the test where the error occurs:

private ModelMapperEmb modelMapper = new ModelMapperEmb();

    @Test
    void verifyCasinoToDtoMappingCasino(){
        Casino casino = Casino.builder()
                .address(Address.builder()
                        .country("österreich")
                        .houseNumber("12")
                        .place("Breitenfurt")
                        .street("Meisenweg")
                        .build())
                .sales(1000f).build();
        CasinoDto casinoDto = modelMapper.modelToDto(casino);
        Assertions.assertThat(casinoDto).isEqualToComparingOnlyGivenFields(casino, "country", "houseNumber", "place", "street", "sales");
    }

Upvotes: 2

Views: 9918

Answers (1)

MartinBG
MartinBG

Reputation: 1648

Caused by: java.lang.IllegalArgumentException: Unsupported class file major version 57

This exception is thrown by a library that is not compatible with JDK 13.

ModelMapper 2.3.0 does not work on Java 13 - the first version that supports it is 2.3.6

Updating modelMapper to 2.3.7 solves the exception and CasinoDto modelToDto(Casino casino) properly maps Casino to CasinoDto instances.

There is a problem in your test method assertion - it fails, but if you debug the method you'll find that the mapping is correct:

Assertions.assertThat(casinoDto).isEqualToComparingOnlyGivenFields(casino, "country", "houseNumber", "place", "street", "sales");

Change assertions as follows to verify it:

Assertions.assertThat(casinoDto).isEqualToComparingOnlyGivenFields(casino.getAddress(), "country", "houseNumber", "place", "street");
Assertions.assertThat(casinoDto).isEqualToComparingOnlyGivenFields(casino, "sales");

Upvotes: 4

Related Questions