RESTeasy Undertow Example in documentation fails
richard.hart Dec 5, 2013 8:51 AMI am new to JBoss and Resteasy but I have been developing Java web applications for many years. I am interested in testing my Resteasy web service with JUnit and so tested the Undertow example. Of course, I want to test my application so what I have is not exactly this example but I keep getting the response 403 Forbidden.
I know what that means but I know I have everything right. The web service works and is running under JBoss AS 7.1.1. So, I thought ok, let's test the original example. Lo and behold, exactly the same response of 403 Forbidden.
First problem:
@BeforeClass public static void init() throws Exception { server = new UndertowJaxrsServer().start(); }
This results in the following trace:
java.lang.RuntimeException: java.net.BindException: Address already in use: bind
at io.undertow.Undertow.start(Undertow.java:129)
at org.jboss.resteasy.plugins.server.undertow.UndertowJaxrsServer.start(UndertowJaxrsServer.java:254)
at com.pwc.fdb.ws.test.UndertowTest.init(UndertowTest.java:68)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: java.net.BindException: Address already in use: bind
at sun.nio.ch.Net.bind0(Native Method)
at sun.nio.ch.Net.bind(Net.java:444)
at sun.nio.ch.Net.bind(Net.java:436)
at sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:214)
at sun.nio.ch.ServerSocketAdaptor.bind(ServerSocketAdaptor.java:74)
at sun.nio.ch.ServerSocketAdaptor.bind(ServerSocketAdaptor.java:67)
at org.xnio.nio.NioXnioWorker.createTcpConnectionServer(NioXnioWorker.java:147)
at org.xnio.XnioWorker.createStreamConnectionServer(XnioWorker.java:237)
at io.undertow.Undertow.start(Undertow.java:113)
... 18 more
java.lang.NullPointerException
at com.pwc.fdb.ws.test.UndertowTest.stop(UndertowTest.java:77)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:33)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
So change the port since 8080 is already in use (yeah, I'm a web developer so I have some servers running.)
@BeforeClass public static void init() throws Exception { // server = new UndertowJaxrsServer().start(); server = new UndertowJaxrsServer(); io.undertow.Undertow.Builder builder = Undertow.builder(); builder.addListener(8090, "localhost"); server.start(builder); }
Run the test: 403 Forbidden; but I also have the following debug entries:
[io.undertow.server.handlers.ResponseCodeHandler] DEBUG Logging Provider: org.jboss.logging.Log4jLoggerProvider [org.xnio.Xnio] INFO XNIO version 3.1.0.CR7 [org.xnio.nio.NioXnio] INFO XNIO NIO Implementation Version 3.1.0.CR7 [org.xnio.nio.WorkerThread] DEBUG Started channel thread 'XNIO-1 I/O-1', selector sun.nio.ch.WindowsSelectorImpl@54efd40d [org.xnio.nio.WorkerThread] DEBUG Started channel thread 'XNIO-1 I/O-3', selector sun.nio.ch.WindowsSelectorImpl@4d4acd0b [org.xnio.nio.WorkerThread] DEBUG Started channel thread 'XNIO-1 I/O-2', selector sun.nio.ch.WindowsSelectorImpl@fc77f54 [org.xnio.nio.WorkerThread] DEBUG Started channel thread 'XNIO-1 I/O-4', selector sun.nio.ch.WindowsSelectorImpl@56cfd14a [org.jboss.resteasy.logging.impl.Log4jLogger] INFO Deploying javax.ws.rs.core.Application: class com.pwc.fdb.ws.test.UndertowTest$MyApp [org.jboss.resteasy.logging.impl.Log4jLogger] INFO Adding class resource com.pwc.fdb.ws.test.UndertowTest$Resource from Application class com.pwc.fdb.ws.test.UndertowTest$MyApp [org.jboss.resteasy.logging.impl.Log4jLogger] DEBUG Unable to retrieve config: expandEntityReferences defaults to true [org.jboss.resteasy.logging.impl.Log4jLogger] DEBUG Unable to retrieve config: expandEntityReferences defaults to true [org.apache.http.impl.conn.BasicClientConnectionManager] DEBUG Get connection for route {}->http://localhost:8081 [org.apache.http.impl.conn.DefaultClientConnectionOperator] DEBUG Connecting to localhost:8081 [org.apache.http.client.protocol.RequestAddCookies] DEBUG CookieSpec selected: best-match [org.apache.http.client.protocol.RequestAuthCache] DEBUG Auth cache not set in the context [org.apache.http.client.protocol.RequestTargetAuthentication] DEBUG Target auth state: UNCHALLENGED [org.apache.http.client.protocol.RequestProxyAuthentication] DEBUG Proxy auth state: UNCHALLENGED [org.apache.http.impl.client.DefaultRequestDirector] DEBUG Attempt 1 to execute request [org.apache.http.impl.conn.DefaultClientConnection] DEBUG Sending request: GET /base/test HTTP/1.1 [org.apache.http.impl.conn.Wire] DEBUG >> "GET /base/test HTTP/1.1[\r][\n]" [org.apache.http.impl.conn.Wire] DEBUG >> "Accept-Encoding: gzip, deflate[\r][\n]" [org.apache.http.impl.conn.Wire] DEBUG >> "Host: localhost:8081[\r][\n]" [org.apache.http.impl.conn.Wire] DEBUG >> "Connection: Keep-Alive[\r][\n]" [org.apache.http.impl.conn.Wire] DEBUG >> "[\r][\n]" [org.apache.http.impl.conn.DefaultClientConnection] DEBUG >> GET /base/test HTTP/1.1 [org.apache.http.impl.conn.DefaultClientConnection] DEBUG >> Accept-Encoding: gzip, deflate [org.apache.http.impl.conn.DefaultClientConnection] DEBUG >> Host: localhost:8081 [org.apache.http.impl.conn.DefaultClientConnection] DEBUG >> Connection: Keep-Alive [org.apache.http.impl.conn.Wire] DEBUG << "HTTP/1.0 403 Forbidden[\r][\n]" [org.apache.http.impl.conn.Wire] DEBUG << "Server: Agent-ListenServer-HttpSvr/1.0[\r][\n]" [org.apache.http.impl.conn.Wire] DEBUG << "Date: do, 05 dec 2013 11:09:18 GMT[\r][\n]" [org.apache.http.impl.conn.Wire] DEBUG << "[\r][\n]" [org.apache.http.impl.conn.DefaultClientConnection] DEBUG Receiving response: HTTP/1.0 403 Forbidden [org.apache.http.impl.conn.DefaultClientConnection] DEBUG << HTTP/1.0 403 Forbidden [org.apache.http.impl.conn.DefaultClientConnection] DEBUG << Server: Agent-ListenServer-HttpSvr/1.0 [org.apache.http.impl.conn.DefaultClientConnection] DEBUG << Date: do, 05 dec 2013 11:09:18 GMT [org.apache.http.impl.conn.BasicClientConnectionManager] DEBUG Releasing connection org.apache.http.impl.conn.ManagedClientConnectionImpl@66755f0 [org.apache.http.impl.conn.DefaultClientConnection] DEBUG Connection 0.0.0.0:61880127.0.0.1:8081 shut down [org.apache.http.impl.conn.BasicClientConnectionManager] DEBUG Connection can be kept alive for 9223372036854775807 MILLISECONDS
Wait! What's this? It is running on localhost:8081? Why is it running on 8081 when I told it to use 8090?
Additionally, the following change makes no difference in the running of the test. It still runs at port 8081 and still have 403 Forbidden response.
@BeforeClass public static void init() throws Exception { // server = new UndertowJaxrsServer().start(); server = new UndertowJaxrsServer(); io.undertow.Undertow.Builder builder = Undertow.builder(); // builder.addListener(8090, "localhost"); server.start(builder); }
So I am obviously missing something. Likely how one sets the port address the server will use. But even if it is running on port 8081, why is it resulting in 403 Forbidden response on all three tests?