REST Web Services Bascis

Basics of REST with Spring

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.

  • Shopping Cart Project: We will use Shopizer API for this.
  • Application Startup, Build and Deployment.
  • Project Configurations.

 

 

1. Shopping Cart Project

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.

  • Ability to get all products based on the store/ site.
  • Get Product details by product ID.
  • Add product to Shopping Cart.
  • Register New User
  • Update User by user id. 
  • Customer Shopping Cart.

 

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.

 

 

2. Application Startup, Build and Deployment

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.

 

2.1 Creating an Executable jar

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.

 

2.2 Embedded Web Server Deployment Tomcat

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

  • Enable HTTPS for REST API.
  • Configure server port
  • Add HTTPS certificate.

Read  Spring Boot Web Application Configuration for more detail.

 

2.3 Embedded Jetty Server

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

  • We excluded spring-boot-starter-tomcat to ensure only one embedded server is available for our application.
  • Jetty server was added using spring-boot-starter-jetty

 

 

2.4 Standalone Application Server

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.

  • Change application packaging from jar to war in your pom.xml.
  • Remove spring-boot-maven-plugin from the pom.xml.
  • Need to add web entry point (We will be using Servlet 3 Java configuration)

 

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;

}

 

 

3. Project Setup

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.

 

Summary

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.

Comments are closed.