迁移到 Spring Boot 2.2.0 @JsonIgnore 不起作用

Posted

技术标签:

【中文标题】迁移到 Spring Boot 2.2.0 @JsonIgnore 不起作用【英文标题】:Migrating to Spring Boot 2.2.0 @JsonIgnore does not work 【发布时间】:2020-02-27 02:29:30 【问题描述】:

迁移到 Spring Boot 2.2.0,所有带有 @JsonIgnore 注释的属性现在都在 API Rest Body 中序列化(而在 Boot 2.1.9 中工作正常)。 此外,在每个嵌套对象之前添加了一个"_embedded" 属性。

这似乎是杰克逊的问题。

代码详情:

@Entity
@Audited
@Data
@EqualsAndHashCode(callSuper = true, of =  "makeId", "modelId", "versionId", "bodyTypeId", "modelYear" )
@ToString(callSuper = true, of =  "makeName", "modelName", "versionName", "bodyTypeId", "modelYear" )
@NoArgsConstructor
@RequiredArgsConstructor
@Table(schema = "fleet", name = "vehicles", uniqueConstraints =  @UniqueConstraint(columnNames =  "id", "make_id", "model_id", "version_id", "body_type_id", "model_year", "port_number" ) )
public class Vehicle extends BaseEntity implements VehicleBaseMakeInfo 

    @OneToMany(mappedBy = "vehicleRef", cascade = CascadeType.DETACH, orphanRemoval = false, fetch = FetchType.LAZY)
    @JsonIgnore
    private Set<Car> cars = new HashSet<>();

    @Length(max = 250)
    @Column(name = "make_id", length = 250)
    private String makeId; // UK_1

    @Length(max = 250)
    @Column(name = "make_name", length = 250)
    @NonNull
    @NotBlank
    private String makeName;

    @Length(max = 250)
    @Column(name = "model_id", length = 250)
    private String modelId; 

...

一旦我执行了 GET 请求“http://localhost:8081/api/vehicles”

我收到


        "id": 105,
        "state": "ACTIVE",
        "enabled": true,
        "avatarDocumentId": null,
        "makeId": "Bmw",
        "makeName": "Bmw",
        "modelId": "X4",
        "modelName": "X4",
        "modelNameOriginal": null,
        "versionId": "Executive",
        "versionName": "Executive",
        "bodyTypeId": "Wagon",
        "engineCapacity": 2.0,
        "fuelingId": "Unleaded",
        "power": 184.0,
        "co2": 2,
        "detailedTransmissionId": "Manual",
        "tractionName": "4x4",
        "portNumber": 3,
        "urbanCycle": 4.3,
        "extraUrbanCycle": 3.8,
        "mixedCycle": 4.0,
        "price": 27000.0,
        "annualFringeBenefit": null,
        "fringeCustomValue": false,
        "modelYear": 2016,
        "sourceType": "CUSTOM",
        "inProduction": true,
        "fringeAlgMatchingState": null,
        "fringeMatchingCorrection": null,
        "requiredReviewType": null,
        "fringeSimilarVehicleId": null,
        "useType": "PASSENGER_TRANSPORT",
        "correctToCreateAsNew": false,
        "productionStartDate": null,
        "productionEndDate": null,
        "calculatedMonthFringeBenefit": null,
        "fullVersionExtended": "Bmw X4 Executive Wagon 3p 4x4 Unleaded 2016",
        "calculatedAnnualFringeBenefit": null,
        "objectId": "105",
        "_embedded": 
          "cars": [
            
              "id": 258,
              "state": "INSTALLED",
              "enabled": true,
              "avatarDocumentId": null,
              "contractVersions": [
                
                  "id": 308,
                  "state": "ACTIVE",

如您所见,“_embedded”和“cars”不应出现。 在升级到 Boot 2.2.0 之前它运行良好

谢谢

安东尼奥

【问题讨论】:

能不能用完整的输入输出例子来展示代码 在帖子中添加了详细信息,谢谢 你能把它添加到 application.properties 文件并检查吗,spring.hateoas.use-hal-as-default-json-media-type=false Ref: ***.com/q/28808220/2987755 我试过了,但仍然有这两个问题 【参考方案1】:

升级到Spring Boot 2.2.1问题解决

【讨论】:

以上是关于迁移到 Spring Boot 2.2.0 @JsonIgnore 不起作用的主要内容,如果未能解决你的问题,请参考以下文章

多模块 Gradle 项目 - 从 Spring-Boot 1.5 迁移到 2.1

迁移到 Spring Boot 2 并使用 Spring Batch 4

从 Spring Boot 安全迁移到 keycloak

现有前面迁移到Spring-boot

将 Waffle Spring Security XML 配置迁移到 Spring Boot

Spring Boot - MySQL 到 Postgres 的迁移