java MapStruct为更新Map器生成空的Map器方法体

tkclm6bt  于 2023-04-04  发布在  Java
关注(0)|答案(1)|浏览(173)

我真的搞不懂发生了什么事。
下面是我的openapi生成的类,它将进行更新:

/**
     * Consultation
     */
    
    @Schema(name = "Consultation", description = "Consultation")
    @Generated(value = "org.openapitools.codegen.languages.SpringCodegen", date = "2023-03-31T19:23:20.818876600+02:00[Europe/Brussels]")
    public class Consultation   {
    
      @JsonProperty("publicId")
      private UUID publicId;
    
      @JsonProperty("decision")
      private Decision decision;
    
      @JsonProperty("consultationDate")
      @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
      private LocalDateTime consultationDate;
    
      @JsonProperty("languageCode")
      private String languageCode;
    
      @JsonProperty("remoteIpAddress")
      private String remoteIpAddress;
    
      @JsonProperty("remoteIpAddressCountryCode")
      private String remoteIpAddressCountryCode;
    
      @JsonProperty("createdBy")
      private String createdBy;
    
      @JsonProperty("createdOn")
      @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
      private LocalDateTime createdOn;
    
      @JsonProperty("modifiedBy")
      private String modifiedBy;
    
      @JsonProperty("modifiedOn")
      @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
      private LocalDateTime modifiedOn;
    
      public Consultation publicId(UUID publicId) {
        this.publicId = publicId;
        return this;
      }
    
      /**
       * ID of the consultation
       * @return publicId
      */
      @Valid 
      @Schema(name = "publicId", description = "ID of the consultation", required = false)
      public UUID getPublicId() {
        return publicId;
      }
    
      public void setPublicId(UUID publicId) {
        this.publicId = publicId;
      }
    
      public Consultation decision(Decision decision) {
        this.decision = decision;
        return this;
      }
    
      /**
       * Get decision
       * @return decision
      */
      @Valid 
      @Schema(name = "decision", required = false)
      public Decision getDecision() {
        return decision;
      }
    
      public void setDecision(Decision decision) {
        this.decision = decision;
      }
    
      public Consultation consultationDate(LocalDateTime consultationDate) {
        this.consultationDate = consultationDate;
        return this;
      }
    
      /**
       * date of the consultation
       * @return consultationDate
      */
      @Valid 
      @Schema(name = "consultationDate", description = "date of the consultation", required = false)
      public LocalDateTime getConsultationDate() {
        return consultationDate;
      }
    
      public void setConsultationDate(LocalDateTime consultationDate) {
        this.consultationDate = consultationDate;
      }
    
      public Consultation languageCode(String languageCode) {
        this.languageCode = languageCode;
        return this;
      }
    
      /**
       * language code of the consultation
       * @return languageCode
      */
      
      @Schema(name = "languageCode", description = "language code of the consultation", required = false)
      public String getLanguageCode() {
        return languageCode;
      }
    
      public void setLanguageCode(String languageCode) {
        this.languageCode = languageCode;
      }
    
      public Consultation remoteIpAddress(String remoteIpAddress) {
        this.remoteIpAddress = remoteIpAddress;
        return this;
      }
    
      /**
       * IP address of the user who consulted the decision
       * @return remoteIpAddress
      */
      
      @Schema(name = "remoteIpAddress", description = "IP address of the user who consulted the decision", required = false)
      public String getRemoteIpAddress() {
        return remoteIpAddress;
      }
    
      public void setRemoteIpAddress(String remoteIpAddress) {
        this.remoteIpAddress = remoteIpAddress;
      }
    
      public Consultation remoteIpAddressCountryCode(String remoteIpAddressCountryCode) {
        this.remoteIpAddressCountryCode = remoteIpAddressCountryCode;
        return this;
      }
    
      /**
       * Country code of the IP address of the user who consulted the decision
       * @return remoteIpAddressCountryCode
      */
      
      @Schema(name = "remoteIpAddressCountryCode", description = "Country code of the IP address of the user who consulted the decision", required = false)
      public String getRemoteIpAddressCountryCode() {
        return remoteIpAddressCountryCode;
      }
    
      public void setRemoteIpAddressCountryCode(String remoteIpAddressCountryCode) {
        this.remoteIpAddressCountryCode = remoteIpAddressCountryCode;
      }
    
      public Consultation createdBy(String createdBy) {
        this.createdBy = createdBy;
        return this;
      }
    
      /**
       * User who created the consultation
       * @return createdBy
      */
      
      @Schema(name = "createdBy", description = "User who created the consultation", required = false)
      public String getCreatedBy() {
        return createdBy;
      }
    
      public void setCreatedBy(String createdBy) {
        this.createdBy = createdBy;
      }
    
      public Consultation createdOn(LocalDateTime createdOn) {
        this.createdOn = createdOn;
        return this;
      }
    
      /**
       * Date when the consultation was created
       * @return createdOn
      */
      @Valid 
      @Schema(name = "createdOn", description = "Date when the consultation was created", required = false)
      public LocalDateTime getCreatedOn() {
        return createdOn;
      }
    
      public void setCreatedOn(LocalDateTime createdOn) {
        this.createdOn = createdOn;
      }
    
      public Consultation modifiedBy(String modifiedBy) {
        this.modifiedBy = modifiedBy;
        return this;
      }
    
      /**
       * User who last modified the consultation
       * @return modifiedBy
      */
      
      @Schema(name = "modifiedBy", description = "User who last modified the consultation", required = false)
      public String getModifiedBy() {
        return modifiedBy;
      }
    
      public void setModifiedBy(String modifiedBy) {
        this.modifiedBy = modifiedBy;
      }
    
      public Consultation modifiedOn(LocalDateTime modifiedOn) {
        this.modifiedOn = modifiedOn;
        return this;
      }
    
      /**
       * Date when the consultation was last modified
       * @return modifiedOn
      */
      @Valid 
      @Schema(name = "modifiedOn", description = "Date when the consultation was last modified", required = false)
      public LocalDateTime getModifiedOn() {
        return modifiedOn;
      }
    
      public void setModifiedOn(LocalDateTime modifiedOn) {
        this.modifiedOn = modifiedOn;
      }
    
      @Override
      public boolean equals(Object o) {
        if (this == o) {
          return true;
        }
        if (o == null || getClass() != o.getClass()) {
          return false;
        }
        Consultation consultation = (Consultation) o;
        return Objects.equals(this.publicId, consultation.publicId) &&
            Objects.equals(this.decision, consultation.decision) &&
            Objects.equals(this.consultationDate, consultation.consultationDate) &&
            Objects.equals(this.languageCode, consultation.languageCode) &&
            Objects.equals(this.remoteIpAddress, consultation.remoteIpAddress) &&
            Objects.equals(this.remoteIpAddressCountryCode, consultation.remoteIpAddressCountryCode) &&
            Objects.equals(this.createdBy, consultation.createdBy) &&
            Objects.equals(this.createdOn, consultation.createdOn) &&
            Objects.equals(this.modifiedBy, consultation.modifiedBy) &&
            Objects.equals(this.modifiedOn, consultation.modifiedOn);
      }
    
      @Override
      public int hashCode() {
        return Objects.hash(publicId, decision, consultationDate, languageCode, remoteIpAddress, remoteIpAddressCountryCode, createdBy, createdOn, modifiedBy, modifiedOn);
      }
    
      @Override
      public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append("class Consultation {\n");
        sb.append("    publicId: ").append(toIndentedString(publicId)).append("\n");
        sb.append("    decision: ").append(toIndentedString(decision)).append("\n");
        sb.append("    consultationDate: ").append(toIndentedString(consultationDate)).append("\n");
        sb.append("    languageCode: ").append(toIndentedString(languageCode)).append("\n");
        sb.append("    remoteIpAddress: ").append(toIndentedString(remoteIpAddress)).append("\n");
        sb.append("    remoteIpAddressCountryCode: ").append(toIndentedString(remoteIpAddressCountryCode)).append("\n");
        sb.append("    createdBy: ").append(toIndentedString(createdBy)).append("\n");
        sb.append("    createdOn: ").append(toIndentedString(createdOn)).append("\n");
        sb.append("    modifiedBy: ").append(toIndentedString(modifiedBy)).append("\n");
        sb.append("    modifiedOn: ").append(toIndentedString(modifiedOn)).append("\n");
        sb.append("}");
        return sb.toString();
      }
    
      /**
       * Convert the given object to string with each line indented by 4 spaces
       * (except the first line).
       */
      private String toIndentedString(Object o) {
        if (o == null) {
          return "null";
        }
        return o.toString().replace("\n", "\n    ");
      }
    }

Map器将更新的业务对象:

public class ConsultationBO extends BasicData {
        private final UUID publicId;
    
        private final Decision decision;
    
        private final LocalDateTime consultationDate;
    
        private final String languageCode;
    
        private final String remoteIpAddress;
    
        private final String remoteIpAddressCountryCode;
    
        public ConsultationBO(String createdBy,
                              LocalDateTime createdOn,
                              String modifiedBy,
                              LocalDateTime modifiedOn,
                              UUID publicId,
                              Decision decision,
                              LocalDateTime consultationDate,
                              String languageCode,
                              String remoteIpAddress,
                              String remoteIpAddressCountryCode) {
            super(createdBy, createdOn, modifiedBy, modifiedOn);
            this.publicId = publicId;
            this.decision = decision;
            this.consultationDate = consultationDate;
            this.languageCode = languageCode;
            this.remoteIpAddress = remoteIpAddress;
            this.remoteIpAddressCountryCode = remoteIpAddressCountryCode;
        }
    
        public UUID getPublicId() {
            return publicId;
        }
    
        public Decision getDecision() {
            return decision;
        }
    
        public LocalDateTime getConsultationDate() {
            return consultationDate;
        }
    
        public String getLanguageCode() {
            return languageCode;
        }
    
        public String getRemoteIpAddress() {
            return remoteIpAddress;
        }
    
        public String getRemoteIpAddressCountryCode() {
            return remoteIpAddressCountryCode;
        }
    }

下面是mapper:

@Mapper
    @Component
    public interface ConsultationMapper {
    
        ConsultationBO consultationLogEntityToConsultationBO(DecisionConsultationLogEntity entity);
    
        void updateConsultationLogEntityWithConsultationBO(ConsultationBO businessObject, @MappingTarget DecisionConsultationLogEntity entity);
        DecisionConsultationLogEntity consultationBOToConsultationLogEntity(ConsultationBO businessObject);
    
        Consultation consultationBOToConsultationAPIModel(ConsultationBO businessObject);
    
    // This one isn't being implemented
        void updateConsultationBOWithConsultationAPIModel(Consultation apiModel, @MappingTarget ConsultationBO businessObject);
    
        ConsultationBO consultationAPIModelToConsultationBO(Consultation apiModel);
    }

下面是mapstruct为one before last mapper方法生成的实现:

@Override
        public void updateConsultationBOWithConsultationAPIModel(Consultation apiModel, ConsultationBO businessObject) {
            if ( apiModel == null ) {
                return;
            }
        }

应该有一个完整的Map实现,就像所有其他已经成功生成的方法一样。下面是一个例子:

@Override
        public ConsultationBO consultationAPIModelToConsultationBO(Consultation apiModel) {
            if ( apiModel == null ) {
                return null;
            }
    
            String createdBy = null;
            LocalDateTime createdOn = null;
            String modifiedBy = null;
            LocalDateTime modifiedOn = null;
            UUID publicId = null;
            be.fgov.just.judgment.dvaauthenticsourceapi.domain.Decision decision = null;
            LocalDateTime consultationDate = null;
            String languageCode = null;
            String remoteIpAddress = null;
            String remoteIpAddressCountryCode = null;
    
            createdBy = apiModel.getCreatedBy();
            createdOn = apiModel.getCreatedOn();
            modifiedBy = apiModel.getModifiedBy();
            modifiedOn = apiModel.getModifiedOn();
            publicId = apiModel.getPublicId();
            decision = decisionToDecision1( apiModel.getDecision() );
            consultationDate = apiModel.getConsultationDate();
            languageCode = apiModel.getLanguageCode();
            remoteIpAddress = apiModel.getRemoteIpAddress();
            remoteIpAddressCountryCode = apiModel.getRemoteIpAddressCountryCode();
    
            ConsultationBO consultationBO = new ConsultationBO( createdBy, createdOn, modifiedBy, modifiedOn, publicId, decision, consultationDate, languageCode, remoteIpAddress, remoteIpAddressCountryCode );
    
            return consultationBO;
        }

我以为这是因为源类是由openapi maven generator生成的,但这对任何其他方法都不是问题。然后我想可能是因为生成的类中没有构造函数。但这没有意义,因为这只是源,它不需要在该类上设置任何东西。我的BO类是目标,它确实有一个构造函数,应该由mapstruct自动使用,但实际上没有setter。我问了我的技术主管,他说让类保持不变。
也许我需要指定它应该使用构造函数,但这也没有意义...根据mapstruct文档:
MapStruct支持使用构造函数来Map目标类型。在进行Map时,MapStruct会检查是否存在用于所Map类型的构建器。如果没有构建器,则MapStruct会查找单个可访问的构造函数。
此外,它还可以找到构造函数,并将其用于BO的标准Map中。下面是它使用不可变BO类的构造函数正确实现的另一个方法:

@Override
    public ConsultationBO consultationLogEntityToConsultationBO(DecisionConsultationLogEntity entity) {
        if ( entity == null ) {
            return null;
        }

        String createdBy = null;
        LocalDateTime createdOn = null;
        String modifiedBy = null;
        LocalDateTime modifiedOn = null;
        UUID publicId = null;
        be.fgov.just.judgment.dvaauthenticsourceapi.domain.Decision decision = null;
        LocalDateTime consultationDate = null;
        String languageCode = null;
        String remoteIpAddress = null;
        String remoteIpAddressCountryCode = null;

        createdBy = entity.getCreatedBy();
        createdOn = entity.getCreatedOn();
        modifiedBy = entity.getModifiedBy();
        modifiedOn = entity.getModifiedOn();
        publicId = entity.getPublicId();
        decision = decisionEntityToDecision( entity.getDecision() );
        consultationDate = entity.getConsultationDate();
        languageCode = entity.getLanguageCode();
        remoteIpAddress = entity.getRemoteIpAddress();
        remoteIpAddressCountryCode = entity.getRemoteIpAddressCountryCode();

        ConsultationBO consultationBO = new ConsultationBO( createdBy, createdOn, modifiedBy, modifiedOn, publicId, decision, consultationDate, languageCode, remoteIpAddress, remoteIpAddressCountryCode );

        return consultationBO;
    }
mbjcgjjk

mbjcgjjk1#

这里的问题是,ConsultationBO一旦创建就不能修改(它的所有字段都是final的,并且没有setter),但是您正在使用@MappingTarget来更新它。由于@MappingTarget提供了一个要更新的对象,而不是创建一个新对象,因此没有使用构造函数。并且由于其上的属性都是不可修改的,因此对象将不作更改地返回。
一种解决方案是让MapStruct为您创建对象:

ConsultationBO map(Consultation apiModel);

另一种解决方案是使ConsultationBO可变(将其字段更改为非final并添加setter)。

相关问题