• Dec
    • 04
    • 2012

JavaFX with a SpringMVC REST server in 5 minutes

Posted by In Enterprise, JavaFX

Creating a simple JavaFX application in 5 minutes was pretty fun, but we can do better. This post shows you how to use the javafx-rest-archetype to create a skeleton project that includes both a REST server (based on SpringMVC) and a JavaFX client that talks to that REST server.

As before you need to have already installed JDK 7 (update 6) or later, and Maven 3.0 or later (make sure you set the JAVA_HOME environment variable when doing this).

Step 1: Fix your classpath

If you did this last time then you do not need to do it again!

When you install the JDK currently, it includes JavaFX but does not put it on the classpath (yes this is weird – it will be fixed in Java 8). To fix this, open a Command Prompt and type:

mvn com.zenjava:javafx-maven-plugin:1.3:fix-classpath

This will ask you to confirm that you want to do this, type “y”.

Step 2: Create a New Project

Open a command prompt and go to a new workspace area (i.e. create a new directory to house your code), then type:

mvn archetype:generate -DarchetypeGroupId=com.zenjava 
    -DarchetypeArtifactId=javafx-rest-archetype -DarchetypeVersion=1.1

You will be prompted for some parameters:

  • groupId: com.mycompany
  • artifactId: my-rest-app
  • version: leave as default
  • package: com.mycompany.myrest

This will create a new directory called “my-rest-project” with some basic source code which you can use as a skeleton to get you up and running quickly.

Step 3: Build the project

Change directory into your newly created project and use Maven to build your application – this will build both the client and server:

cd my-rest-app
mvn clean install

Step 4: Start the server

Change directory into the server sub-directory and start a Jetty server to run your REST web server:

cd my-rest-app-server
mvn jetty:run

You do not have to have Jetty installed, Maven will take care of this for you. Jetty is very useful for getting up and running quickly like this, however you don’t have to use Jetty. Maven builds a standard WAR file that you can deploy into any JEE web server, such as Tomact, JBoss, etc.

Your server should now be running. You can test it using a standard web browser to http://localhost:8080/my-rest-app-server/welcome and you should see XML output like:

Step 5: Run the client

Open a new command prompt, change directory into the client sub-directory and start your JavaFX client:

cd my-rest-app\my-rest-app-client
mvn jfx:run

When the JavaFX client starts up enter your name and hit the “Say Hello” button. The response from the server will be shown in the text area below:

Step 6: Edit your project

You now have a working skeleton project which you can build upon to create your next awesome JavaFX and REST server application. Since you are using Maven, you can simply open the base level POM file in your favourite IDE and your project will be automatically configured for you with the classpath all setup, etc.

Here’s the links again for using Maven with your IDE:

Your project tree should look something like (this is taken from IntelliJ):

Hopefully the code is pretty self explanatory, and I have included JavaDoc with links to external help documentation in most cases. If you need more help, post comments here or raise issues in the GitHub project for the archetype: https://github.com/zonski/javafx-rest-archetype/issues

Step 7: Distribute your app

Distributing your server is very easy, just take the generated WAR file from the ‘target’ directory of your server module and copy this to any JEE web server. I use and highly recommend Tomcat but you can use whatever you like. In tomcat simply download and install Tomcat (i.e. unzip it) onto your server and then copy the WAR file into the webapps directory. Tomcat will take care of the rest – server side deployment is easy these days.

As with the simple JavaFX application example, you can choose any of the JavaFX deployment options such as Webstart, native installer, executable JAR file, etc. They each have their advantages and disadvantages but refer to my previous post for details on this.

All the same commands are available as in the simple JavaFX project, just make sure you run them from within the client module. For example to build an executable JAR use:

cd my-rest-app\my-rest-app-client
mvn jfx:build-jar

And an executable JAR will be built into the target directory of the client module for you to copy and distribute however you want.


Using this archetype it is very easy to get up and running quickly with a simple JavaFX client and SpringMVC REST server. All of the code generated can be modified, added to and deleted to suit your own project needs so there are no dependencies or links to any special classes of my creation in the project. This simply provides a starting shell for you to do with as you please.

This example is deliberately simple. It does not include security or database setup, both of which would be pretty much standard in your typical REST client-server application. If I get time I and/or there is strong interest in seeing a REST setup with security and persistence, I may add an additional archetype. Register your interest in this by commenting below.

REST is very popular because it provides a way for JavaScript/AJAX clients to make remote calls on servers. I’ve provided this archetype because AJAX is such a popular space, however there are better, faster and simpler options than REST when working with pure Java on both the client and server, so why compromise? I am planning to create another archetype that creates the same setup as this one but using Spring Remoting based on Spring’s HTTPInvoker. Hopefully I will have something going on this over the next month or two – again register your interest by commenting if this is something you would find useful.

If you have any problems or suggestions please comment or raise an issue on the archetype GitHub project:  https://github.com/zonski/javafx-rest-archetype/issues


  • Tapani
    December 14, 2012

    Nice work! I did a small change to get it work – changed Archetype Version from 1.1 -> 1.0

  • Diego
    January 11, 2013

    Hello, very good! I’ve been using GWT to construct by front-en for 4 years now. And now I am thinking to switch to JavaFX or ApachePivot. In my project with GWT I use Gilead to help me serialize my beans annotated with HIbernate. And it works just great: it is fast, easy to configure and I don’t have to build and maintain a whole hierarchy of DTOs just to ket client-server conversation. Unfornutally, Gilead does not provide a port to JavaFX or ApachePivot. Do you have any advice? I know there’s GraniteDS but it is complex to make it work. It would be greate to see an article talking about this issue. Your articles are easy to understand and you go ‘straight to the point’. Thank you very much,

  • zonski Author
    January 11, 2013

    If you have both a Java server and a Java client (i.e JavaFX), I would suggest using Spring-Remoting instead of REST. REST is only popular in supporting non-Java clients.

    See this (slightly old) post on using Spring-Remoting with JFX: http://www.zenjava.com/2012/02/25/going-remote-javafx-and-spring-remoting/

    Otherwise you could try one of the Java REST clients to unmarshall the REST to JavaBeans on the client side: http://stackoverflow.com/questions/221442/rest-clients-for-java

    Even with this approach (and the REST one), I still tend to use DTOs (i.e. I don’t return Hibernate beans directly but tend to map them into a simple, plain Java DTO that I then serialize down the wire). This keeps the database decoupled from your service layer and stops accidentally serializing too much down the wire, etc, at the cost of having duplication between the DTOs and the Entity beans. This is an optional design element though.

  • January 22, 2013

    Hello, someone mentioned to me that you were working on getting copies of the jdk8 javafx build tools like the ant-javafx.jar into maven central. I was wondering how that project was going and if there was anywhere I could follow it’s progress?

  • zonski Author
    January 22, 2013

    @Edub yes, this has been the major focus of my recent work on this and the reason I’ve not made many updates to the Maven plugin recently. I’ve been working with the JFX deployment guys from Oracle on this.

    90% done, but I need the guys from Oracle to do the build and deploy as it needs to be assembled on each of the native platforms and I don’t have them all. They have said they will hopefully get to it this week.

    Best place to watch for updates is the JFX Maven plugin home page as I will release an updated version of it very shortly after these tools are in central.

    • January 22, 2013

      thank you for the information, will keep an eye out. These tools are going to prove super useful.

  • Chui
    February 7, 2013

    What can I say, you took my through my first mvn experience. I’m sold on maven now. I’m in Australia too. Get in touch if you are looking for a buddy to tinker around with side JavaFX projects.

  • Samuel
    July 10, 2013

    Hi Daniel, sorry to write in this thread, but i dont found the right place…

    I red the post ” Building A Real World Database Application With JavaFX” and liked too much.

    But it’s not finished yet.

    I would like know if you pretend to finish? Thanks.

Leave a Comment