杰克逊无法反序列化 empy Jhipster 下拉项

Jackson cannot deserialize empy Jhipster dropdown item

我有一个基于 Jhipster 的应用程序和一个实体 Demand:

export interface IDemand {
idDemand?: number;
idSubject?: number;
demandCode?: string;
searchProfileType?: ISearchProfileDomainBean;

有最后一个 属性 的下拉列表,不需要

<select class="form-control" id="field_idProfile" name="profileType" [(ngModel)]="demand.searchProfileType">
  <option [ngValue]="demand.searchProfileType == null"></option>
  <option [ngValue]="profileTypeOption.id === demand.searchProfileType?.id ? demand.searchProfileType : profileTypeOption"
  *ngFor="let profileTypeOption of searchProfileList; trackBy: trackProfileTypeById">{{profileTypeOption.tag}}</option>
</select>

当我收到错误请求时,无法创建新实体或更新提供空项目的现有实体:

.m.m.a.ExceptionHandlerExceptionResolver : Resolved exception caused by handler execution: org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot construct instance of com.domain.SearchProfileDomainBean (although at least one Creator exists): no boolean/Boolean-argument constructor/factory method to deserialize from boolean value (false); nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of com.domain.SearchProfileDomainBean (although at least one Creator exists): no boolean/Boolean-argument constructor/factory method to deserialize from boolean value (false) at [Source: (PushbackInputStream); line: 1, column: 75] (through reference chain: com.domain.Demand["searchProfileType"])

控制台日志显示实体查找 searchProfileType 在反序列化之前未定义(这应该是正确的)。

application-dev.yml 上只有:

jackson:
    serialization:
        indent-output: true

这些是涉及的实体:

package com.onboarding.domain;

import java.io.Serializable;

public class SearchProfileDomainBean implements Serializable {

    private static final long serialVersionUID = 1L;

    private Long id;
    private String tag;

    public SearchProfileDomainBean() {
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getTag() {
        return tag;
    }

    public void setTag(String tag) {
        this.tag = tag;
    }

    @Override
    public int hashCode() {
        return super.hashCode();
    }

    @Override
    public boolean equals(Object obj) {
        return super.equals(obj);
    }

}
package com.onboarding.domain;

import java.io.Serializable;
import java.util.Date;
import java.util.Objects;

public class Demand implements Serializable {

    private Long idDemand;
    private Long idSubject;
    private String demandCode;
    private SearchProfileDomainBean searchProfileType;
    private String description;
    private Boolean flagCustomDemand;
    private String customDemand;
    private Double amount;
    private String currencyCode;
    private String demandState;
    private Date startValidityDate;
    private Date endValidityDate;
    private String insertingUser;
    private String lastUpdateUser;

    public Demand() {
    }

    public Demand(Long idDemand, Long idSubject, String demandCode,
                  SearchProfileDomainBean searchProfileType,
                  String description,
                  Boolean flagCustomDemand, String customDemand, Double amount, String currencyCode,
                  String demandState, Date startValidityDate, Date endValidityDate,
                  String insertingUser, String lastUpdateUser) {
        this.idDemand = idDemand;
        this.idSubject = idSubject;
        this.demandCode = demandCode;
        this.searchProfileType = searchProfileType;
        this.description = description;
        this.flagCustomDemand = flagCustomDemand;
        this.customDemand = customDemand;
        this.amount = amount;
        this.currencyCode = currencyCode;
        this.demandState = demandState;
        this.startValidityDate = startValidityDate;
        this.endValidityDate = endValidityDate;
        this.insertingUser = insertingUser;
        this.lastUpdateUser = lastUpdateUser;
    }

    public Long getIdDemand() {
        return idDemand;
    }

    public void setIdDemand(Long idDemand) {
        this.idDemand = idDemand;
    }

    public Long getIdSubject() {
        return idSubject;
    }

    public void setIdSubject(Long idSubject) {
        this.idSubject = idSubject;
    }

    public String getDemandCode() {
        return demandCode;
    }

    public void setDemandCode(String demandCode) {
        this.demandCode = demandCode;
    }

    public Boolean getFlagCustomDemand() {
        return flagCustomDemand;
    }

    public void setFlagCustomDemand(Boolean flagCustomDemand) {
        this.flagCustomDemand = flagCustomDemand;
    }

    public String getCustomDemand() {
        return customDemand;
    }

    public void setCustomDemand(String customDemand) {
        this.customDemand = customDemand;
    }

    public Double getAmount() {
        return amount;
    }

    public void setAmount(Double amount) {
        this.amount = amount;
    }

    public String getCurrencyCode() {
        return currencyCode;
    }

    public void setCurrencyCode(String currencyCode) {
        this.currencyCode = currencyCode;
    }

    public String getDemandState() {
        return demandState;
    }

    public void setDemandState(String demandState) {
        this.demandState = demandState;
    }

    public Date getStartValidityDate() {
        return startValidityDate;
    }

    public void setStartValidityDate(Date startValidityDate) {
        this.startValidityDate = startValidityDate;
    }

    public Date getEndValidityDate() {
        return endValidityDate;
    }

    public void setEndValidityDate(Date endValidityDate) {
        this.endValidityDate = endValidityDate;
    }

    public String getInsertingUser() {
        return insertingUser;
    }

    public void setInsertingUser(String insertingUser) {
        this.insertingUser = insertingUser;
    }

    public String getLastUpdateUser() {
        return lastUpdateUser;
    }

    public void setLastUpdateUser(String lastUpdateUser) {
        this.lastUpdateUser = lastUpdateUser;
    }

    public SearchProfileDomainBean getSearchProfileType() {
        return searchProfileType;
    }

    public void setSearchProfileType(SearchProfileDomainBean searchProfileType) {
        this.searchProfileType = searchProfileType;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Demand demand = (Demand) o;
        return Objects.equals(idDemand, demand.idDemand) &&
            Objects.equals(idSubject, demand.idSubject) &&
            Objects.equals(demandCode, demand.demandCode);
    }

    @Override
    public int hashCode() {
        return Objects.hash(idDemand, idSubject, demandCode);
    }

}

由于一些项目限制,我修改了原来的Jhipster JPA实体。在调用创建或更新之后以及登陆 DemandResource.java:

之前引发错误
    create(demand: IDemand): Observable<EntityResponseType> {
        const copy = this.convertDateFromClient(demand);
        return this.http
            .post<IDemand>(this.resourceUrl, copy, { observe: 'response' })
            .pipe(map((res: EntityResponseType) => this.convertDateFromServer(res)));
    }

    update(demand: IDemand): Observable<EntityResponseType> {
        const copy = this.convertDateFromClient(demand);
        console.log('#### SERVICE demand.profileType.id = [' + demand.searchProfileType.id + ']');
        console.log('#### SERVICE demand.profileType.tag = [' + demand.searchProfileType.tag + ']');
        return this.http
            .put<IDemand>(this.resourceUrl, copy, { observe: 'response' })
            .pipe(map((res: EntityResponseType) => this.convertDateFromServer(res)));
    }

问题是您正在尝试序列化布尔值而不是对象。这应该有效:

<select class="form-control" id="field_idProfile" name="profileType" [(ngModel)]="demand.searchProfileType">
  <option [ngValue]="null"></option>
  <option [ngValue]="profileTypeOption.id === demand.searchProfileType?.id ? demand.searchProfileType : profileTypeOption"
  *ngFor="let profileTypeOption of searchProfileList; trackBy: trackProfileTypeById">{{profileTypeOption.tag}}</option>
</select>