You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Swagger.json无法加载至Swagger UI 集成swagger-core问题咨询

Hey there! Let's troubleshoot why your Swagger.json isn't loading into Swagger UI after setting up swagger-core with Jersey 2.X. I'll walk you through common fixes step by step:

Common Fixes for Swagger.json Loading Issues

1. Verify Your BeanConfig Configuration

First, double-check that your BeanConfig setup is correct—small mistakes here are often the culprit:

  • Ensure setBasePath matches your API's root path exactly (e.g., if your APIs are at http://localhost:8080/my-app/api, set beanConfig.setBasePath("/my-app/api")).
  • Confirm setResourcePackage points to the package containing your REST resource classes (including any subpackages if needed).
  • Make sure setScan(true) is called after setting all other properties, and that there are no exceptions thrown during the scan (check your logs!).

Example correct BeanConfig setup:

BeanConfig beanConfig = new BeanConfig();
beanConfig.setVersion("1.0.0");
beanConfig.setSchemes(new String[]{"http"});
beanConfig.setHost("localhost:8080");
beanConfig.setBasePath("/my-app/api");
beanConfig.setResourcePackage("com.yourcompany.myapp.resources");
beanConfig.setScan(true);

2. Fix Maven Dependency Conflicts

Since you already downgraded the reflections package, confirm your dependencies are fully resolved:

  • Ensure you've properly excluded the conflicting reflections version from swagger-jaxrs and added the compatible 0.9.11 version:
<dependency>
    <groupId>io.swagger</groupId>
    <artifactId>swagger-jaxrs</artifactId>
    <version>1.5.18</version>
    <exclusions>
        <exclusion>
            <groupId>org.reflections</groupId>
            <artifactId>reflections</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>org.reflections</groupId>
    <artifactId>reflections</artifactId>
    <version>0.9.11</version>
</dependency>
  • Run mvn dependency:tree to check for any leftover conflicting versions of reflections or swagger libraries.

3. Register Swagger Resources in Jersey

Swagger's JAX-RS resources need to be registered with your Jersey application to serve the Swagger.json:

  • In your ResourceConfig subclass, add registrations for ApiListingResource and SwaggerSerializers:
public class MyJerseyApp extends ResourceConfig {
    public MyJerseyApp() {
        // Register your own API resources
        packages("com.yourcompany.myapp.resources");
        
        // Register Swagger core resources
        register(ApiListingResource.class);
        register(SwaggerSerializers.class);
        
        // Initialize BeanConfig here (as shown in step 1)
    }
}

4. Test Swagger.json Directly

Before checking Swagger UI, verify that Swagger.json is accessible directly:

  • Open your browser or use curl to hit http://<your-host>:<port><base-path>/swagger.json (e.g., http://localhost:8080/my-app/api/swagger.json).
    • If you get a 404: Your base path is wrong, or Swagger resources aren't registered.
    • If you get an empty or incomplete JSON: The scan didn't find your API classes—check your resource package and ensure your REST classes have @Api annotations, and methods have @ApiOperation annotations.
    • If you get a 500 error: Check your server logs for exceptions (e.g., reflections scan errors, class loading issues).

5. Correct Swagger UI Configuration

If Swagger.json is accessible directly but not loading in UI:

  • Open your Swagger UI's index.html and confirm the url parameter points to the correct Swagger.json path:
const ui = SwaggerUIBundle({
  url: "http://localhost:8080/my-app/api/swagger.json", // Make sure this matches!
  dom_id: '#swagger-ui',
  // ... other UI configs
});

6. Check Logs for Hidden Errors

Enable debug logging for the io.swagger package to see what's happening during the scan:

  • Add this to your logging config (e.g., logback.xml):
<logger name="io.swagger" level="DEBUG"/>

Look for logs about class scanning, resource registration, or any exceptions that might be blocking Swagger.json generation.


内容的提问来源于stack exchange,提问作者Yogesh Gupta

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 08:43:37