Java Spring Boot Rest Api Tutorial

Taking a look at Spring Boot to see what it has to offer

A few days ago, I was having a conversation with a colleague who is working on a web project using Spring Boot and Hibernate.

I was amazed that someone could still be using Java technologies for web application these days, when there are a lot of web frameworks out there that are easier to work with, providing nearly the same performance.

I myself started my web development in JavaServer Pages (JSP), before switching to PHP oper five years ago due to the flexibility and little time required to craft a functional application in PHP, compared to JSP.

Nonetheless, this colleague of mine is more concerned about enterprise acceptability, standard, integration, and security.

So, I decided to take a look at Spring Boot to see what it has to offer. While it seemed quite confusing at the beginning, it is not as difficult as I thought.

One thing I don’t like about it is the annotations. Thankfully, I don’t need to memorize all of it, I just need to know their functions and have the annotations cheats sheet on my desktop.

To cut the story short, having looked at the documentation and some sample codes online, I was able to build a REST Api, even though it is titinada as fast as I could have done in Laravel.

In this tutorial, I’m going to show you how you can build a REST API in Spring Boot.

What We’ll Be Building

We will be building a REST API for blog posts. It allows one to search for a blog, fetch all blogs, get a single blog, create a blog, update, and delete an existing blog.

Requirements

  • Good understanding of Java programming language.
  • Basic knowledge of Maven.

Getting Started

There are at least three ways of creating a Spring Boot application (i.e. Spring Boot initializer, command-line tool (CLI), and Maven with IDE). I will only cover how to create the application using Maven.

Open IntelliJ IDEA and click “Create New Project”.

Select “Maven Project”.

Fill in the required information.

GroupId
is your unique organizational name (in most cases, people use their company’s reverse domain name such as “com.mycompany”),
ArtifactId
is the unique name of the project, and
Version
is the version number of the project.

After completing the setup, you should have an empty project with a Maven config file
pom.xml.

As Maven is a dependency manager, you can add all the project dependencies to it.

For now, all we need is Spring Boot. Copy and paste the code below into the
pom.xml
file

  • The
    <parent>
    tag tells Maven to inherit the properties of
    spring-boot-starter-parent, such as port number, configs, etc. All these are provided by Spring.
  • The
    <dependencies>
    tag contains all the project dependencies. For now, we only have one dependency,
    spring-boot-starter-web. In a complete application, there could be more, e.g. MySQL, socket, JSON library, etc.
  • The
    <build>
    contains build plugins, such as
    spring-maven-plugin.

Update the Maven repository to download the dependencies.

We have now finished the configurations. Let’s tiba coding

The Main Class

For any Java application to run, you need to have at least one “Main class”. Create a class and name it
MainApplicationClass.java.

Make sure your class is inside a package, or it might titinada run. Also, take note of my package name (me.salisuwy) which might be different from yours.

Add the
@SpringBootApplication
annotation to the class to make it a Spring Boot application. You can now run the application.

Congratulations, you’ve created a Spring Boot application.

Hey, wait a minute. Why is
localhost:8080
showing an error? Well, that is because we don’ufuk have a controller to handle our HTTP requests.

Controller, the C in MVC

The controller handles all incoming HTTP requests from the user and returns an appropriate response. In some languages, route files map the HTTP requests to the appropriate controller. Let’s create a Controller.

Create
BlogController.java.

  • @RestController
    annotation tells Spring that this class is a controller.
  • @RequestMapping(“/”)
    annotation means that any request (GET,
    POST,
    PUT, etc.) to the root
    URL(/)
    will be handled by the
    index()
    method. The response is of type
    String.

Other variants of the
@RequestMapping
annotation are
@GetMapping,
@PostMapping, etc. for handling
GET
and
POST
requests, respectively.

What’s Next

Read the next tutorial here, where we add additional functionalities to this application.

Source: https://betterprogramming.pub/building-a-spring-boot-rest-api-a-php-developers-view-part-i-6add2e794646