java: 不兼容的类型: java.lang.Long 无法转换为 ValidFor

huangapple go评论72阅读模式
英文:

java: incompatible types: java.lang.Long cannot be converted to ValidFor

问题

I have two classes

  1. Builder for SpecContext object
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;
import java.time.LocalDateTime;

@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;
    }
}
  1. Builder for ValidFor object
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);
        }
    }
}

I use it in an autotest:

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();

During compilation, I got the error

java: incompatible types: java.lang.Long cannot be converted to ValidFor

I don't understand why globalPriceId and priceId are being tried to convert to ValidFor?

I tried to use Lombok @Builder for ValidFor but got the same error.

How can I fix this problem?
Thank you.

英文:

I have two classes

  1. Builder for SpecContext object
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;
}
}
  1. Builder for ValidFor object
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);
}
}
}

I use it in an autotest:

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();

During complilation I got the error

java: incompatible types: java.lang.Long cannot be converted to ValidFor

I don't understand why globalPriceId and priceId are being tried to convert in ValidFor?

I tried to use Lombok @Builder for ValidFor but got the same error

How can I fix this problem?
Thank you

答案1

得分: 2

Lombok的@Builder要求你拥有一个构造函数,其中包含每个字段,按照字段列出的顺序。这就是为什么@Builder会为你生成这个,但如果你想编写自己的构造函数,你也可以这样做。在这里,你这样做了,但你的构造函数参数列表 不匹配 - 你交换了顺序:在你的构造函数中,顺序是:[Long globalPriceId, ValidFor validFor, Long priceId],但在你的字段中,顺序是[Long globalPriceId, Long priceId, ValidFor validFor]

英文:

Lombok @Builder requires that you have a constructor that contains each and every field, in the same order your fields are listed. This is why @Builder will generate this for you, but if you want to write your own constructor, you can do that too. You did here, and your constructor's argslist does not match up - you have swapped orders: In your constructor, the order is: [Long globalPriceId, ValidFor validFor, Long priceId], but in your fields the order is [Long globalPriceId, Long priceId, ValidFor validFor].

huangapple
  • 本文由 发表于 2023年5月11日 18:37:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/76226712.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定