Swagger中@ApiModel注解被忽略,接口实现类未生成JSON的问题求助
I’ve run into this exact issue with Swagger 1.5.x before—when an interface’s implementation lives in a separate JAR, Swagger’s default scanner often misses it unless you give it explicit hints. Let’s break down the solutions in order of simplicity:
1. Fix Your swagger-annotations Dependency Scope
You mentioned adding swagger-annotations as a provided dependency, but this could be causing runtime issues. Swagger needs access to the @ApiModel annotation class at runtime to process CustomDataX in the external JAR. Switch the scope from provided to compile (or runtime) in your build file:
<!-- Maven example --> <dependency> <groupId>io.swagger</groupId> <artifactId>swagger-annotations</artifactId> <version>1.5.22</version> <scope>compile</scope> <!-- Replace "provided" with this --> </dependency>
2. Include the CustomDataX Package in Swagger’s Scan
Swagger only scans the packages specified in resourcePackage. If CustomDataX lives in a different package than ContainerObject, add it to the list (separated by commas):
beanConfig.setResourcePackage("ag.company.x, com.external.package.where.customdatax.is");
This tells Swagger to look for annotated classes in both packages.
3. Explicitly Declare Subtypes on the CustomData Interface
If you don’t want to scan the entire external package, you can directly tell Swagger about the implementation using the subTypes attribute on the interface’s @ApiModel:
@ApiModel(subTypes = {CustomDataX.class}) public interface CustomData { // Your interface code }
This works as long as your project has a compile-time dependency on the external JAR (which it should, since you’re using CustomDataX).
4. Register the Subtype Programmatically with a ModelConverter
If the above options aren’t feasible (e.g., you can’t modify the interface), you can register CustomDataX manually using a custom ModelConverter:
First, create the converter class:
public class CustomDataSubtypeConverter implements ModelConverter { @Override public Model resolve(ModelConverterContext context, Type type, Annotation[] annotations, Iterator<ModelConverter> chain) { Model model = chain.next().resolve(context, type, annotations, chain); // Add CustomDataX as a subtype of CustomData if (type == CustomData.class && model instanceof ModelImpl) { ((ModelImpl) model).getSubTypes().add("com.external.package.CustomDataX"); } return model; } // Delegate other required methods to the chain @Override public Schema resolveSchema(ModelConverterContext context, Type type, Annotation[] annotations, Iterator<ModelConverter> chain) { return chain.next().resolveSchema(context, type, annotations, chain); } }
Then register it in your JaxRsActivator:
public JaxRsActivator() { super(); BeanConfig beanConfig = new BeanConfig(); // ... your existing BeanConfig setup ... // Add the custom converter to Swagger's model processing ModelConverters.getInstance().addConverter(new CustomDataSubtypeConverter()); }
One final check: ensure CustomDataX has its @ApiModel annotation (which it does in your example) and that its fields are annotated with @ApiModelProperty so they appear in the Swagger JSON.
内容的提问来源于stack exchange,提问作者Rokko_11

