REST with vert.x

2,878 views
Skip to first unread message

Wilson MacGyver

unread,
Dec 21, 2011, 9:54:00 PM12/21/11
to ve...@googlegroups.com
In a vert.x application,

how does one respond to a specific verb?

ie, POST, DELETE, PUT, or even PATCH.

Thanks

--
Omnem crede diem tibi diluxisse supremum.

Tim Fox

unread,
Dec 22, 2011, 2:45:22 AM12/22/11
to ve...@googlegroups.com
Hi Wilson,

This is the routematcher that we were discussing previously.

Examples:

Java: https://github.com/purplefox/vert.x/blob/master/src/examples/java/org/vertx/java/examples/routematch/RouteMatchExample.java

Ruby: https://github.com/purplefox/vert.x/blob/master/src/examples/ruby/route_match/route_match_example.rb

Substitute the get() method for post(), delete() etc. (Ooops we don't support patch yet, but it would be trivial to add.. if anyone actually uses it ;) )

Javadoc is here: http://purplefox.github.com/vert.x/v0.2/java/api/index.html

Yardoc is here: http://purplefox.github.com/vert.x/v0.2/ruby/api/Vertx/RouteMatcher.html

It's similar to the REST matching you find in Sinatra http://www.sinatrarb.com/intro.html and Express http://expressjs.com/

Hope that helps.

Wilson MacGyver

unread,
Dec 22, 2011, 8:24:59 AM12/22/11
to ve...@googlegroups.com
Excellent, thank you very much!

As for patch, I don't think it's used that often. The first high
profile usage I've come
across is GitHub's v3 API.

--

Tim Fox

unread,
Dec 22, 2011, 4:20:49 PM12/22/11
to vert.x
I've added the missing HTTP methods in master :)

On Dec 22, 1:24 pm, Wilson MacGyver <wmacgy...@gmail.com> wrote:
> Excellent, thank you very much!
>
> As for patch, I don't think it's used that often. The first high
> profile usage I've come
> across is GitHub's v3 API.
>
>
>
>
>
>
>
>
>
> On Thu, Dec 22, 2011 at 2:45 AM, Tim Fox <timvo...@gmail.com> wrote:
> > Hi Wilson,
>
> > This is the routematcher that we were discussing previously.
>
> > Examples:
>
> > Java:
> >https://github.com/purplefox/vert.x/blob/master/src/examples/java/org...
>
> > Ruby:
> >https://github.com/purplefox/vert.x/blob/master/src/examples/ruby/rou...
>
> > Substitute the get() method for post(), delete() etc. (Ooops we don't
> > support patch yet, but it would be trivial to add.. if anyone actually uses
> > it ;) )
>
> > Javadoc is here:http://purplefox.github.com/vert.x/v0.2/java/api/index.html
>
> > Yardoc is here:
> >http://purplefox.github.com/vert.x/v0.2/ruby/api/Vertx/RouteMatcher.html
>
> > It's similar to the REST matching you find in Sinatra
> >http://www.sinatrarb.com/intro.htmland Expresshttp://expressjs.com/

Wilson MacGyver

unread,
Dec 22, 2011, 7:34:21 PM12/22/11
to ve...@googlegroups.com
That was fast. :) Thanks

Antoine Roux

unread,
Jan 12, 2012, 1:31:42 PM1/12/12
to vert.x
Hi,
I had some fun and integrated Jersey into a vert.x application. It is
more of a proof of concept now, but it works. For higer level REST
applications, it can be easier than managing everything by hand.
If you are interested Wilson, I can share the code.


Antoine



On Dec 23 2011, 1:34 am, Wilson MacGyver <wmacgy...@gmail.com> wrote:
> That was fast. :) Thanks
>
> On Dec 22, 2011, at 4:20 PM, Tim Fox <timvo...@gmail.com> wrote:
>
>
>
>
>
>
>
> > I've added the missing HTTP methods in master :)
>
> > On Dec 22, 1:24 pm, Wilson MacGyver <wmacgy...@gmail.com> wrote:
> >> Excellent, thank you very much!
>
> >> As for patch, I don't think it's used that often. The first high
> >> profile usage I've come
> >> across is GitHub's v3 API.
>
> >> On Thu, Dec 22, 2011 at 2:45 AM, Tim Fox <timvo...@gmail.com> wrote:
> >>> Hi Wilson,
>
> >>> This is the routematcher that we were discussing previously.
>
> >>> Examples:
>
> >>> Java:
> >>>https://github.com/purplefox/vert.x/blob/master/src/examples/java/org...
>
> >>> Ruby:
> >>>https://github.com/purplefox/vert.x/blob/master/src/examples/ruby/rou...
>
> >>> Substitute the get() method for post(), delete() etc. (Ooops we don't
> >>> support patch yet, but it would be trivial to add.. if anyone actually uses
> >>> it ;) )
>
> >>> Javadoc is here:http://purplefox.github.com/vert.x/v0.2/java/api/index.html
>
> >>> Yardoc is here:
> >>>http://purplefox.github.com/vert.x/v0.2/ruby/api/Vertx/RouteMatcher.html
>
> >>> It's similar to the REST matching you find in Sinatra
> >>>http://www.sinatrarb.com/intro.htmlandExpresshttp://expressjs.com/

Wilson MacGyver

unread,
Jan 12, 2012, 7:57:35 PM1/12/12
to ve...@googlegroups.com
Sure, I'd like to see the code if you don't mind. But more importantly, I'm
much more interested in your experience.

I had though integrating a traditional framework like Jersey would
have some serious
impact on the non-blocking nature of vert.x. How's the performance? How about
the development process? Did you feel like it's the same as say
writing Jersey app
to run under tomcat/jetty?

Thanks,

On Thu, Jan 12, 2012 at 1:31 PM, Antoine Roux <roux...@gmail.com> wrote:
> Hi,
> I had some fun and integrated Jersey into a vert.x application. It is
> more of a proof of concept now, but it works. For higer level REST
> applications, it can be easier than managing everything by hand.
> If you are interested Wilson, I can share the code.
>
>
> Antoine

Antoine Roux

unread,
Jan 16, 2012, 5:36:00 AM1/16/12
to ve...@googlegroups.com

Hi,
Being still a beginner with vert.x, I had exactly the same questions, this is why I tried integrating Jersey : I wanted to know whether it had a chance to work in this model.

For now, I did not test the performance, except for a quick ab that looked not too bad. But the current integration won't access the network on its own. Jersey is made so that you can integrate it into your own web server. So you just have to pass it the contents of the request and get the result. So you can use vert.x for that, in the non-blocking way. Except for the network, I think Jersey does no I/O. I would have to check though, because it discovers the resources on the classpath, so this might use the filesystem. But if it is only once at startup, this is fine I guess.

I published my first implementation on Github : https://github.com/antoineroux/vert.x-jersey

It is just a demo for now, production use would require a bit more work. But if you have a look at it, and have any suggestions, I would be glad to know.


Antoine

Alexander Blotny

unread,
Aug 23, 2012, 4:38:28 AM8/23/12
to ve...@googlegroups.com
Hi Antoine,

I wanted to try your vert.x-jersey implementation.
After some problems with gradle I was able to build the vert.x-jersey.jar. Guess I would have been faster using another tool I know... ;-)

I also made some modification on your Demo.java file due to some compilation errors:

package test;

import org.vertx.java.deploy.Verticle;
import org.vertx.java.core.http.RouteMatcher;
import org.vertx.java.core.http.HttpServer;
import vertx.RestHandler;
import java.net.URISyntaxException;
import java.net.URI;

public class Demo extends Verticle {

    public void start() {
        RouteMatcher rm = new RouteMatcher();

        try {
                rm.all("/api/.*", new RestHandler(new URI("http://localhost:8080/api/"), "com.example.resources"));
        } catch (URISyntaxException e) {
            // Exceptions are currently not handled by the project.
            e.printStackTrace();
        }

        HttpServer server = vertx.createHttpServer().requestHandler(rm).listen(8080);
}
}

I try to start it the following way:

vertx run Demo.java -cp build/libs/vert.x-jersey.jar:libs/jersey-core-1.1.5.jar:libs/jersey-server-1.1.5.jar

After I solved all compilation problems I just get the following:

Cannot find verticle Demo.java

The vert.x.log does not provide additional information:

[New I/O  worker #3] 10:35:48,898 SEVERE [org.vertx.java.deploy.impl.VerticleManager]  Cannot find verticle Demo.java


Maybe some advise from someone?

Thanks,
Alex



Tim Fox

unread,
Aug 23, 2012, 5:09:05 AM8/23/12
to ve...@googlegroups.com
Running verticles as Java source is just for quick demos and only works with the default package (you are using package test).

For any non trivial Java verticles just compile them like any other Java program and run them specifying the FQCN
--
You received this message because you are subscribed to the Google Groups "vert.x" group.
To view this discussion on the web, visit https://groups.google.com/d/msg/vertx/-/AvjCr_nImT4J.
To post to this group, send an email to ve...@googlegroups.com.
To unsubscribe from this group, send email to vertx+un...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/vertx?hl=en-GB.


Alexander Blotny

unread,
Aug 24, 2012, 7:49:27 AM8/24/12
to ve...@googlegroups.com
Hi,

Thanks for your help - Unfortunately I have still some problems.

I removed the package test and compiled Demo.java. If I run it the following happens:

vertx run Demo -cp libs/vert.x-jersey.jar:libs/jersey-core-1.1.5.jar:libs/jersey-server-1.1.5.jar:libs/jsr311-api-1.1.1.jar

Failed to create verticle 
java.lang.IllegalStateException: Do not add application classes to the vert.x classpath
    at org.vertx.java.deploy.impl.java.JavaVerticleFactory.createVerticle(JavaVerticleFactory.java:81)
    at org.vertx.java.deploy.impl.VerticleManager$9.run(VerticleManager.java:615)
    at org.vertx.java.core.impl.Context$2.run(Context.java:118)
    at org.jboss.netty.channel.socket.nio.AbstractNioWorker.processEventQueue(AbstractNioWorker.java:361)
    at org.jboss.netty.channel.socket.nio.AbstractNioWorker.run(AbstractNioWorker.java:245)
    at org.jboss.netty.channel.socket.nio.NioWorker.run(NioWorker.java:35)
    at org.jboss.netty.util.ThreadRenamingRunnable.run(ThreadRenamingRunnable.java:102)
    at org.jboss.netty.util.internal.DeadLockProofWorker$1.run(DeadLockProofWorker.java:42)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
    at java.lang.Thread.run(Thread.java:722)

I am not sure which application classes I might have added.
If I leave the libs out I get of course ClassNotFoundException.

If I try the same without compiling:

vertx run Demo.java -cp libs/vert.x-jersey.jar:libs/jersey-core-1.1.5.jar:libs/jersey-server-1.1.5.jar:libs/jsr311-api-1.1.1.jar

Exception in Java verticle script 
com.sun.jersey.spi.service.ServiceConfigurationError: com.sun.jersey.spi.container.WebApplicationProvider: The class com.sun.jersey.server.impl.container.WebApplicationProviderImpl implementing the provider interface com.sun.jersey.spi.container.WebApplicationProvider is not found. The provider implementation is ignored.
    at com.sun.jersey.spi.service.ServiceFinder.fail(ServiceFinder.java:388)
    at com.sun.jersey.spi.service.ServiceFinder.access$200(ServiceFinder.java:144)
    at com.sun.jersey.spi.service.ServiceFinder$LazyObjectIterator.hasNext(ServiceFinder.java:652)
    at com.sun.jersey.spi.container.WebApplicationFactory.createWebApplication(WebApplicationFactory.java:61)
    at vertx.RestHandler.<init>(RestHandler.java:20)
    at Demo.start(Demo.java:14)
    at org.vertx.java.deploy.impl.VerticleManager$9.run(VerticleManager.java:642)
    at org.vertx.java.core.impl.Context$2.run(Context.java:118)
    at org.jboss.netty.channel.socket.nio.AbstractNioWorker.processEventQueue(AbstractNioWorker.java:361)
    at org.jboss.netty.channel.socket.nio.AbstractNioWorker.run(AbstractNioWorker.java:245)
    at org.jboss.netty.channel.socket.nio.NioWorker.run(NioWorker.java:35)
    at org.jboss.netty.util.ThreadRenamingRunnable.run(ThreadRenamingRunnable.java:102)
    at org.jboss.netty.util.internal.DeadLockProofWorker$1.run(DeadLockProofWorker.java:42)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
    at java.lang.Thread.run(Thread.java:722)

The class that cannot be found is inside the jersey-server.jar file. I guess the second problem could be also a jersey problem and is not directly related to the vertx platform.

Maybe you have some hints for me.

Thanks,
Alex

Tim Fox

unread,
Aug 24, 2012, 8:23:45 AM8/24/12
to ve...@googlegroups.com
Perhaps you have a CLASSPATH environment variable set which is pointing at some classes/directory which contains your app classes? Or maybe you have edited the vertx script and added?

What I would probably do here, is to create a trivial Verticle and run that (a source and as a class) and demonstrate that works. Then gradually add your Jersey code back in bit by bit until it stops working, then you know what is causing the problem.
--
You received this message because you are subscribed to the Google Groups "vert.x" group.
To view this discussion on the web, visit https://groups.google.com/d/msg/vertx/-/CQRe6r2FgjgJ.

Tim Fox

unread,
Aug 24, 2012, 8:25:40 AM8/24/12
to ve...@googlegroups.com
Another possibility is you've added some other jars in the VERTX_HOME/lib directory
To unsubscribe from this group, send email to vertx+unsubscribe@googlegroups.com.

Alexander Blotny

unread,
Aug 24, 2012, 9:22:48 AM8/24/12
to ve...@googlegroups.com
Hi,

the application is not in the CLASSPATH environment neither did I change anything in the vertx script. In the VERTX_HOME/lib folder is also no additional jar.


I tried a simple Verticle by replacing the RestHandler with null. Just to test if it is starting.
So there are no dependencies now to other jars.

vertx run Demo and
vertx run Demo.java

works.

But if I try the following:

vertx run Demo -cp libs/vert.x-jersey.jar:libs/jersey-core-1.1.5.jar:libs/jersey-server-1.1.5.jar:libs/jsr311-api-1.1.1.jar

Failed to create verticle 
java.lang.IllegalStateException: Do not add application classes to the vert.x classpath
    at org.vertx.java.deploy.impl.java.JavaVerticleFactory.createVerticle(JavaVerticleFactory.java:81)
    at org.vertx.java.deploy.impl.VerticleManager$9.run(VerticleManager.java:615)
    at org.vertx.java.core.impl.Context$2.run(Context.java:118)
    at org.jboss.netty.channel.socket.nio.AbstractNioWorker.processEventQueue(AbstractNioWorker.java:361)
    at org.jboss.netty.channel.socket.nio.AbstractNioWorker.run(AbstractNioWorker.java:245)
    at org.jboss.netty.channel.socket.nio.NioWorker.run(NioWorker.java:35)
    at org.jboss.netty.util.ThreadRenamingRunnable.run(ThreadRenamingRunnable.java:102)
    at org.jboss.netty.util.internal.DeadLockProofWorker$1.run(DeadLockProofWorker.java:42)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
    at java.lang.Thread.run(Thread.java:722)
    

And it does not matter which library I put into the classpath I got everytime this exception. I tried some libraries that should not be in the CLASSPATH.
Seems to be very strange. Some compilation problems maybe? I just compile it with javac Demo.java -cp .:/opt/vert.x/vert.x-1.2.3.final/lib/*

Using 

vertx run Demo.java -cp libs/vert.x-jersey.jar:libs/jersey-core-1.1.5.jar:libs/jersey-server-1.1.5.jar:libs/jsr311-api-1.1.1.jar

it works.

So as you stated that using source should only used for little test cases I would like to figure out what I am doing wrong with the classpath stuff.
Adding a library that is not used should not produce the error above - or?

Tim Fox

unread,
Aug 24, 2012, 9:38:50 AM8/24/12
to ve...@googlegroups.com
Have you tried it with other jars (i.e. not Jersey ones)? E.g. just create a simple jar with some class in it, add it to the classpath and see if you can reference it from Demo
--
You received this message because you are subscribed to the Google Groups "vert.x" group.
To view this discussion on the web, visit https://groups.google.com/d/msg/vertx/-/DGZu-ijE4-IJ.

To post to this group, send an email to ve...@googlegroups.com.
To unsubscribe from this group, send email to vertx+un...@googlegroups.com.

Tim Fox

unread,
Aug 24, 2012, 9:43:09 AM8/24/12
to ve...@googlegroups.com
If you can create a simple test contained program (e.g. attach a zip to this thread) that demonstrates the issue, I can take a look.
To unsubscribe from this group, send email to vertx+unsubscribe@googlegroups.com.

Alexander Blotny

unread,
Aug 24, 2012, 10:15:44 AM8/24/12
to ve...@googlegroups.com
Yes I tried other jars.

I will create and try simple tests on monday.

Thank you for your help!

Alexander Blotny

unread,
Aug 28, 2012, 5:55:31 AM8/28/12
to ve...@googlegroups.com
I tried a simple test case with an Handler implemented in the jar file.
Now even the package of the jar cannot be found though it is set in the classpath.

I attached the simple test.

The following output is caused by the test:

vertx run Demo.java -cp lib/simpleTest.jar

/home/ngni/opt/test/SimpleTest/Demo.java:1: error: package de.fhg.fokus.ngni.test does not exist
import de.fhg.fokus.ngni.test.TestHandler;
                             ^
/home/ngni/opt/test/SimpleTest/Demo.java:11: error: cannot find symbol
    TestHandler testHandler = new TestHandler();
    ^
  symbol:   class TestHandler
  location: class Demo
/home/ngni/opt/test/SimpleTest/Demo.java:11: error: cannot find symbol
    TestHandler testHandler = new TestHandler();
                                  ^
  symbol:   class TestHandler
  location: class Demo
Failed to create verticle
java.lang.RuntimeException: Compilation failed
    at org.vertx.java.deploy.impl.java.CompilingClassLoader.<init>(CompilingClassLoader.java:70)
    at org.vertx.java.deploy.impl.java.JavaVerticleFactory.createVerticle(JavaVerticleFactory.java:67)

    at org.vertx.java.deploy.impl.VerticleManager$9.run(VerticleManager.java:615)
    at org.vertx.java.core.impl.Context$2.run(Context.java:118)
    at org.jboss.netty.channel.socket.nio.AbstractNioWorker.processEventQueue(AbstractNioWorker.java:361)
    at org.jboss.netty.channel.socket.nio.AbstractNioWorker.run(AbstractNioWorker.java:245)
    at org.jboss.netty.channel.socket.nio.NioWorker.run(NioWorker.java:35)
    at org.jboss.netty.util.ThreadRenamingRunnable.run(ThreadRenamingRunnable.java:102)
    at org.jboss.netty.util.internal.DeadLockProofWorker$1.run(DeadLockProofWorker.java:42)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
    at java.lang.Thread.run(Thread.java:722)
Caused by: java.lang.RuntimeException: Compilation failed!
    at org.vertx.java.deploy.impl.java.CompilingClassLoader.<init>(CompilingClassLoader.java:67)
    ... 11 more

SimpleTest.tar.gz

Joern Bernhardt

unread,
Aug 28, 2012, 6:21:37 AM8/28/12
to ve...@googlegroups.com
If you want to run java source files, you must not put a package declaration at the top AFAIK.
If you compile it, you need to run it with the fully qualified name (vertx run de.fhg.fokus.ngni.test.Demo -cp lib/simpleTest.jar).

HTH
Joern

Joern Bernhardt

unread,
Aug 28, 2012, 6:25:26 AM8/28/12
to ve...@googlegroups.com
Sorry I didn't see your attachment first - the error message looked like you had the package declaration at the top...

Pid

unread,
Aug 28, 2012, 12:52:58 PM8/28/12
to ve...@googlegroups.com
On 28/08/2012 10:55, Alexander Blotny wrote:
> I tried a simple test case with an Handler implemented in the jar file.
> Now even the package of the jar cannot be found though it is set in the
> classpath.
>
> I attached the simple test.
>
> The following output is caused by the test:
>
> vertx run Demo.java -cp lib/simpleTest.jar

What does the following command produce:

jar -tf lib/simpleTest.jar | grep TestHandler

?


p
> --
> You received this message because you are subscribed to the Google
> Groups "vert.x" group.
> To view this discussion on the web, visit
> https://groups.google.com/d/msg/vertx/-/XWL8S1k-1PkJ.
> To post to this group, send an email to ve...@googlegroups.com.
> To unsubscribe from this group, send email to
> vertx+un...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/vertx?hl=en-GB.


--

[key:62590808]

signature.asc

Alexander Blotny

unread,
Aug 29, 2012, 3:19:49 AM8/29/12
to ve...@googlegroups.com

What does the following command produce:
 jar -tf lib/simpleTest.jar | grep TestHandler
 
de/fhg/fokus/ngni/test/TestHandler.class

flyz...@gmail.com

unread,
Jun 22, 2013, 12:18:42 AM6/22/13
to ve...@googlegroups.com
http://java.dzone.com/articles/rest-scala-and-vertx

在 2011年12月22日星期四UTC+8上午10时54分00秒,Mac写道:
Reply all
Reply to author
Forward
0 new messages