Swagger 2使用字段而不是getters + setters

时间:2019-10-03 13:07:00

标签: swagger swagger-2.0

我有以下Java REST服务

    @GET
    @Path("/{customerId}")
    @Operation(
            summary = "Test summary",
            description = "Test desciption",
            responses = {
                    @ApiResponse(
                            responseCode = "200",
                            content = @Content(
                                    schema = @Schema(implementation = CustomerDto.class),
                                    mediaType = "application/json"),
                            description = "CustomerDto description"),
                    @ApiResponse(
                            responseCode = "400",
                            description = "Description for 400 status")
                    //Here you can add all response statuses and their's descriptions
            })
    public CustomerDto findCustomerById(@Parameter(description = "Id of the customer", required = true) @PathParam("customerId") Long customerId) {

        return apiFacade.getCustomerDtoByCustomerId(customerId);
    }

简单的dto类

    public class CustomerDto {

        private TestInfo testInfo;

    }

简单子类

public class TestInfo {
    private Long testId;
    private Long checkId;

    public TestInfo(Long testId, Long checkId) {
        this.testId = testId;
        this.checkId = checkId;
    }

    public Long getTestId() {
        return testId;
    }

    public Long getCheckId() {
        return checkId;
    }

    public boolean isDecisionMade() {
        return testId != null || checkId != null;
    }
}

我希望看到没有'decisionMade'部分的TestInfo-json。你能帮我吗?

      "TestInfo" : {
        "type" : "object",
        "properties" : {
          "testId" : {
            "type" : "integer",
            "format" : "int64"
          },
          "checkId" : {
            "type" : "integer",
            "format" : "int64"
          },
          "decisionMade" : {
            "type" : "boolean"
          }
        }
      }

是否可以说出昂首阔步的使用类字段而不是getter + setter? 我使用的是2.2.2版。通过BaseOpenApiResource生成json。所有配置均为默认设置。

1 个答案:

答案 0 :(得分:0)

我找到了决定:

        Json.mapper().setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.NONE);
        Json.mapper().setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);