【问题标题】:Springfox - Schema for Interfaces EmptySpringfox - 接口模式为空
【发布时间】:2021-07-22 23:42:21
【问题描述】:

我使用的是 springfox 3.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();
}

在 Springfox 2.9.* 中,StringInterface 的架构是

{
  "title": "StringInterface",
   "type": "object",
   "properties": {
     "s": {
       "type": "String"
    }
  }
}

但是,当我升级到 3.0.0 时,架构现在是空的。

{
  "title": "StringInterface",
  "type": "object"
}

常规类正确显示。似乎只有接口有问题。

【问题讨论】:

  • 你使用什么依赖?你能展示你的招摇配置吗
  • @user3696953 我更新了问题

标签: java jackson springfox


【解决方案1】:

我能够解决问题。不幸的是,它需要对涉及的每个接口进行一些配置。

@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>()

【讨论】:

    【解决方案2】:

    我建议切换到 SpringDoc。

    <dependency>
      <groupId>org.springdoc</groupId>
      <artifactId>springdoc-openapi-ui</artifactId>
    </dependency>
    

    截至目前的最新版本,1.5.6 将为您的界面提供以下架构

    {
       "type": "object",
       "properties": {
         "string": {
           "type": "string"
        }
      }
    }
    

    您还必须稍微不同地设置配置类并使用@RestController 注释您的控制器。我个人从未见过将接口用作 RequestBody 的情况,所以这种情况有点奇怪。

    但是,如果您打算使用依赖版本,那么我认为这与 swagger 模式生成如何解释您的 JsonDeserialization 有关。

    【讨论】:

    • 这个和Springfox有同样的问题
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-17
    • 1970-01-01
    • 2013-12-03
    • 1970-01-01
    • 1970-01-01
    • 2010-09-05
    相关资源
    最近更新 更多