Adams Bros Blog

28Jan/163

Spring Building a RESTful Web Service – revisited

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.

We won't be using gradle, as I personally think it's a really bad tool.

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.

        <!-- Required for WAR based deployments-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
Filed under: Spring Leave a comment
Comments (3) Trackbacks (0)
  1. The spring documentation is much easier to read.

  2. This isn’t a guide to creating a RESTful web service in Spring. This is a guide to cloning a git repository and starting it. You’ve presented this article as an alternative to Spring’s allegedly terrible documentation and yet anyone reading this would have to still read Spring’s documentation to have any idea what you’re summarizing here.

    • If you’re referring to brand new Java developers, sure, that may be the case.

      But for a junior developer with even a bit of experience, if you read this blog post, and follow it through, you’re up and running and understand how it works within minutes. The spring docs on the other hand go into extremely verbose descriptions that are wordy as can be, to the point where it’s 7 printed pages if you wanted to print it. Verbosity is not a good thing when it comes to documentation.


Leave a comment

 

No trackbacks yet.