[JAVA] Implement a simple Web REST API server with Spring Boot + MySQL

Outline Implement a REST API server using Spring Boot from scratch. YutaKase6/spring-api-sample

Goal Entry Point Create an API with the following entry points.

table

Physical name Logical name
id User ID
value User information

Steps...

First, create a project

Create a Spring Boot project using IntelliJ (Spring Initializr) --Qiita ij.jpeg

Check about Gradle

A little research on Gradle and read the build.gradle generated by Spring Initializr --Qiita

Think about architecture and class design before implementation

Consider the architecture of Web API implemented by Spring Boot --Qiita

I will implement

Implementing REST API with Spring Boot and JPA (domain layer) --Qiita Implementing REST API with Spring Boot and JPA (Infrastructure Layer) --Qiita Implementing REST API with Spring Boot and JPA (Application Layer) --Qiita

MySQL settings

MySQL preparation

Install & Start

% brew install mysql
% mysql.server start                                                                  

** Create table **

CREATE TABLE test_users (
  id VARCHAR(18) PRIMARY KEY
  , value TEXT DEFAULT NULL
  , created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
  , updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8
;
mysql> desc test_users;
+------------+-------------+------+-----+-------------------+-----------------------------+
| Field      | Type        | Null | Key | Default           | Extra                       |
+------------+-------------+------+-----+-------------------+-----------------------------+
| id         | varchar(18) | NO   | PRI | NULL              |                             |
| value      | text        | YES  |     | NULL              |                             |
| created_at | timestamp   | NO   |     | CURRENT_TIMESTAMP |                             |
| updated_at | timestamp   | NO   |     | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
+------------+-------------+------+-----+-------------------+-----------------------------+
4 rows in set (0.00 sec)

Spring Boot settings

Write the settings in src / main / resources / application.properties. You can write in yml, so write in yml. First of all, rename.

Write the connection settings with MySQL.

application.yml


spring:
  datasource:
    url: jdbc:mysql://localhost:3306/<SchemaName>
    username: root
    password:

  jpa:
    hibernate:
      ddl-auto: none

Operation check

Run

** Run from IntelliJ (Part 1) ** It can be executed by pressing the play button on the left of the main method.

** Run from IntelliJ (Part 2) ** You can select the main class from the pull-down on the upper right and execute it with the play button on the right.

** Run from Gradle **

% ./gradlew bootRun

** Run from java **

% ./gradlew build
% java -jar build/libs/spring-api-0.0.1-SNAPSHOT.jar

Normal confirmation

--Registration

% curl -X POST "http://localhost:8080/v1/users" -H "Content-Type: application/json" -d "{ \"id\": \"id\", \"value\": \"value\"}" -s -w '\nstatus code: %{http_code}\n'

{"id":"id","value":"value"}
status code: 201

--Reference

% curl "http://localhost:8080/v1/users/id" -s -w '\nstatus code: %{http_code}\n'

{"id":"id","value":"value"}
status code: 200
% curl -X DELETE "http://localhost:8080/v1/users/id" -s -w '\nstatus code: %{http_code}\n'


status code: 204

Abnormality check (Spring Boot default)

--Non-existent user

% curl "http://localhost:8080/v1/users/hoge" -s -w '\nstatus code: %{http_code}\n'

{"timestamp":"2018-07-20T12:11:51.131+0000","status":500,"error":"Internal Server Error","message":"No message available","path":"/v1/users/hoge"}
status code: 500

--Undefined methods

% curl "http://localhost:8080/v1/users" -s -w '\nstatus code: %{http_code}\n'

{"timestamp":"2018-07-20T12:14:08.013+0000","status":405,"error":"Method Not Allowed","message":"Request method 'GET' not supported","path":"/v1/users"}
status code: 405

--Undefined path

% curl "http://localhost:8080/v1/user" -s -w '\nstatus code: %{http_code}\n'

{"timestamp":"2018-07-20T12:14:14.668+0000","status":404,"error":"Not Found","message":"No message available","path":"/v1/user"}
status code: 404

There are many others.

Customize the response when an error occurs

Customize the error response of REST API created by Spring Boot --Qiita

Introduce Swagger

Introducing Swagger to REST API of Spring Boot --Qiita

Create unit tests

Unit test with Spring Boot + JUnit --Qiita

Unit test with Spring Boot + JUnit + Mockito --Qiita

Create a functional test

Do a stand-alone functional test with Spring Boot + JUnit + h2 --Qiita

Introduce a logger

Log Spring Boot application using Spring AOP --Qiita

Measure coverage

TBA...

Recommended Posts

Implement a simple Web REST API server with Spring Boot + MySQL
Implement a simple Rest API with Spring Security with Spring Boot 2.0
Implement a simple Rest API with Spring Security & JWT with Spring Boot 2.0
Create a web api server with spring boot
Implement REST API in Spring Boot
Implement REST API with Spring Boot and JPA (Application Layer)
Implement REST API with Spring Boot and JPA (Infrastructure layer)
Let's make a simple API with EC2 + RDS + Spring boot ①
Implement REST API with Spring Boot and JPA (domain layer)
Implement CRUD with Spring Boot + Thymeleaf + MySQL
I made a simple search form with Spring Boot + GitHub Search API.
Create a simple search app with Spring Boot
Implement GraphQL with Spring Boot
Hello World (REST API) with Apache Camel + Spring Boot 2
[Spring Boot] Get user information with Rest API (beginner)
Customize REST API error response with Spring Boot (Part 2)
A memorandum when creating a REST service with Spring Boot
Create a simple demo site with Spring Security with Spring Boot 2.1
Customize REST API error response with Spring Boot (Part 1)
Spring with Kotorin --4 REST API design
Create a simple web server with the Java standard library com.sun.net.httpserver
Let's make a book management web application with Spring Boot part1
Let's make a book management web application with Spring Boot part3
Let's make a book management web application with Spring Boot part2
[Beginner] Let's write REST API of Todo application with Spring Boot
Create a simple web application with Dropwizard
Create a simple on-demand batch with Spring Batch
Start web application development with Spring Boot
Implement simple CRUD with Go + MySQL + Docker
Implement paging function with Spring Boot + Thymeleaf
Run WEB application with Spring Boot + Thymeleaf
Spring Boot2 Web application development with Visual Studio Code SQL Server connection
Java beginner tried to make a simple web application using Spring Boot
[Spring Boot] Precautions when developing a web application with Spring Boot and placing war on an independent Tomcat server
Create a website with Spring Boot + Gradle (jdk1.8.x)
Create a simple bulletin board with Java + MySQL
Try to implement login function with Spring Boot
Build a WEB system with Spring + Doma + H2DB
Create a Spring Boot development environment with docker
Create Spring Cloud Config Server with security with Spring Boot 2.0
I tried to clone a web application full of bugs with Spring Boot
Settings for connecting to MySQL with Spring Boot + Spring JDBC
Build a docker container for a python simple web server
Build a WEB system with Spring + Doma + H2DB + Thymeleaf
Automatically map DTOs to entities with Spring Boot API
[JUnit 5 compatible] Write a test using JUnit 5 with Spring boot 2.2, 2.3
[JUnit 5] Write a validation test with Spring Boot! [Parameterization test]
Introduce swagger-ui to REST API implemented in Spring Boot
A story that stumbled when deploying a web application created with Spring Boot to EC2
I wrote a test with Spring Boot + JUnit 5 now
Download with Spring Boot
Build a WEB system with Spring + Doma + H2DB Part 2
Let's find out how to receive in Request Body with REST API of Spring Boot
Handle Java 8 date and time API with Thymeleaf with Spring Boot
The first WEB application with Spring Boot-Making a Pomodoro timer-
A story packed with the basics of Spring Boot (solved)
Create a Hello World web app with Spring framework + Jetty
Try hitting the zip code search API with Spring Boot
Introducing Spring Boot2, a Java framework for web development (for beginners)
I made a simple MVC sample system using Spring Boot
[Java] Sample project for developing web applications with Spring Boot