This article proposes a solution that has worked well for one of our projects at Volume Integration. I’ve created a sample application that demonstrates some of the key components of this solution.
You can download/clone the project here:
The sample application is a very simple web application that shows a graph of Utah State Park attendance for 2015. Here is a screenshot of the finished product.
The main components of the application are:
Web Application (SPA). The web application uses modern web application practices - including transpiling ES2016 code using Babel, packing and optimizing code and dependencies using webpack, as well as compiling advanced css using preprocessors like less and sass.
Development of this application requires the following:
The REST services are written in Java Utilizing Spring Boot & Spring MVC functionality. All of that code is located in
To develop the services code interactively just run
This will start up the embedded webserver (Tomcat by default) and deliver your services. As you write your code - the server should detect code changes and restart as necessary due the inclusion of Spring Boot DevTools.
gradlew bootRun -Dcors.origins=http://localhost:3000
There is one
hack feature that’s required to enable the
bootRun gradle tasks to accept and apply configuration properties in this manner. Add this snippet to the
This is because when developing the web application - it will be running on its own development server on port 3000 (see below) which will have a different host and we will need to configure CORS to allow the web application to consume the REST services.
I won’t delve into the all of the intricacies of how Spring MVC works its magic, but the following configurations are required in the app to make it work:
src/main/app. You will need to be in this directory to run the following commands.
The web application is packaged with webpack and utilizes webpack-dev-server to enable interactive development.
First you must download all of the necessary dependencies from npm.
npm run setup
Once that is complete you are ready to start the development server by simply running
npm start. However the application can be configured via an environment variable to connect to a REST server at any location. This way you can work on the web application and connect to any instance of your API (dev/test environments).
If you would like to connect to your local instance of the REST services that are running using the instructions above - you will just need to set the
REST_URL environment variable to
http://localhost:8080/api. The easiest way to do that is to just prepend the variable declaration to the start command like this:
REST_URL=http://localhost:8080/api npm start
Just add & configure the plugin in the
webpack.config.json file like this:
The application is transpiled, packaged and available at http://localhost:3000. This is why the CORS property must be configured properly above.
The development server communicates with your browser via web sockets - so any changes that are made to your code are immediately re-packaged and available to your browser without needing to refresh. Like Magic!
To compile and package the application just run this:
./gradlew bootRepackage on OSX/Linux
gradlew.bat bootRepackage on Windows.
build npm script inside our web application’s
package.json will transpile and package all of the front end code as necessary and place it in
src/main/resources/static - from which the Spring Boot application is preconfigured to serve static content.
The key is to add the proper gradle build dependencies to run the npm scripts before packaging the entire application into a jar. This is done with the following code in the
After it is packaged running the application is simple:
java -jar build/libs/packaged-webapp-1.0-SNAPSHOT.jar
This will start an embedded webserver (Tomcat) which you can access at http://localhost:8080. You can change the port if necessary by adding the
java -jar build/libs/packaged-webapp-1.0-SNAPSHOT.jar --server.port=8989
Being able to develop the separate application components both individually and independently provides many benefits. We can have both server side and client developers work concurrently in the same code base. This helps in keeping the REST services and Web application in sync.
Also utilizing Spring Boot to package and run the application simplifies both the building and deployment of the application. A simple gradle command compiles, transpiles, and packages all of the code (server code and client code) together. Deployment is simple - because it’s only one simple jar file and the only dependency is Java. No mucking about with slightly different servlet container configurations on different environments etc.
I’m sure there are other great solutions out there that help ease the burden of developing server/client web applications together and we’d love to hear about them. Let us know in the comments.
If you have any questions about how we’re making this work or questions about the example project feel free to reach out:
Utah State Park attendance Data: