Week 4 - Rest APIs

Java Annotations

Introduction to REST

Spring Boot Setup

Writing Endpoints

Message Formats

Input Validation

Practice

Assignment

Back end Track

Introduction

It is time to build something. In this chapter, you will set up your first Spring Boot project: a ready-to-run web application.

Spring Boot removes much of the manual server and framework configuration that older Java web applications needed. That means you can focus on writing API logic. By the end of this page, you will have a running server that responds to HTTP requests, ready to be build on in Writing Endpoints chapter.

What is Spring Framework?

Spring is a comprehensive Java framework for building applications. It provides core features like:

Spring is powerful, but older Spring projects often required a lot of manual configuration: XML files, bean definitions, external server setup, and many decisions before you could write your first endpoint. That flexibility is useful in large systems, but it can feel heavy when you are getting started.

That is where Spring Boot comes in.

Spring Boot

Spring Boot is an opinionated* layer built directly on top of the Spring Framework to dramatically simplify the process of creating, configuring, and running production-ready Java applications.

<aside> 💡

Opinionated means Spring Boot makes sensible default choices for you: which server to use, how to configure JSON serialization, where to scan for components, and how to start the application. You can override these defaults when needed, but you often do not have to.

</aside>

Spring Boot uses auto-configuration based on the dependencies you add. If your project includes the web dependency, Spring Boot can configure an embedded web server, Spring MVC, JSON handling, and other pieces that a REST API needs.

This is why a Spring Boot application can start with a small main() method and a single @SpringBootApplication annotation. You’ll see that shortly.

Starting a Spring Boot Project

A typical Spring Boot project is a standard Java application with a few established conventions. We will use Spring Initializr to generate the project, but it helps to understand what we are asking it to create.

Let’s go through our requirements in reverse. We want to build a REST API that:

We have a checklist, let’s take a look how the Spring Initializr tool helps us generate all of it!

Generate using Spring Initializr

Spring Initializr is a tool for generating a starter Spring Boot project. It creates the directory structure, build configuration, application class, and adds the dependencies you need for a specific feature (Eg: Spring Web, Lombok, etc…).

Spring Initializr is available at start.spring.io.

https://www.youtube.com/watch?v=gJrjgg1KVL4&t=715s

<aside> 💭

Watch only from 11:55 to 13:44 to see the Spring Initializr UI flow.

Please ignore the part about doing this within IntelliJ for now. You can use that as a shortcut later as long as you know what it’s a shortcut for.

</aside>

Now we can generate our very first project!

image.png

Once you have selected the options, click Generate. This downloads a .zip file.

Extract the .zip file. Then continue with the import steps below.

Importing into IntelliJ IDEA

Now that you have the generated project on disk, import it into IntelliJ:

  1. Open IntelliJ IDEA

  2. Go to File → Open

  3. In the File Dialog, find the unzipped project folder and select the root folder. The root folder is the one containing pom.xml

  4. With the root folder selected, click Open

    Root folder “WeekFourRestAPI” is selected

    Root folder “WeekFourRestAPI” is selected

  5. If IntelliJ IDEA asks whether to trust the project, click Trust Project

    image.png

<aside> ⚠️

</aside>

In Unit Testing with JUnit , you added JUnit to pom.xml manually and reloaded Maven. This time, Spring Initializr already added JUnit and other useful dependencies for you.

Maven reads pom.xml and downloads the dependencies. On first import this is done automatically. Later, adding or changing dependencies, make sure to reload the Maven project. You get popup for that in IntelliJ when you change pom.xml :

Maven reload popup

Maven reload popup

What are the Spring Web dependencies added?

Spring Web is a feature dependency. It brings in the pieces your application needs to be web-ready:

<aside> ⚠️

</aside>

Project Structure Verification

Below is the folder structure created by Spring Initializr for your Spring Boot project.

📂 WeekFourRestAPI/
├── 📂 src/
│   ├── 📂 main/
│   │   ├── 📂 java/
│   │   │   └── 📂 net/hackyourfuture/WeekFourRestAPI/
│   │   │       └── 📄 WeekFourRestApiApplication.java
│   │   └── 📂 resources/
│   │       └── application.yaml
│   └── 📂 test/
│       └── 📂 java/
│           └── 📂 net/hackyourfuture/WeekFourRestAPI/
│               └── 📄 WeekFourRestApiApplicationTests.java
├── 📄 pom.xml
├── 📄 HELP.md     # Helpful documentation by Spring with useful commands and tips
├── 📄 .gitignore, .gitattributes
└── 📄 mvnw, mvnw.cmd

The main class and @SpringBootApplication

In WeekFourRestApiApplication.java , you have the runnable main class:

package net.hackyourfuture.WeekFourRestAPI;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class WeekFourRestApiApplication {

	public static void main(String[] args) {
		SpringApplication.run(WeekFourRestApiApplication.class, args);
	}

}

@SpringBootApplication is a meta-annotation. That means it combines multiple annotations into one convenient annotation.

The three important annotations included by @SpringBootApplication are: