Create a simple Events API with Spring Boot in Java

In this tutorial we look at creating a simple API using Spring Boot. The first thing we do is download an IDE to create the application. We download Spring STS IDE. To get started head to the download page by clicking here.

We need to add some dependencies to our project before we can continue. We use maven has our dependency manager so we add some elements to the pom.xml file.


Above we set the springframework as the parent application.


Above we add the spring framework dependency.

I should note I’m following a video course so the versions of Spring I’m using might be outdated but it works just as fine. However Spring has an great guide on RestControllers you can find it here.


Lets look at the folder structure of the application we will be working with.

Folder Structure

Our is the bootstrap file of our application. In the static method we start up the application. You will notice @SpringBootApplication and other @ methods. These are called annotations and are are used a lot to add additional features to our application.

public class App 
    public static void main( String[] args )
    {, args);

Spring uses simple Java objects to create the application. So we create a Event class to as a model to manage our data. Our Event class will have getters and setters for all its attributes.

public class Event {

	private String id;
	private String name;
	private String details;
	private String venue;
	private String eventDate;
	private String eventTime;
	private String author;

Next we create a controller. This holds all our application logic.

Our Controller is a POJO with some annotations that makes it into an awesome JSON web service.

public class EventController {


We add @RestController which does most of the work in the background to create our API. We also have @RequestMapping() which points to our API base url.

Lets look at the functions in our controller. We have functions for our rest actions being GET, POST, PUT and DELETE.

The GET function is show below. It is named list() and returns a list of Event objects. The URL will look like localhost:8080/app/v1/events

@RequestMapping(value="events", method= RequestMethod.GET)
public List<Event> list(){
  return events;

We use the this.createAllEvents() function to create some mock events.

We using POSTMAN to do our testing. We can see the GET call using the list() function in action.

Getting all the events
public List<Event> events = new ArrayList<Event>();

Above we have the global events list that we added to to create our mock list data.

We use a global list of events to mock our data. For this example we didn’t connect to a data source so we need mock data.


The GET by Id is show below. A sample URL will look like localhost:8080/app/v1/events/1. The function we use is called get().

@RequestMapping(value="events/{id}", method= RequestMethod.GET)
	public Event get(@PathVariable long id) {
		Event event1 = new Event();
		event1.setVenue("Port of Spain");
		event1.setName("Birthday Party");
		event1.setDetails("My Birthday party");
		event1.setAuthor("Wynton Frankin");
		event1.setEventDate("28 Feb 2016");
		return event1;

In POSTMAN we can see the get() function in action.

Getting an EVENT by id

We can add an event using the PUT request. The update() function is shown below.

	public Event update(@PathVariable Long id, @RequestBody Event event) {
		Event event1 = new Event();
		return event1;

In POSTMAN we test to see if it works.

Putting in some event data using POSTMAN

Finally we have the DELETE request.

@RequestMapping(value = "events/{id}", method = RequestMethod.DELETE)
	public String delete(@PathVariable Long id) {
		return "Removed Event " + String.valueOf(id);

In POSTMAN when we run this function it returns “removed event” with the id of the requested event to remove.

DELETE request with POSTMAN

We are now finish.

We have just created a simple JSON web service. Next we connect to a database and then we will try to upload this to a production server to see how that works.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s