MapStruct 和不可变对象
MapStruct and Immutables
我在使用 mapstruct 和不可变对象时遇到问题。
@Value.Immutable
public abstract class FoobarValue {
public abstract Integer foo();
}
@Value.Immutable
public abstract class TargetFoo {
public abstract Integer foo();
}
@Mapper
public interface ImmutableMapper {
ImmutableMapper INSTANCE = Mappers.getMapper(ImmutableMapper.class);
public TargetFoo toTarget(FoobarValue foobarValue);
}
主要class测试
public class FoobarValueMain {
public static void main(String... args) {
FoobarValue value = ImmutableFoobarValue.builder()
.foo(2)
.build();
ImmutableMapper mapper = ImmutableMapper.INSTANCE;
System.out.println(mapper.toTarget(value).foo());
}
}
我得到的错误是
Exception in thread "main" java.lang.IllegalStateException: Cannot build TargetFoo, some of required attributes are not set [foo]
at org.play.ImmutableTargetFoo$Builder.build(ImmutableTargetFoo.java:158)
at org.play.ImmutableMapperImpl.toTarget(ImmutableMapperImpl.java:21)
at org.play.FoobarValueMain.main(FoobarValueMain.java:12)
我的build.gradle
如下
ext {
mapstructVersion = "1.4.0.Beta2"
immutablesVersion = "2.8.2"
}
dependencies {
annotationProcessor "org.immutables:value:$immutablesVersion" // <--- this is important
annotationProcessor "org.mapstruct:mapstruct-processor:1.4.0.Beta2"
compileOnly "org.immutables:value:$immutablesVersion"
implementation "org.mapstruct:mapstruct:${mapstructVersion}"
testCompile group: 'junit', name: 'junit', version: '4.12'
}
根据 reference 这一切都应该开箱即用。我在这里错过了什么?
之所以不起作用,是因为您没有使用 JavaBean 约定。
您需要在方法前加上 get
前缀
例如
@Value.Immutable
public abstract class TargetFoo {
public abstract Integer getFoo();
}
我在使用 mapstruct 和不可变对象时遇到问题。
@Value.Immutable
public abstract class FoobarValue {
public abstract Integer foo();
}
@Value.Immutable
public abstract class TargetFoo {
public abstract Integer foo();
}
@Mapper
public interface ImmutableMapper {
ImmutableMapper INSTANCE = Mappers.getMapper(ImmutableMapper.class);
public TargetFoo toTarget(FoobarValue foobarValue);
}
主要class测试
public class FoobarValueMain {
public static void main(String... args) {
FoobarValue value = ImmutableFoobarValue.builder()
.foo(2)
.build();
ImmutableMapper mapper = ImmutableMapper.INSTANCE;
System.out.println(mapper.toTarget(value).foo());
}
}
我得到的错误是
Exception in thread "main" java.lang.IllegalStateException: Cannot build TargetFoo, some of required attributes are not set [foo]
at org.play.ImmutableTargetFoo$Builder.build(ImmutableTargetFoo.java:158)
at org.play.ImmutableMapperImpl.toTarget(ImmutableMapperImpl.java:21)
at org.play.FoobarValueMain.main(FoobarValueMain.java:12)
我的build.gradle
如下
ext {
mapstructVersion = "1.4.0.Beta2"
immutablesVersion = "2.8.2"
}
dependencies {
annotationProcessor "org.immutables:value:$immutablesVersion" // <--- this is important
annotationProcessor "org.mapstruct:mapstruct-processor:1.4.0.Beta2"
compileOnly "org.immutables:value:$immutablesVersion"
implementation "org.mapstruct:mapstruct:${mapstructVersion}"
testCompile group: 'junit', name: 'junit', version: '4.12'
}
根据 reference 这一切都应该开箱即用。我在这里错过了什么?
之所以不起作用,是因为您没有使用 JavaBean 约定。
您需要在方法前加上 get
例如
@Value.Immutable
public abstract class TargetFoo {
public abstract Integer getFoo();
}