gpt4 book ai didi

java - Swagger requestbody 示例值为空

转载 作者:行者123 更新时间:2023-12-02 00:49:28 27 4
gpt4 key购买 nike

我们正在尝试设置 springfox:swagger-ui。但是,当使用 Value.Immutable 接口(interface)通过 post 方法创建 RestController 时,示例显示 {} 并且模型显示接口(interface)名称,但没有其他内容。

我注意到,当我使用 Immutable 类作为请求主体时,该示例显示了它的所有属性。我尝试在界面上添加子类型 (@ApiModel(subTypes = ImmutableInputLead.class)) 和父配置 (@ApiModel(parent = ImmutableInputLead.class)) ,但没有不改变任何东西

这是我们的不可变接口(interface):

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

@Valid
Contact getContact();
}

我们的 map :

@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;
}

我们的 Swagger 配置:

@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 类的变量。

我做错了什么?

最佳答案

我相信您已经解决了这个问题,但是,我将在这里留下一个解决方案以供将来查询。

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

一种方法是手动为不可变接口(interface)添加替代类型,如下所示

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

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

关于java - Swagger requestbody 示例值为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57871972/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com