带有Swagger @ApiResponse的JsonView类

问题描述 投票:0回答:1

我有一个名为@JsonViewBankAccountView.Public,它帮助我限制BankAccount中的一些字段,因为我不想在公共get操作中发送所有属性。我的问题是当我尝试使用swagger指定它时,因为如果我指定BankAccount.class它显示整个对象而不是我的@JsonView中指定的所有字段,但是如果我指定BankAccount.Public.class它会向我显示一个空对象。你能否告诉我,Swagger是否可能只显示公共领域?

这是我的代码:

// BankAccount Json View
public class BankAccountView {
    public static class Public {}
}
// BankAccount class
@ApiModel("BankAccount")
public class BankAccount {

    @ApiModelProperty
    @JsonView(BankAccountView.Public.class)
    private Long accountId;

    @ApiModelProperty
    private Long owner;

    @ApiModelProperty
    @NotBlank
    @JsonView(BankAccountView.Public.class)
    private String currency;

    @ApiModelProperty
    @NotBlank
    @JsonView(BankAccountView.Public.class)
    private String bankName;

    @ApiModelProperty
    @JsonView(BankAccountView.Public.class)
    private BankAccountType accountType;

    @ApiModelProperty
    @JsonView(BankAccountView.Public.class)
    private BankAccountStatus status;

    @ApiModelProperty
    private Instant verificationDate;

    @ApiModelProperty
    @JsonView(BankAccountView.Public.class)
    private String mask;
}
// BankAccountController class

    @ApiOperation(value = "Fetch a list of all bank accounts")
    @JsonView({BankAccountView.Public.class})
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "Bank accounts successfully retrieved", response = BankAccountView.Public.class, responseContainer = "List"),
            @ApiResponse(code = 400, message = "Validation failed", response = ApiHttpClientErrorException.class),
            @ApiResponse(code = 403, message = "User is not an employee", response = ResourceForbiddenException.class),
            @ApiResponse(code = 404, message = "User not found", response = NoSuchElementException.class),
            @ApiResponse(code = 500, message = "Internal server error", response = ApiHttpServerErrorException.class)
    })
    @GetMapping
    public List<BankAccount> getAllBankAccounts() {
        return service.getAll();
    }

非常感谢! :)

java spring spring-boot swagger swagger-ui
1个回答
0
投票
If you're using Jackson, you can use @JsonIgnore.

else为各个属性设置隐藏true

@ApiModelProperty(position = 1, required = true, hidden=true, notes = "used to display user name")
© www.soinside.com 2019 - 2024. All rights reserved.