Lab9 - Building A Basic CRUD RESTful Spring Boot MVC Application
Lab9 - Building A Basic CRUD RESTful Spring Boot MVC Application
Lab Objectives
After completing this lab, you will be able to:
1. Using Recent project and Create structure project
2. Running previous project
3. Set up Spring MVC REST Controller
4. Create Shipwreck Model
5. Create Stubclass for Mock Data
6. Spring MVC Integration Overview
7. Summary
It describes that the different independent components of your application should communicate
with each other through simple HTTP/HTTPS calls. If an application satisfies this criterion it can
be considered as a RESTful web application.
Lab 9 : Building a Basic CRUD RESTful Spring Boot MVC Application: Getting Started With Java Spring -1
In a RESTful web application, you should utilize the different HTTP verbs for the CRUD
operations:
REST is the underlying architectural principle of the web. The main benefit of it is that the client
and the server can not know anything about each other. Therefore, you can use any client or server
technology, which can send, accept, and can respond to HTTP requests. This is the method that
shows how the different microservices can communicate with each other in Spring Cloud, which
we will talk about in a later article.
Lab Procedure
We will use Angular.js for the client side. Since this post is not about Angular but about how to
utilize Spring Boot and Spring MVC to serve REST requests, we will use an already created
source. This is a basic Angular client that showcases a good example for basic CRUD operations
by the theme of different shipwrecks. It's written by Dan Bunker, and I already merged its
resources to our source code, which I uploaded to a git repository. You can simply clone it from
here: spring-boot-demo-project.
In Spring Boot, there are default static content resources where it can natively serve us our content
files. These folders automatically added to the project's classpath:
/static
/resources
/public
I created a resources folder under /java/main in our starting application. Hence, we can reach them
by simply referencing to them by name in our browser.
Lab 9 : Building a Basic CRUD RESTful Spring Boot MVC Application: Getting Started With Java Spring -2
2. Running previous project
Lab 9 : Building a Basic CRUD RESTful Spring Boot MVC Application: Getting Started With Java Spring -3
However, if you click on the Shipwrecks link in the header and press F12, you will get the
following error in the browser's console:
That's because we didn't set up our backend Spring MVC Controller yet to serve the client API
calls.
One import thing I would like to mention before we go towards that is if you would like to change
static resources in Spring Boot, you do not need to restart the server. You can try it out pretty easily
Lab 9 : Building a Basic CRUD RESTful Spring Boot MVC Application: Getting Started With Java Spring -4
by updating something in the resources folder without restarting your running application. You
will see that the changes apply after you refresh your browser.
What we have done so far, and you can see in your browser, is the orange Client side from above.
To communicate with the backend server through HTTP/HTTPS calls, we should build our REST
API in our application. Fortunately, we can achieve it quite easily since Spring Boot provides the
REST functionality via the Spring MVC dependency. It is the entry point in and out of our server
for our web client. Hence, we can write our REST endpoints simply and rapidly.
Each endpoint follows the standard RESTful best practices by association with an HTTP verb with
a URL to handle requests. With these, we can achieve different resources needs, such as adding,
viewing, deleting, etc.
@RestController
@RequestMapping("api/v1/")
public class ShipwreckController {
@RequestMapping(value = "shipwrecks", method = RequestMethod.GET)
public List<Shipwreck> list(){
return ShipwreckStub.list();
}
@RequestMapping(value = "shipwrecks", method = RequestMethod.POST)
Lab 9 : Building a Basic CRUD RESTful Spring Boot MVC Application: Getting Started With Java Spring -5
public Shipwreck create(@RequestBody Shipwreck shipwreck){
return ShipwreckStub.create(shipwreck);
}
@RequestMapping(value = "shipwrecks/{id}", method = RequestMethod.GET)
public Shipwreck get(@PathVariable Long id){
return ShipwreckStub.get(id);
}
@RequestMapping(value = "shipwrecks/{id}", method = RequestMethod.PUT)
public Shipwreck update(@PathVariable Long id, @RequestBody Shipwreck shipw
reck){
return ShipwreckStub.update(id, shipwreck);
}
@RequestMapping(value = "shipwrecks/{id}", method = RequestMethod.DELETE)
public Shipwreck delete(@PathVariable Long id){
return ShipwreckStub.delete(id);
}
}
Here important to use this request mapping: @RequestMapping("api/v1/"), because of our client
reference to this path. This gives the base URL for all of our endpoints in our class.
The @RequestMapping value gives our endpoint name and the method says that this endpoint
receives only GET HTTP requests.
package springboot.my_first_application.model;
public class Shipwreck {
Long id;
String name;
String description;
String condition;
Integer depth;
Lab 9 : Building a Basic CRUD RESTful Spring Boot MVC Application: Getting Started With Java Spring -6
Double latitude;
Double longitude;
Integer yearDiscovered;
public Shipwreck() { }
public Shipwreck(Long id, String name, String description, String condition
, Integer depth, Double latitude, Double longitude, Integer yearDiscovered) {
this.id = id;
this.name = name;
this.description = description;
this.condition = condition;
this.depth = depth;
this.latitude = latitude;
this.longitude = longitude;
this.yearDiscovered = yearDiscovered;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getCondition() {
Lab 9 : Building a Basic CRUD RESTful Spring Boot MVC Application: Getting Started With Java Spring -7
return condition;
}
public void setCondition(String condition) {
this.condition = condition;
}
public Integer getDepth() {
return depth;
}
public void setDepth(Integer depth) {
this.depth = depth;
}
public Double getLatitude() {
return latitude;
}
public void setLatitude(Double latitude) {
this.latitude = latitude;
}
public Double getLongitude() {
return longitude;
}
public void setLongitude(Double longitude) {
this.longitude = longitude;
}
public Integer getYearDiscovered() {
return yearDiscovered;
}
public void setYearDiscovered(Integer yearDiscovered) {
this.yearDiscovered = yearDiscovered;
}
}
Lab 9 : Building a Basic CRUD RESTful Spring Boot MVC Application: Getting Started With Java Spring -8
5. Create Stubclass for Mock Data
Because we don't have any database connection yet, we should mock our data through a Stub
class. We wrote it as below:
Lab 9 : Building a Basic CRUD RESTful Spring Boot MVC Application: Getting Started With Java Spring -9
public static Shipwreck delete(Long id) {
return wrecks.remove(id);
}
}
Now, if you restart the application and click on the same Shipwreck item in the header, you
should see the stubbed shipwreck list.
Lab 9 : Building a Basic CRUD RESTful Spring Boot MVC Application: Getting Started With Java Spring -10
Here we are. If you check the different actions, they should work as well.
It is more about what is included in the appropriate jar file to our classpath. Because we had
@SpringBootApplication annotation, which contains @EnableAutoConfiguration, it
automatically set up the MVC features for us as well:
7. Summary
In this article, we successfully plugged in a front-end client to our basic Spring Boot application
and created RESTful endpoints that Angular can successfully talk to through Spring MVC in our
Spring Boot app.
Lab 9 : Building a Basic CRUD RESTful Spring Boot MVC Application: Getting Started With Java Spring -11