迁移到Spring Boot 2.2.0后,@JsonIgnore无法使用。

3
迁移到Spring Boot 2.2.0后,所有使用@JsonIgnore注解的属性都会在API Rest Body中进行序列化(而在Boot 2.1.9中可以正常工作)。 此外,在每个嵌套对象之前添加了一个"_embedded"属性。 这似乎是Jackson的问题。 代码细节如下:
@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之前,这个问题是不存在的。

谢谢

Antonio


1
你能展示完整的输入输出示例代码吗? - Ryuzaki L
在帖子中添加了细节,谢谢。 - Antonio Vivalda
你能否将 spring.hateoas.use-hal-as-default-json-media-type=false 添加到 application.properties 文件中并检查一下?参考链接:https://dev59.com/A14b5IYBdhLWcg3wrzfZ - dkb
我已经尝试过了,但它仍然继续存在这两个问题。 - Antonio Vivalda
1个回答

0

升级到Spring Boot 2.2.1,问题得到解决。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接