我在使用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);
}要测试的主类
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的说法,这应该都是开箱即用的。这里我漏掉了什么?
发布于 2020-07-11 16:06:53
它不工作的原因是因为您没有使用JavaBean约定。
您需要在方法前面加上get前缀
例如:
@Value.Immutable
public abstract class TargetFoo {
public abstract Integer getFoo();
}https://stackoverflow.com/questions/62807301
复制相似问题