Step-by-step Spring Boot RESTful web services example in Java using STS –

In a previous article about important RESTful principles and how to create a beautiful RESTful API, I used the example of a web service that kept track of an ongoing, global score for an online rock-paper-scissors application. In this step-by-step Spring Boot RESTful web services example, we’ll implement that scenario using the Spring Web APIs and the Eclipse based SpringSource Tool Suite (STS) as the IDE. 
The RESTful score service will provide the following functions:
RESTful clients can find out the current number of wins, losses and ties through a GET request to the following URL:
This invocation will return a JSON string in the following format:
RESTful clients can update the score by invocating the same URL through a PUT invocation while also passing query parameters. Again, the program will return a JSON-based representation of the score after the update. A better design would be to use JSON as the payload, but query parameters are used here for the sake of simplicity.
With the RESTful API, the user cannot directly set the number of wins, losses or ties. The client can only use a POST invocation to increment values one at a time. This is not an idempotent function, so it is handled through a POST. In this case, a simple number is returned to the program as opposed to a JSON string.
You can use a simple GET invocation on the appropriate resource locator to obtain the number of wins, losses or ties:
This step-by-step Spring Boot RESTful web services example in Java has very few prerequisites, but it does assume that you have the SpringSource Tool Suite (STS) installed. If that’s the case, you are ready to jump on the fast track to developing RESTful web services with Spring.
The first step in this exercise is to create a new Spring Boot project named restful-spring-example that uses both the Web and DevTools features. This is accomplished by kicking off the Spring Starter Project wizard in Eclipse. Select the Spring Web and DevTools options and click Finish on the Spring Boot project wizard.
The next step is to open the restful-spring-example project and create two classes: and
Right click on the com.mcnz.restful.spring.boot package and choose to create a new class named Score. The Score class is used to keep track of the global number of wins, losses and ties that occur in the online rock-paper-scissors game. The Score class will maintain the number of wins, losses and ties as static variables of type int. State should never be held within a RESTful web service, so this class represents the externalization of state.
After you code the Score class, create a second Java component named ScoreService. We’ll use the ScoreService class to provide access to — and the ability to manipulate — the Score. The first iteration of the class used in this Spring Boot web service example will have getter methods that return the number of wins, losses and ties, along with update methods that can increment the number of wins, losses and ties by one.
Here is the first iteration of the ScoreService class before we decorate it with RESTful annotations and subsequently configure it as a Spring Boot web service.
With the first iteration of the ScoreService component coded, the next step is to transform it into a Spring Boot RESTful web service by decorating the class declaration with the @RestController annotation. The @RestController annotation informs the Spring Boot framework that the class contains methods that will be invoked through a web-based resource URL.
Step four is to add @RequestMapping annotations to methods. These annotations define the HTTP method used along with the structure of the resource URLs that will be used to invoke them. In the previous tutorial about how to create beautiful RESTful APIs, we established that we would invoke the update methods through a POST, and all getter methods sensibly through a GET invocation. The URLs are all resource based, so use of the URL path is /score/wins to get information about the number of wins and the path /score/ties to increase the number of ties.
Fully decorated with Spring Boot RESTful web service annotations, the next iteration of the ScoreService class looks as follows:
This step-by-step Spring Boot RESTful web services example needs to generate a JSON-based response for the client. There are excellent frameworks like Jackson and GSON, which you should use in larger projects, but for this simple RESTful web services example, we will simply employ some Java String manipulation to generate the JSON.
The getScore() method needs to return a JSON string formatted in the following manner:
Here is how the un-annotated getScore() method looks when coded to return a JSON string:
Note that you’ll use this same pattern match and String format approach again to return a JSON-based representation of the score in the updateScore method.
Since the getScore() method now returns JSON, you need to add a new produces attribute to the @RequestMapping annotation. The fully annotated getScore() method that indicates that JSON will be returned to the calling program looks as follows:
In order to generate JSON, the updateScore method follows a similar pattern of updated annotations and String manipulation within the code:
To update the ScoreService, the client will use the /score path while it passes query parameters to represent the number of wins, losses and ties. For example, a request to update the number of wins to 99, ties to 88 and losses to 7 would look like this:
As long as the updateScore method has method parameters that match the names of the query parameters, the Spring Boot web services framework will assign the queryParam values to the correspondingly named method signature parameter.
The updateScore method will take the values passed into the method and assign them to the corresponding properties of the ScoreService class. An updated JSON string will be sent to the client indicating the new, updated value of the score. You’ll use the same process used to generate JSON in the getScore()method here. The full method implementation is as follows:
With the updateScore method coded, the step-by-step Spring Boot RESTful web services example is code complete, and the only steps left are to test and deploy.
To run the application, simply right click on the restful-spring-example project and choose Run As → Spring Boot App and Maven will package the application and deploy it to the STS server. When the console window indicates the Tomcat-based server has finished starting, open a browser window and go to localhost:8080/score/wins. A value of zero will appear.
To test the increaseWins method, run the following two CURL command in a Bash shell:
The output of the second command is:
Now trigger a PUT invocation, with query parameters being used to carry the payload:
This results in an output of:
And that’s it, a complete, step-by-step Spring Boot RESTful web services example that creates a fully functional Spring Boot application.
Now there is plenty of room for improvement and expansion when it comes to this step-by-step Spring Boot RESTful web services example.
Obviously, using the static variables in the Score class is a poor way to manage state, so at the very least we should offload the job of state management to a JavaBean with an ObjectOutputStream. And neither a JavaBeans or RESTful web service should be responsible for data persistence, so there will be opportunities to integrate Hibernate, JPA and Spring Boot web services. We could also invoke the Spring Boot RESTful web service example through a Spring MVC app or a Spring RestTemplate instead of CURL, and of course, it would be nice to package this app in an executable JAR file with an embedded Tomcat or embedded WebSphere Liberty server and deploy it as a microservice. But all of those things would complicate the example and distract from the core purpose, which is to demonstrate just how easy it is to develop a RESTful Spring web service using the Spring Boot framework.
These aforementioned issues will be addressed in subsequent tutorials. The source code for this step-by-step Spring Boot RESTful web services example can be found on GitHub.
While they’re both central to inter-application communication processes, it’s important to understand the fundamental differences…
While the code-first approach certainly works, its appeal is fading as architectures become increasingly complex webs of software…
Many enterprise-level architects rely on an object-oriented approach, but there are good reasons and practical ways to introduce …
Chaos engineering tool options include the original (Chaos Monkey), open source projects like Chaos Toolkit and Chaos Mesh and …
Docker Desktop is now part of all Docker subscriptions, including a new Business tier with added security, but enterprise users …
Any Agile team that wants its team’s culture to be squarely centered on improving software quality, should consider the following…
While it’s inevitable that certain cloud-hosted applications will share resources, the vulnerabilities these connections expose …
A poorly executed cloud resource tagging strategy, or having no strategy at all, can cost a bundle down the road. Build the best …
Follow along to set up Google Cloud Operations’ Monitoring and Logging services, try Google’s Monitoring Query Language and …
The REvil decryptor key helps victims recover their encrypted files, as long as the attacks were made before July 13, which is …
ExpressVPN said it will not cut ties with CIO Daniel Gericke, who was implicated by the DOJ in state-sponsored hacking on behalf …
Cybersecurity isn’t just IT’s responsibility. Use these seven tips to build a security culture where employees and IT work …
Think you’re ready for the AWS Certified Solutions Architect certification exam? Test your knowledge with these 12 questions, and…
Amazon said its van monitoring system is designed solely for driver safety. But many industry experts have concerns regarding the…
Amazon would like to strengthen its global footprint, but the e-commerce giant faces roadblocks and challenges today that did not…
All Rights Reserved, Copyright 2000 – 2021, TechTarget

Privacy Policy
Cookie Preferences
Do Not Sell My Personal Info