I do frequently find that Spring documentation reads more like a novel than it does technical documentation. I find you can sometimes take many minutes, or even hours, just wading through stuff to find out how to do something that should have taken 5-10 minutes. Spring’s REST framework is relatively straight forward to use, but there doesn’t seem to be a good quick start on it’s use.
So, we endeavour to have you up and running with their “Building a RESTful Web Service” tutorial in under 5 minutes, assuming you have a basic java development environment going, meeting their requirements. Please quickly review the first two “What you’ll build” and the “What you’ll need” sections at “Building a RESTful Web Service“, then come back here.
We have one additional requirement. It’s assumed you’re able to develop from a Linux command line. If you’re not using Linux as a development platform, you really should be.
git clone https://github.com/spring-guides/gs-rest-service.git cd gs-rest-service/complete mvn clean install java -jar target/gs-rest-service-0.1.0.jar # on a separate terminal curl http://localhost:8080/greeting |
The key components of this tutorial, which are quite self explanatory with a simple explanation, are…
- Application.java – starts the application with an embedded tomcat server
- GreetingController.java – binds the REST url and handles the request
- Greeting.java – serializable object for the framework to return JSON
- pom.xml
- spring-boot-start-parent – a dependency management parent project
- spring-boot-starter-web – includes things like the mvc framework and the rest framework
- For a WAR working in an existing tomcat instance…
- include spring-boot-starter-tomcat dependency
- add a pom “packaging” element with a value of “war”
- make your Application class extend SpringBootServletInitializer
Example for the spring tomcat starter dependency.
org.springframework.boot spring-boot-starter-tomcat provided