Using Spring for REST on Liberty Buildpack
During a recent development sprint our team was investigating frameworks in place to support Social Login for a RESTful application. In our research, we found that the common social login areas we were looking at Twitter, Facebook and Google all provided solid documentation on how to integrate into an application.
As we dug deeper to find previous artifacts and frameworks that we could take advantage of, we found Spring Social as one of the Spring Projects. It has core support for both Twitter and Facebook and linked to a community project for Google Social.
All of our previous RESTful implementations on Liberty Buildpack had used JAX-RS and we have found using that framework simple and powerful. The annotations were simple and for the most part our apps were simple enough to utilize just that and be successful.
As a team, we discussed if we wanted to mix JAX-RS and Spring Social, or did we set this project up with Spring REST and use one set of annotations. It’s important to note here that you can very much combine both Spring and JAX-RS, our decision was primarily being made based on code style and developer convenience rather than technical factors.
Setting up a Sprint REST app
One of the many attractions we had with JAX-RS was the simplicity of setting up and RESTful providers and associated methods for handling GET, POST, PUT and DELETE requests; annotate the method and done. Spring REST, unsurprisingly, does it the same way. Below you can see the annotations on a JAX-RS class and how that maps to the Spring annotations to achieve the same goal.
As you can see, from a code perspective, there isn’t much of a change to the general approach; the annotations are different but their use is obvious.
The real difference is how you then tell Spring to activate the application as a RESTful app.
Setting up web.xml
If you had previous experience with JAX-RS you will be familiar with the following web.xml fragment:
This provided the info to liberty as it loaded the war, it told it the context path to use and then said “hey I am JAX-RS” and the code scan of the annotations then loaded in the appropriate providers.
Spring isn’t a million miles away from this, however the approach is a little different.
The web.xml looks like this:
Then, along with web.xml, we add a spring-servlet.xml into the WEB-INF directory of your project:
The context:component-scan tag tells spring where to go find components with the annotations. You should set the base-package attribute to the name of your package in your war file.
Deploying to Bluemix
We are deploying our apps with gradle, for reference here is our gradle build file in case the dependencies prove useful. You can start the with the web-starter-boot from spring but our dependancies just list what is needed for Spring REST to work.
We hope this is a useful starter for getting Spring REST up and running on the liberty buildpack, as we said before we don’t see a huge difference between Spring and JAX-RS in terms of ease of development it was more a developer familiarity choice. Moving our code to spring certainly allows us to take advantage of the other areas of usefulness that spring provides.