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:
1. Verify Your BeanConfig Configuration
First, double-check that your BeanConfig setup is correct—small mistakes here are often the culprit:
- Ensure
setBasePathmatches your API's root path exactly (e.g., if your APIs are athttp://localhost:8080/my-app/api, setbeanConfig.setBasePath("/my-app/api")). - Confirm
setResourcePackagepoints 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-jaxrsand 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:treeto 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
ResourceConfigsubclass, add registrations forApiListingResourceandSwaggerSerializers:
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
curlto hithttp://<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
@Apiannotations, and methods have@ApiOperationannotations. - 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.htmland confirm theurlparameter 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

