Spring Boot 应用程序中使用 JAX-RS 的 Swagger UI (SpringFox) - 无法加载 API 定义

问题描述 投票:0回答:1

我按照本指南在 Spring Boot 应用程序中启用带有 JAX-RS 的 Swagger UI:https://atechref.com/using-swagger-2-with-spring-boot-spring-fox-and-jax-rs -project/ 但我总是收到以下错误:无法加载 API 定义

enter image description here

我使用以下依赖项:

 <dependency>
        <groupId>io.swagger</groupId>
        <artifactId>swagger-jersey2-jaxrs</artifactId>
        <version>1.6.2</version>
    </dependency>

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-boot-starter</artifactId>
    <version>3.0.0</version>
</dependency>

Spring 启动版本:2.3.3.RELEASE

这些是我现在的课程:

@EnableSwagger2
@SpringBootApplication
@Import({MyAppWebSpringConfig.class})
public class MyAppApplication {

  public static void main(String[] args) {
    SpringApplication.run(MyAppApplication.class, args);
  }
}

@Component
@Primary
public class CombinedResourceProvider implements SwaggerResourcesProvider{

  @Resource
  private InMemorySwaggerResourcesProvider inMemorySwaggerResourcesProvider;

  public List<SwaggerResource> get() {

    SwaggerResource jerseySwaggerResource = new SwaggerResource();
    jerseySwaggerResource.setLocation("/my-app/swagger.json");
    jerseySwaggerResource.setSwaggerVersion("2.0");
    jerseySwaggerResource.setName("Jersey");

    return Stream.concat(Stream.of(jerseySwaggerResource),
        inMemorySwaggerResourcesProvider.get().stream()).collect(Collectors.toList());
  }

}

@Configuration
@ApplicationPath("/my-app")
public class MyAppResourceConfig extends ResourceConfig{

  @Autowired
  public MyAppResourceConfig(@Qualifier("ets-feature") Feature jerseyOperationalFeature){
    register(jerseyOperationalFeature);
    register(MyAppResource.class);

    //exceptions
    register(GenericExceptionMapper.class);

    configureJersey();
  }

  public void configureJersey(){
    BeanConfig swaggerConfig = new BeanConfig();
    swaggerConfig.setBasePath("/my-app");
    SwaggerConfigLocator.getInstance().putConfig(SwaggerContextService.CONFIG_ID_DEFAULT, swaggerConfig);

    packages(getClass().getPackage().getName(), ApiListingResource.class.getPackage().getName());


  }



@Configuration
@EnableConfigurationProperties
@ComponentScan(basePackages = {"com.my.app"})
@EnableGlobalMethodSecurity(prePostEnabled = true, proxyTargetClass = true)
@Import({CoreSpringConfig.class})
public class MyAppWebSpringConfig extends SpringBootServletInitializer{

  @Bean
  public Docket api(){
    return new Docket(DocumentationType.SWAGGER_2)
        .select()
        .apis(RequestHandlerSelectors.basePackage("com.my.app"))
        .paths(PathSelectors.any())
        .build();
  }

}

知道我做错了什么吗?..

java spring-boot jax-rs swagger-ui springfox
1个回答
0
投票

https://modelzoo.co/model/swagger-spring-jaxrs-sample-app

根据上面的文章。我对课程做了一些调整

JerseyConfig
请参阅代码:

@Component
@ApplicationPath("/api")
public class JerseyConfig extends ResourceConfig {

 public JerseyConfig() {
    configureSwagger();
  }

  private void configureSwagger() {
        register(ApiListingResource.class);
        BeanConfig beanConfig = new BeanConfig();
        beanConfig.setVersion("0.0.1");
        beanConfig.setSchemes(new String[]{"http"});
        beanConfig.setHost("localhost:8080"); // change if necessary 
        beanConfig.setBasePath("/");
        beanConfig.setDescription("Sample");
        beanConfig.setContact("VIIGit");
        beanConfig.setResourcePackage("ch.vii.git.swagger.sample.rest"); // chanage the package path which you need to scan
        beanConfig.setPrettyPrint(true);
        beanConfig.setScan(true);
    }
}

之后,尝试打开

http://localhost:8080/api/swagger.json
,如果看到swagger json数据,则打开页面
http://localhost:8080/swagger-ui.html
将不再显示错误。

您遵循的指南的其余部分(

https://atechref.com/using-swagger-2-with-spring-boot-spring-fox-and-jax-rs-project/
)似乎没有问题。

© www.soinside.com 2019 - 2024. All rights reserved.