我有两节课
- SpecContext对象的生成器
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import lombok.Builder;
import lombok.Data;
import lombok.Value;
import javax.annotation.Nullable;
@Data
@Builder
@JsonIgnoreProperties(ignoreUnknown = true)
public class SpecContext {
private Long globalPriceId;
private Long priceId;
private ValidFor validFor;
@JsonCreator
public SpecContext(
@JsonProperty(value = "globalPriceId")
Long globalPriceId,
@JsonProperty(value = "validFor")
ValidFor validFor,
@JsonProperty(value = "priceId")
Long priceId
) {
this.globalPriceId = globalPriceId;
this.validFor = validFor;
this.priceId = priceId;
}
}
- ValidFor对象的生成器
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import utils.DateJsonDeserializer;
import utils.DateJsonSerializer;
import lombok.*;
import java.time.LocalDateTime;
@Data
@Value
@JsonIgnoreProperties(ignoreUnknown = true)
public class ValidFor {
private LocalDateTime startDateTime;
private LocalDateTime endDateTime;
public static final String START_DATE_TIME = "startDateTime";
public static final String END_DATE_TIME = "endDateTime";
@JsonCreator
public ValidFor(
@JsonDeserialize(using = DateJsonDeserializer.class)
@JsonSerialize(using = DateJsonSerializer.class)
@JsonProperty(value = START_DATE_TIME)
LocalDateTime startDateTime,
@JsonDeserialize(using = DateJsonDeserializer.class)
@JsonSerialize(using = DateJsonSerializer.class)
@JsonProperty(value = END_DATE_TIME)
LocalDateTime endDateTime)
{
this.startDateTime = startDateTime;
this.endDateTime = endDateTime;
}
public static ValidForBuilder builder() {
return new ValidForBuilder();
}
public static class ValidForBuilder {
private LocalDateTime startDateTime;
private LocalDateTime endDateTime;
public ValidForBuilder() {
}
public ValidForBuilder startDateTime(LocalDateTime startDateTime) {
this.startDateTime = startDateTime;
return this;
}
public ValidForBuilder endDateTime(LocalDateTime endDateTime) {
this.endDateTime = endDateTime;
return this;
}
public ValidFor build() {
return new ValidFor(startDateTime, endDateTime);
}
}
}
我在自动测试中使用它:
void test() {
SpecContext sp = SpecContext.builder()
.globalPriceId(101928081L)
.priceId(2L)
.validFor(ValidFor.builder().
startDateTime(LocalDateTime.of(2019, 10, 10, 14, 15, 00))
.endDateTime(LocalDateTime.of(2025, 10, 10, 14, 15, 00))
.build())
.build();
在编译过程中,我得到了错误java: incompatible types: java.lang.Long cannot be converted to ValidFor
,我不明白为什么globalPriceId和priceId正在尝试转换ValidFor?
我尝试使用Lombok @Builder进行ValidFor,但得到了相同的错误
如何解决此问题?谢谢你
1条答案
按热度按时间dffbzjpn1#
Lombok
@Builder
要求你有一个包含每个字段的构造函数,按照你的字段列出的顺序。这就是为什么@Builder
会为您生成这个函数,但是如果您想编写自己的构造函数,您也可以这样做。你在这里做了,你的构造函数的argslist * 不匹配 * -你交换了顺序:在构造函数中,顺序为:[Long globalPriceId, ValidFor validFor, Long priceId]
,但在您的字段中顺序是[Long globalPriceId, Long priceId, ValidFor validFor]
。