一、异常描述
关于通过Undertow注入使用web应用服务器代码示例(如下所示),运行后报出“Exception in thread "main" java.lang.IllegalArgumentException: XNIO001001: No XNIO provider found”异常,详情如下INFO: XNIO version 3.6.2.Final@b@Exception in thread "main" java.lang.IllegalArgumentException: XNIO001001: No XNIO provider found@b@at org.xnio.Xnio.doGetInstance(Xnio.java:261)@b@at org.xnio.Xnio.getInstance(Xnio.java:178)@b@at io.undertow.Undertow.start(Undertow.java:95)@b@at test.HelloWorldServer.main(HelloWorldServer.java:24)
代码示例如下package test;@b@@b@import io.undertow.Undertow;@b@import io.undertow.server.HttpHandler;@b@import io.undertow.server.HttpServerExchange;@b@import io.undertow.util.Headers;@b@ @b@@UndertowExample("Hello World")@b@public class HelloWorldServer {@b@@b@ public static void main(final String[] args) {@b@ Undertow server = Undertow.builder()@b@ .addHttpListener(8080, "localhost")@b@ .setHandler(new HttpHandler() {@b@ @Override@b@ public void handleRequest(final HttpServerExchange exchange) throws Exception {@b@ exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, "text/plain");@b@ exchange.getResponseSender().send("Hello World");@b@ }@b@ }).build();@b@ server.start();@b@ }@b@@b@}@b@@b@package test;@b@@b@import java.lang.annotation.ElementType;@b@import java.lang.annotation.Retention;@b@import java.lang.annotation.RetentionPolicy;@b@import java.lang.annotation.Target;@b@@b@ @b@@Retention(RetentionPolicy.RUNTIME)@b@@Target(ElementType.TYPE)@b@public @interface UndertowExample {@b@ String value();@b@ String location() default "http://localhost:8080";@b@}
二、解决方法
项目引入了xnio-api-3.3.0.Final.jar依赖包,其中有org.xnio.XnioProvider接口对于依赖xnio-nio.jar中org.xnio.nio.NioXnioProvider实现类,因为版本不一致导致其无法完成Xnio类无法完成依赖的XnioProvider实例化,将项目中的依赖xnio-nio.jar依赖包改为对于3.3.0版本的实现为xnio-nio-3.3.0.Final.jar,问题都解决了