LoginSignup
89
114

More than 3 years have passed since last update.

Spring Bootで実装するWebAPIのアーキテクチャを考える

Last updated at Posted at 2018-07-04

Spring Boot + MySQLでシンプルなWeb REST APIサーバを実装する - Qiita

Outline

Spring bootを使ったWebAPIのアーキテクチャ、クラス設計を考える。
Springの思想?に則り、3層アーキテクチャを採用する。

3層アーキテクチャ

以下のような3層を定義する。

各層の役割

  • アプリケーション層
    • UI
    • クライアントとの入出力とビジネスロジックをつなぐ
  • ドメイン層
    • ビジネスロジック
    • ドメインを表現するオブジェクト、手続きたち
  • インフラ層
    • 永続化の実装
    • 他サービスとの通信等の実装

基本的なルール

以下を定義する

  • ドメイン層は、他の層に依存してはならない
  • アプリケーション層、インフラ層はドメイン層に依存して良い

このルールにより以下のような成果物となる

  • ビジネスロジックが明確になる
  • UIからロジックが独立する
  • 永続化(ストア)の実装からロジックが独立する
  • テスタブル

パッケージ構成

.
├── SpringApiApplication.java
├── application
│   ├── controller
│   │   ├── ControllerExceptionHandler.java
│   │   └── UserController.java
│   └── resource
│       ├── ErrorResponse.java
│       └── UserBody.java
├── domain
│   ├── exception
│   │   └── NotFoundException.java
│   ├── object
│   │   └── User.java
│   ├── repository
│   │   └── UserRepository.java
│   └── service
│       └── UserService.java
├── infrastructure
│   ├── entity
│   │   └── UserEntity.java
│   └── repository
│       ├── UserJpaRepository.java
│       └── UserRepositoryImpl.java

各パッケージの解説

Application層

Resource

リクエストやレスポンスのオブジェクト

Controller

入出力とサービスのマッピングをする

Domain層

Domain Object

ビジネスを行う上で必要なモデルを表現するオブジェクト

Service

ビジネスロジック本体

Repository Interface

Infrastructure Layerとのインタフェース
依存関係逆転の原則ってやつ

Exception

Exceptionたち

Infrastructure層

Entity

通信時の構造体を表現するオブジェクト

Repository Implement

Repository Interfaceの実装。

89
114
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
89
114