如何解决Wildfly Swarm中SwaggerSerializers类的WELD-001318错误?
I’ve dealt with this exact frustrating issue before—those ambiguous CDI resolution errors from duplicate Swagger classes are a real headache. Since you want to avoid manually listing all fractions just to exclude Swagger, here are three practical, clean fixes:
1. Exclude the Swagger Fraction Without Disabling Auto-Discovery
You can keep relying on Wildfly Swarm’s automatic fraction detection while specifically skipping the unstable Swagger fraction. There are two straightforward ways to do this in your pom.xml:
Option A: Exclude via Core Dependency
Add an exclusion to your base Wildfly Swarm dependency:
<dependency> <groupId>org.wildfly.swarm</groupId> <artifactId>wildfly-swarm</artifactId> <version>2017.12.1</version> <type>pom</type> <exclusions> <exclusion> <groupId>org.wildfly.swarm</groupId> <artifactId>swagger</artifactId> </exclusion> </exclusions> </dependency>
Option B: Exclude via Maven Plugin
If you’re using the Wildfly Swarm Maven plugin, add an exclusion directly to its configuration:
<plugin> <groupId>org.wildfly.swarm</groupId> <artifactId>wildfly-swarm-plugin</artifactId> <version>2017.12.1</version> <configuration> <excludedFractions> <excludedFraction>swagger</excludedFraction> </excludedFractions> </configuration> </plugin>
Both approaches let Swarm continue auto-discovering all other required fractions while ditching the problematic Swagger one.
2. Upgrade to a Fixed Wildfly Swarm Version
The SWARM-271 bug you referenced was resolved in later releases. Upgrading to 2018.1.1 or any subsequent version will eliminate the duplicate Swagger class issue entirely. Just update the version number across all your Wildfly Swarm dependencies and plugins:
<properties> <wildfly-swarm.version>2018.1.1</wildfly-swarm.version> </properties>
This is the cleanest long-term solution, as it also gets you other bug fixes and minor improvements.
3. Temporary CDI Ambiguity Workaround (Last Resort)
If you can’t exclude or upgrade right now, you can force CDI to resolve the ambiguity by prioritizing one instance of SwaggerSerializers. Create a producer method in your code:
import javax.enterprise.inject.Produces; import javax.annotation.Priority; import javax.interceptor.Interceptor; import io.swagger.jaxrs.listing.SwaggerSerializers; public class SwaggerSerializerProducer { @Produces @Priority(Interceptor.Priority.APPLICATION + 1) public SwaggerSerializers produceSwaggerSerializers() { return new SwaggerSerializers(); } }
This tells CDI to use your produced instance over the duplicate ones. Note: This is a hacky workaround and should only be used if the first two options aren’t feasible.
内容的提问来源于stack exchange,提问作者user5444681

