1. Introduction

In this post, we will develop REST APIs using Spring Boot Application. We will learn how to develop REST APIs for CRUD operations using RestController mappings.

You will learn

  • How to develop a RestController under Spring environment.
  • How to develop a service under Spring environment to manage application data.
  • Developing REST mappings to perform CRUD operations.
  • Using default logging available in Spring Boot environment.

2. Preparing Data Management Service

2.1. Creating a Bean

Let’s create a Java bean that will be used to store and process data.

public class Item {

    private final long code;
    private final String name;
    ...
}

2.2. Creating a Service

Let’s create a service that manages the application data. As you can see, we used @Service annotation to create the service. ItemManager service contains a simple implementation of performing CRUD operations over items(Map).

@Service
public class ItemManager {

    private Map<Long, Item> items;
    ...

    public void addItem(long code, String name) {
        items.put(code, new Item(code, name));
    }

    public void deleteItem(long code) {
        items.remove(code);
    }

    public Item getItem(Long code) {
        return items.get(code);
    }
}

Currently, we are using a Map collection for storing items data. You can replace this service to store data into a database.

3. Creating a RestController

Now we need a controller that will read/write JSON as input/output. Let’s get an instance of ItemManager from Spring Context using @Autowire annotation.

@RestController
public class ItemController {

    @Autowired
    private ItemManager itemManager;
}

If the above example we are using @RestController to define the controller. It means all the mapping under this controller will be treated as a REST API.

Let’s create mappings to handle CRUD operations.

public class ItemController {

    ....

    @GetMapping("/getItems")
    public List<Item> getItems() {
        return itemManager.getItems();
    }

    @GetMapping("/getItem/{code}")
    public Item getItem(@PathVariable Long code) {
        ....
        return item;
    }

    @PostMapping("/saveItem")
    public Item saveItem(@RequestBody Item item) {
        ....
        return item;
    }

    @DeleteMapping("/deleteItem/{code}")
    public Long deleteItem(@PathVariable Long code) {
        ....
        return code;
    }
}

In the above code, we are using @GetMapping, @PostMapping, @DeleteMapping to indicate the HTTP method used to handle the client request.

4. Using Default Logger in Spring Boot

We can easily configure different loggers supported by Spring Boot. To make things simpler let’s use the default logging available in Spring Boot environment.

....

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@RestController
public class ItemController {

    Logger logger = LoggerFactory.getLogger(ItemController.class);

    ....
}

We are using slf4j implementation of Logger and LoggerFactory classes.

5. Calling REST APIs from a REST Client

Let’s use a REST client to test all APIs. I am using Postman to call all APIs. Following is a screenshot of saving item using Postman REST client.

REST Client POST

We are using the default port (8080) to run our application. Read this article to change the embedded server port in Spring Boot Application.

6. Conclusion

In this post, we learned how to develop REST APIs using Spring Boot Application. We covered CRUD examples using RestController mappings. You also saw how to use default logging environment available in Spring Boot.

The complete source can be found over on GitHub.

Developing REST APIs using Spring Boot

Satish Pandey

I am an expert Java Spring Angular developer with 10+ years of rich and varied experience in developing end-to-end Web Applications. I maintain this blog and publish articles in my free time to help the community. Email: satish@cloudtechpro.com

Leave a Reply

avatar
1024
  Subscribe  
Notify of