Pomijanie getterów jeśli wartość jest nullem

0

Czy znacie jakiś przystojny sposób aby pobierać tylko te getter z klasy które nie są nullem?

Mam taki np przykładowy response:


{
totalIncome:0,
totalQty:null,
trans:null,
withCommentsIncome:null,
withCommentsQty:null,
withNoCommentsIncome:null,
withNoCommentsQty:null
}

Chciałbym aby to było robione dynamicznie i pobierało tylko te pola które nie są null.

Klasa:

package pl.bpo.archiwumallegro.model.allesuma;
public class SummaryField {

    Long income;
    Long totalQty;
    Long totalIncome;
    Long grossIncome;
    Long soldItems;
    Long trans;
    Long auctions;
    Long items;
    Long auctionsByTrans;
    Long withCommentsIncome;
    Long withNoCommentsIncome;
    Long notSalesIncome;
    Long withCommentsQty;
    Long withNoCommentsQty;
    Long notSalesQty;

    public Long getIncome() {
        return income;
    }

    public void setIncome(Long income) {
        this.income = income;
    }

    public Long getTotalQty() {
        return totalQty;
    }

    public void setTotalQty(Long totalQty) {
        this.totalQty = totalQty;
    }

    public Long getTotalIncome() {
        return totalIncome;
    }

    public void setTotalIncome(Long totalIncome) {
        this.totalIncome = totalIncome;
    }

    public Long getGrossIncome() {
        return grossIncome;
    }

    public void setGrossIncome(Long grossIncome) {
        this.grossIncome = grossIncome;
    }

    public Long getSoldItems() {
        return soldItems;
    }

    public void setSoldItems(Long soldItems) {
        this.soldItems = soldItems;
    }

    public Long getTrans() {
        return trans;
    }

    public void setTrans(Long trans) {
        this.trans = trans;
    }

    public Long getAuctions() {
        return auctions;
    }

    public void setAuctions(Long auctions) {
        this.auctions = auctions;
    }

    public Long getItems() {
        return items;
    }

    public void setItems(Long items) {
        this.items = items;
    }

    public Long getAuctionsByTrans() {
        return auctionsByTrans;
    }

    public void setAuctionsByTrans(Long auctionsByTrans) {
        this.auctionsByTrans = auctionsByTrans;
    }

    public Long getWithCommentsIncome() {
        return withCommentsIncome;
    }

    public void setWithCommentsIncome(Long withCommentsIncome) {
        this.withCommentsIncome = withCommentsIncome;
    }

    public Long getWithNoCommentsIncome() {
        return withNoCommentsIncome;
    }

    public void setWithNoCommentsIncome(Long withNoCommentsIncome) {
        this.withNoCommentsIncome = withNoCommentsIncome;
    }

    public Long getNotSalesIncome() {
        return notSalesIncome;
    }

    public void setNotSalesIncome(Long notSalesIncome) {
        this.notSalesIncome = notSalesIncome;
    }

    public Long getWithCommentsQty() {
        return withCommentsQty;
    }

    public void setWithCommentsQty(Long withCommentsQty) {
        this.withCommentsQty = withCommentsQty;
    }

    public Long getWithNoCommentsQty() {
        return withNoCommentsQty;
    }

    public void setWithNoCommentsQty(Long withNoCommentsQty) {
        this.withNoCommentsQty = withNoCommentsQty;
    }

    public Long getNotSalesQty() {
        return notSalesQty;
    }

    public void setNotSalesQty(Long notSalesQty) {
        this.notSalesQty = notSalesQty;
    }
}

0

Ale dlaczego tak? Masz juz w tym obiekcie wartosci i chcesz nadpisac je tymi z response (ale tylko nie nullowe) ?

0

Mam takie warunki w kontrolerze:

SummaryField allesumaField = new SummaryField();
            allesumaField.setTotalIncome(zero);
if (searchParams.getAllTransactions() != null && searchParams.getAllTransactions() == true) {
            }

            if (searchParams.getWithComments() != null && searchParams.getWithComments() == true) {
                allesumaField.setWithCommentsIncome(zero);
            }

            if (searchParams.getWithNoComments() != null && searchParams.getWithNoComments() == true) {
                allesumaField.setWithNoCommentsIncome(zero);
            }

            if (searchParams.getNotSales() != null && searchParams.getNotSales() == true) {
                allesumaField.setNotSalesIncome(zero);
            }

allesumaaTableSummary.setRows(tsStart, allesumaField);

Mam takie warunki i chce pola uzupelniac jedynie jeśli parametry są ustawione jezeli jest null czyli nie ustawiony to w ogóle nie chce go w modelu.

0

Ale wiesz, ze ten Long z modelu jest automatycznie nullem? :P

1

Ok, chyba na starcie nie zrozumialem.

masz taka odpowiedz:

{
totalIncome:0,
totalQty:null,
trans:null,
withCommentsIncome:null,
withCommentsQty:null,
withNoCommentsIncome:null,
withNoCommentsQty:null
}

a chcialbys taka:

{
totalIncome:0
}

Dobrze rozumiem? Jesli tak to czym serializujesz obiekt tej klasy do JSONa? Jacksonem? Jesli tak to szukaj w jego opcjach zeby pomijal nullowe pola.

http://stackoverflow.com/questions/11757487/how-to-tell-jackson-to-ignore-a-field-during-serialization-if-its-value-is-null

0

O to właśnie mi chodziło dzięki.

1 użytkowników online, w tym zalogowanych: 0, gości: 1