Log in
Sign up
Documentation
  • - Quick start guides
  • - How Jumpydoll works
  • - Tutorials
  • - Java/Spring Boot tutorial
  • - Step 1: Introduction
  • - Step 2: Setting up the project
  • - Step 3: Building the first API endpoint
  • - Step 4: Storing data in MongoDB Atlas
  • - Step 5: Adding more API endpoints
  • - Step 6: Creating a user interface
  • - Step 7: Conclusion

Step 3: Building the first API endpoint - Java/Spring Boot tutorial

  • Creating the Rest controller
  • Building a Task class
  • Returning data

In this step, you will create a class to represent data and add an API endpoint that returns data.

Creating the Rest controller

The current application returns the static files in the public directory. However, to return dynamic data, you’ll need to add a REST API. Spring uses a component called a Rest controller to handle dynamic requests.

To create your rest controller, in the com.jumpydoll.app.[user].jumpydolltodolist, create a new package called controller. This package will contain your controller class.

In this package, create a new class called TodoController.java. In this class, paste the following:

src/main/java/com/jumpydoll/app/[user]/jumpydolltodolist/controller/TodoController.java

package com.jumpydoll.app.[user].jumpydolltodolist.controller; 
 
import org.springframework.http.HttpStatus; 
import org.springframework.http.ResponseEntity; 
import org.springframework.web.bind.annotation.GetMapping; 
import org.springframework.web.bind.annotation.RestController; 
 
@RestController 
public class TodoController {
    @GetMapping(value = "/api/tasks")
    public ResponseEntity<?> getTasks() {
        return new ResponseEntity<String>("response", HttpStatus.OK);
    }
}

This class defines the first API endpoint. When the service receives an HTTP GET request to the /api/tasks URL, it creates a response with the data you specify and returns that to the caller. You can visit http://localhost:8080/jumpydoll-todo-list/api/tasks, and you will see the string “response” that you returned in the getTasks() method. Now that you know how to create an API endpoint, we’ll create an object for your application to represent and return data consistently.

Building a Task class

An item in a to-do list can be represented by an object. It should be able to store information such as the description of the task, the time the task was created, and whether the task has been completed yet. You can create an object that stores this information.

In the com.jumpydoll.app.[user].jumpydolltodolist package, create a new package called model. This package will contain your classes that model data.

In this package, create a new class called Task.java. Add the following fields:

String id;
String description;
Long createTime;
boolean finished;

Add getters and setters for all fields, and add a constructor containing all four fields as inputs. Your final class should look like this:

/src/main/java/com/jumpydoll/app/[user]/jumpydolltodolist/model/Task.java

package com.jumpydoll.app.[user].jumpydolltodolist.model;

public class Task {
    String id;
    String description;
    Long createTime;
    boolean finished;

    public Task(String id, String description, long createTime, boolean finished) {
        super();
        this.id = id;
        this.description = description;
        this.createTime = createTime;
        this.finished = finished; 
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public long getCreateTime() {
        return createTime;
    }

    public void setCreateTime(long createTime) {
        this.createTime = createTime;
    }

    public boolean isFinished() {
        return finished;
    }

    public void setFinished(boolean finished) {
        this.finished = finished;
    }
}
Returning data

You can then update the getTasks method in the TodoController to return an example item. For example, change the getTasks method to

@GetMapping(value = "/api/tasks")
public ResponseEntity<?> getTasks() {
   Task task = new Task("1", "Build the todo app", System.currentTimeMillis(), false);
   List<Task> todoList = new ArrayList<>();
   todoList.add(task);
   return new ResponseEntity<>(todoList, HttpStatus.OK);
}

Run the application again, and go to http://localhost:8080/jumpydoll-todo-list/api/tasks and you’ll see a result like this:

[{"id":"1","description":"Build the todo app","createTime":1654744520522,"finished":false}]

You have just added your first API endpoint to the service and created a model to return data. However, this is still just static data. Next, you will connect your Task model with a database so you can store data long-term.

Step 2: Setting up your projectStep 4: Storing data in MongoDB Atlas
Try Jumpydoll and host your own project

• Sign up with a GitHub account
• No setup required - start right in your browser
• Start your first project in 5 minutes
Get started for free

Jumpydoll

About

Project Gallery

Sign up

Contact

Email: [email protected]

© 2022