Swagger请求主体示例值为空

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

我们正在尝试设置springfox:swagger-ui。但是,在使用Value.Immutable接口的post方法创建restcontroller时,示例显示{},模型显示接口名称,但仅显示其他名称。

我注意到,当我将Immutable类用作请求正文时,该示例显示了其所有属性。我尝试在接口上添加子类型(@ApiModel(subTypes = ImmutableInputLead.class))和父配置(@ApiModel(parent = ImmutableInputLead.class)),但未做任何更改

这是我们不变的界面:

@Value.Immutable
@ApiModel
@JsonSerialize(as = ImmutableInputLead.class)
@JsonDeserialize(as = ImmutableInputLead.class)
public interface InputLead {
    @Nullable
    @ApiModelProperty(example = "request id")
    String getRequestId();

    @Valid
    Contact getContact();
}

我们的映射:

@ApiOperation(value = "Create a new lead")
@PostMapping(value = "/")
@Validated
public ResponseEntity create(@Valid @RequestBody InputLead inputLead) throws URISyntaxException {
    String leadId = leadService.create(inputLead);
    ResourceRef leadResourceRef = resourceRefFactory.newResourceRef("/api/leads/" + leadId);

    return ResponseEntity.created(new URI(leadResourceRef.getUrl())).build();
}

我们的对象映射器:

@Bean
public ObjectMapper objectMapper() {
    Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder()
            .indentOutput(true)
            .dateFormat(new ISO8601DateFormat())
            .serializationInclusion(JsonInclude.Include.NON_EMPTY);

    ObjectMapper objectMapper = builder.build();
    objectMapper.registerModule(new JodaModule());
    objectMapper.registerModule(new GuavaModule());
    objectMapper.registerModule(new MazdaValuesModule());
    objectMapper.registerModule(new DateTimeJacksonModule());
    return objectMapper;
}

我们的招摇配置:

@EnableSwagger2
@Configuration
public class SwaggerConfig {
    @Bean
    public Docket api() {

        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any())
                .build()
                .directModelSubstitute(LocalDate.class, String.class)
                .directModelSubstitute(LocalTime.class, String.class)
                .directModelSubstitute(LocalDateTime.class, String.class)
                .apiInfo(apiInfo());
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder().title("Lead capturing API").build();
    }

}

我希望在示例中显示InputLead类的变量。

我在做什么错?

java swagger springfox immutables-library
1个回答
0
投票
我不确定您使用的是哪个版本,但是看起来[email protected]不支持@JsonSerializer @JsonDeserializer,您可以确认here

一种方法是为不可变的接口手动添加替代类型,例如这里

Docket(SWAGGER_2).alternateTypeRules(AlternateTypeRules.newRule(KeyIdentifier.class, ImmutableKeyIdentifier.class))

但是,我正在尝试提出更好的解决方案,如果找到它,将在此处发布

© www.soinside.com 2019 - 2024. All rights reserved.