Nowadays, when it comes to web applications, they run on client-tier browsers using HTML + JavaScript, and the servers use the REST API to deliver data. In this article, I'll show you how easy it is to create a REST API for your server in Java.
Spring Boot + Lombok Spring Boot makes it easy to create web applications. You can also use Lombok to eliminate redundant code from Java. This time, by combining the two, you can create a REST API terribly easily. As a sample, create an API that can REST TODO.
Create a base for your application with SPRING INITIALIZR. Selected Dependencies JPA, H2, Lombok, Rest Repositories To specify. Press Generate Project to download the zip file and unzip it.
Data definition and application creation Create the data structure as a Java class.
package com.ukiuni.easyrest.entity;
import java.util.Date;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import lombok.Data;
@Data
@Entity
public class Todo {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
private String title;
private String description;
private boolean done;
@Temporal(TemporalType.TIMESTAMP)
private Date deadline;
}
Kimo is @Data of the annotation attached to the class. Annotation defined by Lombok that automatically assigns accessor methods. This allows programmers to focus on defining data by field without writing redundant getters / setters.
Define a class to make this data structure the CRUD type structure of REST API as it is.
package com.ukiuni.easyrest.repository;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;
import com.ukiuni.easyrest.entity.Todo;
@RepositoryRestResource(collectionResourceRel = "todos", path = "todos")
public interface TodoRepository extends PagingAndSortingRepository<Todo, Long> {
}
Just define the interface. No need to implement the contents.
This is the end of the source code description. Easy.
start up. In the application directory
gradle bootRun
Start with.
Let's operate it. Use the curl command.
$ curl -i http://localhost:8080/todos
HTTP/1.1 200
Content-Type: application/hal+json;charset=UTF-8
Transfer-Encoding: chunked
Date: Thu, 16 Nov 2017 11:47:24 GMT
{
"_embedded" : {
"todos" : [ ]
},
"_links" : {
"self" : {
"href" : "http://localhost:8080/todos{?page,size,sort}",
"templated" : true
},
"profile" : {
"href" : "http://localhost:8080/profile/todos"
}
},
"page" : {
"size" : 20,
"totalElements" : 0,
"totalPages" : 0,
"number" : 0
}
}
You can see that empty data has been acquired with "todos": []. Now, let's create one Todo. Use the curl command.
$ curl -i -X POST -H "Content-Type:application/json" -d "{ \"title\" : \"Advent calendar\", \"description\" : \"I have to write an article.\", \"deadline\":\"2017-12-21T07:00:00.000Z\" }" http://localhost:8080/todos
{
"title" : "Advent calendar",
"description" : "I have to write an article.",
"done" : false,
"deadline" : "2017-12-21T07:00:00.000+0000",
"_links" : {
"self" : {
"href" : "http://localhost:8080/todos/1"
},
"todo" : {
"href" : "http://localhost:8080/todos/1"
}
}
}
It seems that No. 1 was created. I will check it.
$ curl -i http://localhost:8080/todos
HTTP/1.1 200
Content-Type: application/hal+json;charset=UTF-8
Transfer-Encoding: chunked
Date: Thu, 16 Nov 2017 11:50:57 GMT
{
"_embedded" : {
"todos" : [ {
"title" : "Advent calendar",
"description" : "I have to write an article.",
"done" : false,
"deadline" : "2017-12-21T07:00:00.000+0000",
"_links" : {
"self" : {
"href" : "http://localhost:8080/todos/1"
},
"todo" : {
"href" : "http://localhost:8080/todos/1"
}
}
} ]
},
"_links" : {
"self" : {
"href" : "http://localhost:8080/todos{?page,size,sort}",
"templated" : true
},
"profile" : {
"href" : "http://localhost:8080/profile/todos"
}
},
"page" : {
"size" : 20,
"totalElements" : 1,
"totalPages" : 1,
"number" : 0
}
}
You can get what you created. I will UPDATE with PUT.
$curl -i -X PUT -H "Content-Type:application/json" -d "{ \"title\" : \"Advent calendar\", \"description\" : \"I have to write an article. Let's do our best.\", \"deadline\":\"2017-12-21T07:00:00.000Z\" }" http://localhost:8080/todos/1
HTTP/1.1 200
Location: http://localhost:8080/todos/1
Content-Type: application/hal+json;charset=UTF-8
Transfer-Encoding: chunked
Date: Thu, 16 Nov 2017 11:43:30 GMT
{
"title" : "Advent calendar",
"description" : "I have to write an article. Let's do our best.",
"done" : false,
"deadline" : "2017-12-21T07:00:00.000+0000",
"_links" : {
"self" : {
"href" : "http://localhost:8080/todos/1"
},
"todo" : {
"href" : "http://localhost:8080/todos/1"
}
}
}
I will check it.
$ curl -i http://localhost:8080/todos
HTTP/1.1 200
Content-Type: application/hal+json;charset=UTF-8
Transfer-Encoding: chunked
Date: Thu, 16 Nov 2017 11:51:55 GMT
{
"_embedded" : {
"todos" : [ {
"title" : "Advent calendar",
"description" : "I have to write an article. Let's do our best.",
"done" : false,
"deadline" : "2017-12-21T07:00:00.000+0000",
"_links" : {
"self" : {
"href" : "http://localhost:8080/todos/1"
},
"todo" : {
"href" : "http://localhost:8080/todos/1"
}
}
} ]
},
"_links" : {
"self" : {
"href" : "http://localhost:8080/todos{?page,size,sort}",
"templated" : true
},
"profile" : {
"href" : "http://localhost:8080/profile/todos"
}
},
"page" : {
"size" : 20,
"totalElements" : 1,
"totalPages" : 1,
"number" : 0
}
}
It has been updated.
Finally, delete it.
$ curl -i -X DELETE http://localhost:8080/todos/1
HTTP/1.1 204
Date: Thu, 16 Nov 2017 11:45:45 GMT
Confirm that it has disappeared.
$ curl -i http://localhost:8080/todos
HTTP/1.1 200
Content-Type: application/hal+json;charset=UTF-8
Transfer-Encoding: chunked
Date: Thu, 16 Nov 2017 11:47:24 GMT
{
"_embedded" : {
"todos" : [ ]
},
"_links" : {
"self" : {
"href" : "http://localhost:8080/todos{?page,size,sort}",
"templated" : true
},
"profile" : {
"href" : "http://localhost:8080/profile/todos"
}
},
"page" : {
"size" : 20,
"totalElements" : 0,
"totalPages" : 0,
"number" : 0
}
}
"todos": [] It has disappeared.
I was able to create a REST API just by creating a data structure and a Repository class for publishing. Using Java's type definition strength and annotation capabilities makes it so easy to create a REST API. how is it? Java is often disliked these days, but have you reviewed it? Please use Java elegantly.
Sample code can be found at here.
That's all from the field.
Recommended Posts