In the previous post, we learned to build REST API using Spring Boot. Let’s discuss some of the Basics of REST with Spring. We will be covering following topics.
Let’s build a real REST API in our course for understanding fundamentals of REST API. We will be building a shopping cart REST API with below features.
We will be using Shopizer to build above REST API. Shopizer is a complete e-commerce platform with ready to use features for build e-commerce web applications. Our goal is to build Spring based REST API on the Shopizer platform.
Our REST API is based on Spring and Spring Boot. Let’s discuss the process of deploying Spring boot REST application.Spring Boot comes with many built-in features which include flexibility in deploying Spring Boot Applications.
Spring Boot provides a feature for creating self-contained executable jars which can run standalone in production.An executable jar contains all compiled files along with required dependencies to run our code.
For creating jar, add spring-boot-maven-plugin to the pom.xml file
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
Run mvn package command to create jar file under the target directory. Type java -jar target/myproject-0.0.1-SNAPSHOT.jar command to run Spring Boot application.
We added spring-boot-starter-web in our application. When we run our application, Spring Boot will detect Spring MVC in the classpath and startup embedded Apache Tomcat server.We can configure and customize embedded tomcat using application.properties file
Read Spring Boot Web Application Configuration for more detail.
If you like to use Jetty as the embedded server, you need to add spring-boot-starter-jetty to your pom.xml.Spring Boot will automatically delegate work to Jetty. Here is the updated POM for Embedded Jetty server.
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
There are few things which are interesting in above configuration
What if you like to deploy your REST application on an existing Tomcat instance or to Java EE application servers like Apache Tomcat EE, WildFly or Websphere? You need to make only a few changes to make sure your application is ready for the deployment of these servers.
To enable our application, we will add ServletInitializer class. On a high level, our class will look like something
package com.example.demo;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;
public class CustomWebApplicationInitializer extends SpringBootServletInitializer {
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(applicationClass);
}
private static Class applicationClass = DemoApplication.class;
}
Please read our previous article Building Restful Web Services to get an understanding of the project structure in Spring Boot. In addition to this structure, we will start adding extra components and libraries to this structure. If you are starting with this post, I highly recommend reading our last post to start with.
In this post of Building REST services with Spring, we covered Basics of REST with Spring. We discussed the REST application which we will be creating during this course. We also covered some of the basics of deploying our Spring Boot REST application. In our next article, we will learn how to validate REST API data.
Hello!! Welcome to the Java Development Journal. We love to share our knowledge with our readers and love to build a thriving community.