【问题标题】:swagger-core v3 ignore my configurations in javax.ws.rs.core.Applicationswagger-core v3 忽略我在 javax.ws.rs.core.Application 中的配置
【发布时间】:2020-12-16 17:43:40
【问题描述】:

使用 swagger-core v3 2.1.6 这三个配置产生相同的 openapi.json 结果。

无配置:

@ApplicationPath("/rest")
public class AuthRestConfig extends Application {

  public AuthRestConfig() {

  }
}

服务器配置,我需要使用这种conf:

@ApplicationPath("/rest")
public class AuthRestConfig extends Application {

  public AuthRestConfig(@Context ServletConfig servletConfig) {
    super();

    OpenAPI oas = new OpenAPI();
    Info info = new Info()
        .title("Suma Automaton OpenAPI")
        .description("This is a sample server Petstore server.  You can find out more about Swagger "
            + "at [http://swagger.io](http://swagger.io) or on [irc.freenode.net, #swagger](http://swagger.io/irc/).  For this sample, "
            + "you can use the api key `special-key` to test the authorization filters.")
        .termsOfService("http://swagger.io/terms/")
        .contact(new Contact()
            .email("baldodavi@gmail.com"));

    oas.info(info);

    String url = "/suma-automaton-ms";
    List<Server> servers = new ArrayList<>();
    Server server = new Server();
    server.setUrl(url);
    servers.add(server);
    oas.setServers(servers);

    SwaggerConfiguration oasConfig = new SwaggerConfiguration()
        .openAPI(oas)
        .prettyPrint(true)
        .resourcePackages(Stream.of("io.swagger.sample.resource").collect(Collectors.toSet()));

    try {
      new JaxrsOpenApiContextBuilder<>()
          .servletConfig(servletConfig)
          .application(this)
          .openApiConfiguration(oasConfig)
          .buildContext(true);
    } catch (OpenApiConfigurationException e) {
      throw new RuntimeException(e.getMessage(), e);
    }

  }
}

对这种行为有什么建议吗?有什么我误解了吗?考虑到我也使用https://github.com/swagger-api/swagger-core/wiki/Swagger-2.X---Integration-and-configuration#jax-rs-application中报告的标准配置有相同的行为@

【问题讨论】:

    标签: java rest swagger openapi


    【解决方案1】:

    将此添加到您的 AuthRestConfig 类中:

    import io.swagger.v3.jaxrs2.integration.resources.OpenApiResource;
    
    @Override
    public Set<Class<?>> getClasses() {
        Set<Class<?>> resources = new HashSet<>();
    
        resources.add(OpenApiResource.class);
    
        return resources;
    }
    

    【讨论】:

      猜你喜欢
      • 2022-07-08
      • 1970-01-01
      • 2021-01-21
      • 2017-08-23
      • 1970-01-01
      • 1970-01-01
      • 2013-11-08
      • 2021-01-17
      • 2017-04-29
      相关资源
      最近更新 更多