DEV Community

Alex Merced
Alex Merced

Posted on

How to build a Java Spring JSON API from a blank maven project

Step 1: Set Up the Maven Project

Create a new Maven project using your preferred IDE or by using the following command in your terminal:

mvn archetype:generate -DgroupId=com.example -DartifactId=todocrud -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
Enter fullscreen mode Exit fullscreen mode

Navigate to the project directory:

cd todocrud
Enter fullscreen mode Exit fullscreen mode

Open the pom.xml file and add the Spring Boot and PostgreSQL dependencies. Here's an example:

    <!-- Spring Boot Starter -->

    <!-- Spring Boot Starter Data JPA -->

    <!-- PostgreSQL Driver -->
Enter fullscreen mode Exit fullscreen mode

Save the pom.xml file and let Maven resolve the dependencies.

Step 2: Configure the Database

Create a PostgreSQL database and remember the connection details (URL, username, and password).

Open the src/main/resources/ file and configure the database connection:

Enter fullscreen mode Exit fullscreen mode

Step 3: Create the Model

Create a package called com.example.todocrud.model.

Inside the model package, create a class named Todo to represent your model:

package com.example.todocrud.model;

import javax.persistence.*;

@Table(name = "todos")
public class Todo {
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String task;
    private boolean completed;

    public Todo() {

    public Todo(String task, boolean completed) {
        this.task = task;
        this.completed = completed;

    public Long getId() {
        return id;

    public void setId(Long id) { = id;

    public String getTask() {
        return task;

    public void setTask(String task) {
        this.task = task;

    public boolean isCompleted() {
        return completed;

    public void setCompleted(boolean completed) {
        this.completed = completed;
Enter fullscreen mode Exit fullscreen mode

Step 4: Create the Repository

Create a package named com.example.todocrud.repository.

Inside the repository package, create an interface named TodoRepository:

package com.example.todocrud.repository;

import com.example.todocrud.model.Todo;
import org.springframework.stereotype.Repository;

public interface TodoRepository extends JpaRepository<Todo, Long> {
Enter fullscreen mode Exit fullscreen mode

Step 5: Create the Controller

Create a package named com.example.todocrud.controller.

Inside the controller package, create a class named TodoController to handle API endpoints:

package com.example.todocrud.controller;

import com.example.todocrud.model.Todo;
import com.example.todocrud.repository.TodoRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

public class TodoController {
    private TodoRepository todoRepository;

    public Todo createTodo(@RequestBody Todo todo) {

    public List<Todo> getAllTodos() {
        return todoRepository.findAll();

    public Optional<Todo> getTodoById(@PathVariable Long id) {
        return todoRepository.findById(id);

    public Todo updateTodo(@PathVariable Long id, @RequestBody Todo updatedTodo) {
        return todoRepository.findById(id)
                .map(todo -> {
                .orElseGet(() -> {

    public void deleteTodoById(@PathVariable Long id) {
Enter fullscreen mode Exit fullscreen mode

Implement the CRUD operations in the TodoController class using appropriate annotations (@PostMapping, @GetMapping, @PutMapping, @DeleteMapping) and methods.

Step 6: Run the Application

Run the Spring Boot application:

mvn spring-boot:run
Enter fullscreen mode Exit fullscreen mode

Access the API endpoints at http://localhost:8080/todos.

That's it! You've created a Java Spring CRUD JSON API from scratch with PostgreSQL integration using the "TODO" model. You can now define and implement the specific CRUD operations in the TodoController class and test your API using a tool like Postman or curl.

Top comments (0)