You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在SpringBoot中实现客户访问业务及忽略JSON序列化字段?

JSON序列化字段忽略方案

在JSON序列化/反序列化过程中需忽略特定字段或属性时,可使用Java生态常用的JSON处理库Jackson提供的注解。若要忽略PokemonSprites类中的other字段,直接在类级别添加@JsonIgnoreProperties注解即可。举个实际场景的例子:在findByProductProductId方法中,我们指定属性路径product.productId,表示要基于关联的Product实体的productId进行查询。

SpringBoot客户访问业务实体实现

Visit实体类

package fact.it.examcustomercheck.entities;

import java.util.Calendar;

public class Visit {
    private int enterWeight;
    private int exitWeight;

    private Customer customer;

    public int getEnterWeight() {
        return enterWeight;
    }

    public void setEnterWeight(int enterWeight) {
        this.enterWeight = enterWeight;
    }

    public int getExitWeight() {
        return exitWeight;
    }

    public void setExitWeight(int exitWeight) {
        this.exitWeight = exitWeight;
    }

    public Customer getCustomer() {
        return customer;
    }

    public void setCustomer(Customer customer) {
        this.customer = customer;
    }

    public int getDepositedWeight() {
        return this.enterWeight - this.exitWeight;
    }

    public int getMembershipAge() {
        int year = Calendar.getInstance().get(Calendar.YEAR);
        return year - this.customer.getYearOfRegistration();
    }

    public int getCost() {
        if (getDepositedWeight() <= 7 && getMembershipAge() > 5) {
            return 0;
        } else if (getDepositedWeight() <= 50) {
            return getDepositedWeight() * 2;
        } else {
            return getDepositedWeight() * 3;
        }
    }

    @Override
    public String toString() {
        return this.customer.getName() + " registered in " + this.customer.getYearOfRegistration() +
                " has deposited " + getDepositedWeight() + "kg. of waste: Cost = " + getCost() + " euro.";
    }
}

Customer实体类

package fact.it.examcustomercheck.entities;

public class Customer {
    private String name;
    private boolean citizen;
    private int yearOfRegistration;

    public Customer() {

    }

    public Customer(String name) {
        this.name = name;
    }

    public Customer(String name, boolean citizen, int yearOfRegistration) {
        this.name = name;
        this.citizen = citizen;
        this.yearOfRegistration = yearOfRegistration;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public boolean isCitizen() {
        return citizen;
    }

    public void setCitizen(boolean citizen) {
        this.citizen = citizen;
    }

    public int getYearOfRegistration() {
        return yearOfRegistration;
    }

    public void setYearOfRegistration(int yearOfRegistration) {
        this.yearOfRegistration = yearOfRegistration;
    }

    @Override
    public String toString() {
        if (citizen) {
            return this.name + " (citizen) registered in " + this.yearOfRegistration;
        } else {
            return this.name + " registered in " + this.yearOfRegistration;
        }
    }
}

SpringBoot启动类

@SpringBootApplication
public class ExamCustomerCheckApplication {
    public static void main(String[] args) {
        SpringApplication.run(ExamCustomerCheckApplication.class, args);
    }
}

内容的提问来源于stack exchange,提问作者Salman Wattoo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.19 07:03:19