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.
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.1.RELEASE</version>
</parent>
Above we set the springframework
as the parent application.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
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.
NB
Lets look at the folder structure of the application we will be working with.

Our App.java
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.
@SpringBootApplication
public class App
{
public static void main( String[] args )
{
SpringApplication.run(App.class, 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.
@RestController
@RequestMapping("app/v1/")
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(){
this.createAllEvents();
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.

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.
NB
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.setId(String.valueOf(id));
event1.setVenue("Port of Spain");
event1.setName("Birthday Party");
event1.setDetails("My Birthday party");
event1.setAuthor("Wynton Frankin");
event1.setEventDate("28 Feb 2016");
event1.setEventTime("10:00am");
return event1;
}
In POSTMAN we can see the get()
function in action.

We can add an event using the PUT request. The update()
function is shown below.
@RequestMapping(value="events/{id}",method=RequestMethod.PUT)
public Event update(@PathVariable Long id, @RequestBody Event event) {
Event event1 = new Event();
event1.setId(String.valueOf(id));
event1.setVenue(event.getVenue());
event1.setName(event.getName());
event1.setDetails(event.getDetails());
event1.setAuthor(event.getAuthor());
event1.setEventDate(event.getEventDate());
event1.setEventTime(event.getEventTime());
return event1;
}
In POSTMAN we test to see if it works.

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.

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.