SpringPortal #1 - Get-ting started with Spring Boot
Published: 2022-11-27 17:15:59

---
title: SpringPortal #1 - Get-ting started with Spring Boot
published: true
date: 2022-10-20 10:26:28 UTC
tags: 
canonical_url: https://24naman.hashnode.dev/springportal-1-get-ting-started-with-spring-boot
---

# Why another Spring blog?

**Spring** is one of the most popular frameworks to build web applications using Java and there are tons of excellent guides & blogs online to get one started with it.

### So, why this new blog???

There are mainly two goals:

1. I want to document my spring journey.
2. I want to create short guides on various topics in the Spring framework for my future reference and help anyone who wants a concise introduction to the topic.

#### Availability of code from the blogs

- All code from this and hopefully several upcoming blogs will be available on [GitLab](https://gitlab.com/spring-hashnode).

- We'll use [VS Code](https://code.visualstudio.com/) for this journey. As a first step, we will create simple GET and POST APIs in Spring. 

* * *

## Setting up VS Code

- For creating a Spring project in VS Code, you'll need these two extensions:

* * *

## Creating Spring Project in VS Code

1. Open VS Code terminal using `Ctrl + Shift + P`.
2. Search for "Spring Maven" and select "Spring Initializr: Create a Maven Project...". ![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1666201796521/hvnDuB98M.png)
3. Select the default spring version and "Java" as the project language.
4. Type your desired group id and you can pretty much select all the default options.
5. At last select the following dependencies:
  1. Spring Boot DevTools
  2. [Lombok](https://projectlombok.org/)
  3. Spring Web

* * *

## Let's code

So, we have set up all the things we need, now code.

1. VS Code will provide a project similar to this:

2. Create three folders `api`, `service` & `entity` for separating our API and Service layers and to put our entity classes. ![Screenshot 2022-10-20 00:38:06.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1666206510440/1RkkCC0Fc.png)

3. Create a Person class to use in our POST API later.

4. Create a `PersonService` class in the service package.

5. Now to use this service class, we'll create a REST Controller to expose our APIs.

```
package com.naman.hellospring.api;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.naman.hellospring.entity.Person;
import com.naman.hellospring.service.PersonService;

@RestController
@RequestMapping("/person")
public class PersonApi {

    @Autowired
    PersonService personService;

    @GetMapping("/get")
    public ResponseEntity<Person> getPerson() {
        return ResponseEntity.ok(personService.getPerson());
    }

    @PostMapping("/add")
    public ResponseEntity<Person> addPerson(@RequestBody Person person) {
        return ResponseEntity.ok(personService.addPerson(person));
    }
}

```

- `@RequestMapping` map the class and its methods for our HTTP requests.
- `@GetMapping` will map the function to handle the `/person/get` request.
- `@PostMapping` will map the function to handle the `/person/add` request.
- [POST](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST) is an HTTP method that contains the data in its body.
- ResponseEntity class allow us to set headers, body, and status for our API response.
- `ResponseEntity.ok(personService.getPerson())` creates a response with the status code `200`.

## Now, bringing it all together.

- Install [Postman](https://www.postman.com/) for testing the APIs.
- **Testing POST API** - Sending the data to the server and logging it to the console.

- **Testing GET API** - Requesting Server for the same data.

## What's next

This was a brief introduction to API creation in the Spring framework. There will be more articles in the future covering various nooks and corners of the Spring framework.

* * *

## GitLab

[Code Repository](https://gitlab.com/spring-hashnode/hello-spring)

<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-2104033689576056"
     crossorigin="anonymous"></script>