Swagger请求正文示例值为空

时间:2019-09-10 13:33:29

标签: java swagger springfox immutables-library

我们正在尝试设置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类的变量。

我在做什么错了?

1 个答案:

答案 0 :(得分:0)

我相信您已经解决了这个问题,但是,我将在此处为将来的查询留下一个解决方案。

我不确定您使用的是哪个版本,但是您可以确认here时,swagger@2.x似乎不支持@JsonSerializer @JsonDeserializer >

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

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

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