我使用的是Springfox3.0.0,并且我有一个用作@RequestBody的接口。
pom.xml
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>SwaggerConfig.java
class SwaggerConfig {
@Bean
Docket api() {
return new Docket(OAS_30)
.select()
.apis(RequestHandlerSelectors.basePackage(
"org.springframework.boot").negate())
.build();
}
}模型
//assume Lombok or standard getter/setters/constructors
class Clazz implements StringInterface {
String s;
Integer i;
}
@JsonDeserialize(as = Clazz.class)
interface StringInterface {
String getString();
}在Springfox2.9.*中,StringInterface的模式是
{
"title": "StringInterface",
"type": "object",
"properties": {
"s": {
"type": "String"
}
}
}但是,当我升级到3.0.0时,模式现在是空的。
{
"title": "StringInterface",
"type": "object"
}常规类显示正确。这似乎只是接口的问题。
发布于 2021-05-04 05:15:01
我可以做一个变通的方法。不幸的是,它需要对涉及到的每个接口进行一些配置。
@JsonDeserialize(as = Clazz.class)
interface StringInterface {
static class Model {
String getString();
}
}SwaggerConfig.java类SwaggerConfig {
@Bean
Docket api() {
return new Docket(OAS_30)
.select()
.apis(RequestHandlerSelectors.basePackage(
"org.springframework.boot").negate())
.directModelSubstitute(StringInterface.class, StringInterface.Model.class)
.build();
}
@Component
@Order(SwaggerPluginSupport.SWAGGER_PLUGIN_ORDER)
static class ModelDeclaringClassName implements TypeNameProviderPlugin {
boolean supports(DocumentationType delimiter) {
return true;
}
String nameFor(type: Class<?>) {
Class<?> actualClass = type.getSimpleName().equals("Model") ?
type.getDeclaringClass() :
type;
return type.getSimpleName();
}
}
}我的应用程序实际上是用Kotlin编写的,所以directModelSubstitute调用可以简化为:
private inline fun <reified T> Docket.substituteModel() =
T::class.java.let {
directModelSubstitute(
it,
it.declaredClasses.first { c ->
c.simpleName == "Model"
}
)
}用法如下:
.substituteModel<StringInterface>()发布于 2021-05-01 07:12:08
我建议改用SpringDoc。
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-ui</artifactId>
</dependency>到目前为止,最新版本1.5.6将为您的接口提供以下模式
{
"type": "object",
"properties": {
"string": {
"type": "string"
}
}
}您还必须以稍微不同的方式设置config类&使用@RestController注释您的控制器。我个人从来没有见过接口作为RequestBody的用法,所以,这种情况有点奇怪。
然而,如果你打算使用你的依赖版本,那么我认为这与swagger模式生成如何解释你的JsonDeserialization有关。
https://stackoverflow.com/questions/67320183
复制相似问题