A simple Restful CRUD Spring boot application (blog) – Part 1

Share This:

Hi it’s been a while since I wrote anything. So what would be the best way to start than make a simple CRUD (Create Read Update Delete) Restful application.

So lets start.

First you need to have installed Java JDK and Maven. After you install those (if you don’t have them installed).

First create a folder blog and inside it create a pom.xml file. So here it is how it looks like:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>net.flakron</groupId>
    <artifactId>blog</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>blog</name>
    <description>A simple blog app</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.3.2.RELEASE</version>
        <relativePath/>
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

I personally prefer to use IntelliJ IDE, you can use whatever you find better. Anyway, so lets create our first entity Post this is where we will store our post data.

package net.flakron.blog.entities;

public class Post {

    private String title;
    private String body;

    public Post(String title, String body) {
        this.title = title;
        this.body = body;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getBody() {
        return body;
    }

    public void setBody(String body) {
        this.body = body;
    }
}

Now lets create our controller, this where we will send the post data and retrieve them back, just as a test.

package net.flakron.blog.controllers;

import net.flakron.blog.entities.Post;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping("posts")
@RestController
public class PostsController {

    @RequestMapping(method = RequestMethod.GET)
    public Post index() {
        return new Post("My first post", "So this will be a very long post about ...");
    }

}

This looks ok for now, just that where do we start this app, or how do we start it. Hmmmm let’s bootstrap our app with this main class

package net.flakron.blog;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class BlogApplication {

    public static void main(String[] args) {
        SpringApplication.run(BlogApplication.class, args);
    }
}

As you can see this final class has a main method, so it means you can start the application as any other java console app.

So fire it up and go to http://localhost:8080/posts and as a response you will get:

{"title":"My first post","body":"So this will be a very long post about ..."}

This looks pretty nice 🙂

Below you can download the full source:

https://github.com/flakron/blog/archive/0.0.1-SNAPSHOT.zip

See you later