簡體   English   中英

Spring @GetMapping 不使用 bean 中的每個 getter?

[英]Spring @GetMapping without using every getter in the bean?

我有一個帶有重載構造函數的 bean ProfileStandardSection 我對 bean 中的每個 object 都有一個吸氣劑,但並非所有構造函數都使用每個 object。 當我在 controller 中使用 bean 時,不打算使用的對象仍顯示在 JSON object 中。

我可以擁有它...不要那樣做嗎?

我的豆子:

public class ProfileStandardSection {

    private List<ProfileStandardRow> summandRows;
    private List<ProfileStandardRow> topFive;
    private final ProfileStandardRow totalRow;
    private ProfileStandardRow pctOfSuperTotal;

    public ProfileStandardSection(List<ProfileStandardRow> summandRows, ProfileStandardRow totalRow, ProfileStandardRow pctOfSuperTotal) {
        this.summandRows = summandRows;
        this.totalRow = totalRow;
        this.pctOfSuperTotal = pctOfSuperTotal;
    }

    public ProfileStandardSection(List<ProfileStandardRow> topFive, ProfileStandardRow totalRow) {
        this.topFive = topFive;
        this.totalRow = totalRow;
    }

    public List<ProfileStandardRow> getSummandRows() {
        return summandRows;
    }
    
    public List<ProfileStandardRow> getTopFive() {
        return topFive;
    }

    public ProfileStandardRow getTotalRow() {
        return totalRow;
    }

    public ProfileStandardRow getPctOfSuperTotal() {
        return pctOfSuperTotal;
    }
}

我的 controller 調用 bean:

@CrossOrigin(origins = {"${api-setting.cors-allowed-origin}"})
@RestController
public class ProfileController {
    @Autowired
    private ProfileRepository profileRepository;
    
    @GetMapping("/liabilities/{company}/{year}")
    public ProfileStandardSection getLiabilitiesSection(@PathVariable int company, @PathVariable int year) {
        Optional<ProfileStandardRow> totalRow = profileRepository.findById(
                new RowId(company, year, RowInfo.TOTAL_LIABILITIES.getRowkey()));

        List<ProfileStandardRow> topFive = profileRepository.findAllById(Arrays.asList(
                new RowId(company, year, RowInfo.FIRST_HIGHEST_LIABILITY.getRowkey()),
                new RowId(company, year, RowInfo.SECOND_HIGHEST_LIABILITY.getRowkey()),
                new RowId(company, year, RowInfo.THIRD_HIGHEST_LIABILITY.getRowkey()),
                new RowId(company, year, RowInfo.FOURTH_HIGHEST_LIABILITY.getRowkey()),
                new RowId(company, year, RowInfo.FIFTH_HIGHEST_LIABILITY.getRowkey())));

        return new ProfileStandardSection(topFive, totalRow.get());
    }
}

JSON object 我回來了:

{
"summandRows": null,
"topFive": [...stuff I want...],
"totalRow": {...stuff I want...},
"pctOfSuperTotal": null
}

有沒有辦法配置它,使其不包括 JSON 中的summandRowspctOfSuperTotal對象?

我知道它包括了它們,因為 bean 中有用於它們的吸氣劑,它們剛剛被使用,因為 Spring 東西,而 IntelliJ 甚至沒有意識到這一點。 所以它將它們標記為未使用。 那么有沒有辦法告訴 Spring 使用哪些吸氣劑?

嘗試使用@jsonIgnore 示例

public class ProfileStandardSection { 
 @JsonIgnore  
private final ProfileStandardRow totalRow;
// rest of the property same
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM