1
0

More than 1 year has passed since last update.

「はじめての Django アプリ作成」をSpring WebFluxでやってみた、その 1

Last updated at Posted at 2022-02-12

はじめに

この記事はDjangoの公式チュートリアル「はじめての Django アプリ作成」と同じものをSpring WebFluxで作成することでDjangoとSpring WebFluxの違いを理解することを目的としています。

違い一覧

DjangoとSpringでは同じコンセプトでも用語が結構違います。

Django Spring
Model(モデル) Model(モデル)
Template(テンプレート) View(ビュー)
View(ビュー) Controller(コントローラ)

はじめてのアプリ作成、その 1

プロジェクトを作成する

Django

Djangoではdjango-adminを使ってプロジェクトを作成できます。

django-admin startproject mysite

Spring WebFlux

SpringではSpring Initializrを使ってプロジェクトを作成できます。Spring Initializrの場合はプロジェクト名以外のいくつかの設定を与えることができます。
今回はSpring WebFluxを使うため、dependenciesにSpring Reactive Webを追加します。

fig1.png

下のGENERATEを押すとプロジェクトをzipファイルでダウンロードできます。

サーバーを立ち上げる

Djangoのrunserverの場合、ただの開発用サーバーに過ぎませんがgradleを使ってWebFluxのサーバーを立ち上げるとNettyという実際のプロダクション環境で使えるサーバーが立ち上がります。ログで確認できるように両方ともデフォルトで8080ポートを使います。

Django

python manage.py runserver

Spring WebFlux

./gradlew bootRun

Polls アプリケーションをつくる

Django

Djangoではmanage.pyを使ってアプリケーションを作成できます。

python manage.py startapp polls

Spring WebFlux

Spring WebFluxではstartappのような機能はありません。mysite/src/main/java/com/example/mysiteの中にpollsというディレクトリーを直接作ります。

cd mysite
mkdir -p src/main/java/com/example/mysite/polls

はじめてのビュー作成

Djangoでのビューを意味するものでSpringではコントローラに該当します。
"Hello, world. You're at the polls index."というHTTPレスポンスは返すコントローラを作ります。

Django

Djangoではviews.pyでファンクションを定義し、Rootからpollsアプリ、pollsアプリからviewに向かうパスを繋げます。

まず、HttpResponseを返すindex(request)を定義し、

polls/views.py
from django.http import HttpResponse

def index(request):
    return HttpResponse("Hello, world. You're at the polls index.")

pollsアプリの''URLにindex(request)を紐づけます。

polls/urls.py
from django.urls import path

from . import views

urlpatterns = [
    path('', views.index, name='index'),
]

あとプロジェクトルートからpolls/pollsアプリを紐づけます。

mysite/urls.py
from django.contrib import admin
from django.urls import include, path

urlpatterns = [
    path('polls/', include('polls.urls')),
    path('admin/', admin.site.urls),
]

Spring WebFlux

先に作ったmysite/src/main/java/com/example/mysite/pollsMyController.javaというJavaクラスファイルを生成します。

cd mysite
touch src/main/java/com/example/mysite/polls/MyController.java

MyController.javaの中身は以下のようになります。

MyController.java
package com.example.mysite.polls;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Mono;

@RestController
@RequestMapping("polls")
public class MyController {

    @GetMapping(value = "")
    Mono<ResponseEntity<String>> index() {
        return Mono.just(ResponseEntity.status(HttpStatus.OK)
                         .body("Hello, world. You're at the polls index.")
        );
    }
}
  • @RestController : SpringにMyControllerクラスをアプリとして登録するような効果を持ちます。
  • @RequestMapping("polls") : mysite/urls.pypollsアプリを登録するような効果を持ちます。クラスの中のURLマッピングはpolls/URLをベースに持つようになります。
  • @GetMapping(value = "") : Djangoでindex(request)polls/urls.pyに登録するような効果を持ちます。value = ""なのでhttp://127.0.0.1:8080/pollsで接続するとindex()が呼ばれます。
  • Mono<ResponseEntity<String>> : Spring WebFluxで使うオブジェクトの箱のようなものです。0..1個のオブジェクトを入れることができます。Mono.just()関数でMonoのオブジェクトを生成できます。
  • ResponseEntity.status(HttpStatus.OK).body("Hello, world. You're at the polls index.") : DjangoのHttpResponse("Hello, world. You're at the polls index.")に該当します。
1
0
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
1
0