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...
Create a Spring Boot project using IntelliJ (Spring Initializr) --Qiita

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

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

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
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)
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
** 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
--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
--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 error response of REST API created by Spring Boot --Qiita
Introducing Swagger to REST API of Spring Boot --Qiita
Unit test with Spring Boot + JUnit --Qiita

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

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

Log Spring Boot application using Spring AOP --Qiita

TBA...
Recommended Posts