Latest reactive spring boot spring webflux thymeleaf

Spring Start WebFlux + Thymeleaf Reactive Example – Mkyong.com

Project Directory

In this article we’ll present the right way to develop a reactive net software

  • Spring Bag 2.1.2.RELEASE
  • Spring WebFlux 5.1.4.RELEASE
  • Thymeleaf 3.0.11.RELEASE [19659003] Maven three

Spring Boot Defines all, and the key makes use of Thymeleaf ReactiveDataDriverContextVariable to allow the Thymeleaf mannequin to make use of knowledge-controlled area.

@RequestMapping ("/")
public string (remaining template)

// knowledge streaming, knowledge managed mode.
IReactiveDataDriverContextVariable ReactiveDataDrivenMode =
new ReactiveDataDriverContextVariable (movieRepository.findAll (), 1);

model.addAttribute ("movies", reactiveDataDrivenMode);

return "view";

Notice
Working within the spring-loaded WebFlux + Thymeleaf + server (SSE) occasion. Updating right here.

1. Undertaking Directory

2. Maven

pom.xml

four.0.zero

com.mkyong.spring.reactive
webflux-thymeleaf
1.0

1.eight

org.springframework.boot [19659016] spring-boot boot for mum or dad
2.1.2.RELEASE

org.springframework.boot
bow-play-starter-webflux

org.springframework.boot
spring-play-starter -thymeleaf [19659023] org.springframework.boot
spring-play-starter check
check

org.springframework.boot
bow-play-DevTools
true

org.springframework .boot [19659030] spring-boot-maven plugin

Show venture dependencies.

$ mvn dependence: tree

[INFO] com.mkyong.spring.webflux: ReactiveApp: jar: 1.0-SNAPSHOT
[INFO] + – org.springframework.boot: spring-boot-starter-webflux: jar: 2.1.2.RELEASE: compile
[INFO] | + – org.springframework.boot: spring-boot-starter: jar: 2.1.2.RELEASE: turn
[INFO] | | + – org.springframework.boot: spring-boot: jar: 2.1.2.RELEASE: compile
[INFO] | | | – org.springframework: spring-context: jar: 5.1.four.RELEASE: compile
[INFO] | | | + – org.springframework: spring-aop: jar: 5.1.four.RELEASE: compile
[INFO] | | | – org.springframework: spring-expression: jar: 5.1.four.RELEASE: compile
[INFO] | | + – org.springframework.boot: spring-boot-autoconfigure: jar: 2.1.2.RELEASE: compile
[INFO] | | + – org.springframework.boot: spring-boot-starter-logging: jar: 2.1.2.RELEASE: compile
[INFO] | | | + – ch.qos.logback: logback-basic: jar: 1.2.three: translate
[INFO] | | | | – ch.qos.logback: logback-core: jar: 1.2.3: compile
[INFO] | | | + – org.apache.logging.log4j: log4j-to-slf4j: jar: 2.11.1: compile
[INFO] | | | | – org.apache.logging.log4j: log4j-api: jar: 2.11.1: compile
[INFO] | | | – org.slf4j: jul-to-slf4j: jar: 1.7.25: translate
[INFO] | | + – javax.annotation: javax.annotation-api: jar: 1.three.2: compile
[INFO] | | – org.yaml: snakeyaml: jar: 1.23: runtime
[INFO] | + – org.springframework.boot: spring-boot-starter-json: jar: 2.1.2.RELEASE: compile
[INFO] | | + – com.fasterxml.jackson.core: jackson-datasind: jar: 2.9.8: compile
[INFO] | | | + – com.fasterxml.jackson.core: jackson: jar: 2.9.0: compile
[INFO] | | | com.fasterxml.jackson.core: jackson-core: jar: 2.9.8: compile
[INFO] | | + – com.fasterxml.jackson.datatype: jackson-datatype-jdk8: jar: 2.9.8: compile
[INFO] | | + – com.fasterxml.jackson.datatype: jackson-datatype-jsr310: jar: 2.9.8: compile
[INFO] | | com.fasterxml.jackson.module: jackson-module-parameter-names: jar: 2.9.eight: assemble
[INFO] | + – org.springframework.boot: spring-boot-starter reactor: jar: 2.1.2.RELEASE: compile
[INFO] | | – io.projectreactor.netty: reactor: jar: 0.eight.4.RELEASE: compile
[INFO] | | + – io.netty: netty-codec-http: jar: 4.1.31.The terminal: turn
[INFO] | | | – io.netty: netty-codec: jar: four.1.31.The terminal: flip
[INFO] | | + – io.netty: netty-codec-http2: jar: four.1.31.The terminal: flip
[INFO] | | + – io.netty: netty-handler: jar: four.1.31.The terminal: flip
[INFO] | | | + – io.netty: netty buffer: jar: 4.1.31. Terminal: assemble
[INFO] | | | – io.netty: netty-transport: jar: 4.1.31.The terminal: flip
[INFO] | | | – io.netty: netty-resolver: jar: 4.1.31.The terminal: flip
[INFO] | | + – io.netty: netty-handler-proxy: jar: four.1.31.The terminal: flip
[INFO] | | | – io.netty: netty-codec-socks: jar: four.1.31.The terminal: flip
[INFO] | | – io.netty: netty-transport-native epoll: jar: linux-x86_64: four.1.31.Remaining: compile
[INFO] | | + – io.netty: netty-widespread: jar: four.1.31.The terminal: turn
[INFO] | | – io.netty: netty-transport-native-unix-widespread: jar: four.1.31.Conclusion: compile
[INFO] | + – org.hibernate.validator: Hibernate-Validator: jar: 6.0.14.Conclusion: compile
[INFO] | | + – javax.validation: validation-api: jar: 2.0.1. Conclusion: put together
[INFO] | | + – org.jboss.logging: jboss-logging: jar: 3.3.2 Conclusion: assemble
[INFO] | | – com.fasterxml: Classmate: jar: 1.4.zero: Translate
[INFO] | + – org.springframework: spring-net: jar: 5.1.four.RELEASE: put collectively
[INFO] | | – org.springframework: spring-beans: jar: 5.1.4.RELEASE: put together
[INFO] | + – org.springframework: spring-webflux: jar: 5.1.four.RELEASE: put together
[INFO] | | – io.projectreactor: reactor core: jar: three.2.5.RELEASE: assemble
[INFO] | | – org.reactivestreams: reactive currents: jar: 1.zero.2: assemble
[INFO] | org.synchronoss.cloud:nio-multipart-parser:jar:1.1.zero:compile
[INFO] | + – org.slf4j: slf4j-api: jar: 1.7.25: translate
[INFO] | org.synchronoss.cloud:nio-stream-storage:jar:1.1.3:compile
[INFO] + – org.springframework.boot: spring-boot-starter-thymeleaf: jar: 2.1.2.RELEASE: compile
[INFO] | + – org.thymeleaf: thymeleaf-spring5: jar: three.0.11.RELEASE: compile
[INFO] | | – org.thymeleaf: thymeleaf: jar: 3.0.11.RELEASE: compile
[INFO] | | + – org.attoparser: attoparser: jar: 2.zero.5.RELEASE: Translate
[INFO] | | – org.unbescape: unbescape: jar: 1.1.6.RELEASE: assemble
[INFO] | org.thymeleaf.extras: thymeleaf-extras-java8time: jar: 3.0.2.RELEASE: compile
[INFO] org.springframework.boot: spring-boot-starter check: jar: 2.1.2.RELEASE: check
[INFO] + – org.springframework.boot: spring-boot-check: jar: 2.1.2.RELEASE: check
[INFO] + – org.springframework.boot: spring-boot-check-autoconfigure: jar: 2.1.2.RELEASE: check
[INFO] + – com.jayway.jsonpath: json-path: jar: 2.four.zero: check
[INFO] | – internet.minidev: json-sensible: jar: 2.three: check
[INFO] | – internet.minidev: accessors-sensible: jar: 1.2: check
[INFO] | – org.ow2.asm: asm: jar: 5.0.4: check
[INFO] + – trains: trains: jar: four.12: check
[INFO] + – org.assertj: assertj-core: jar: 3.11.1: check
[INFO] + – org.mockito: mockito-core: jar: 2.23.4: check
[INFO] | + – internet.bytebuddy: byte-buddy: jar: 1.9.7: check
[INFO] | + – internet.bytebuddy: byte-man-agent: jar: 1.9.7: check
[INFO] | – org.objenesis: objenesis: jar: 2.6: check
[INFO] + – org.hamcrest: hamcrest-core: jar: 1.3: check
[INFO] + – org.hamcrest: hamcrest-library: jar: 1.3: check
[INFO] + – org.skyscreamer: jsonassert: jar: 1.5.0: check
[INFO] | com.vaadin.external.google:android-json:jar:0.0.20131108.vaadin1:check
[INFO] + – org.springframework: spring-core: jar: 5.1.four.RELEASE: compile
[INFO] | – org.springframework: spring-jcl: jar: 5.1.4.RELEASE: put together
[INFO] + – org.springframework: spring check: jar: 5.1.4.RELEASE: check
[INFO] – org.xmlunit: xmlunit-core: jar: 2.6.2: check
[INFO] – javax.xml.bind: jaxb-api: jar: 2.3.1: check
[INFO] – javax.activation: javax.activation-api: jar: 1.2.0: check

3. Spring Boot + Spring WebFlux

three.1 Spring WebFlux tag-based mostly driver. Compress knowledge with ReactiveDataDriverContextVariable, it allows a mannequin pushed by reactive knowledge within the Thymeleaf mannequin.

MovieController.java

package deal com.mkyong.reactive.controller;

import com.mkyong.reactive.repository.MovieRepository;
import org.springframework.beans.manufacturing unit.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Mannequin;
import org.springframework.net.bind.annotation.RequestMapping;
import org.thymeleaf.spring5.context.webflux.IReactiveDataDriverContextVariable;
import org.thymeleaf.spring5.context.webflux.ReactiveDataDriverContextVariable;

@Controller
public class MovieController

@Autowired
personal MovieRepository movieRepository;

@RequestMapping ("/")
public string (ultimate template)

// masses 1 and display 1, stream knowledge, knowledge managed mode.
IReactiveDataDriverContextVariable ReactiveDataDrivenMode =
new ReactiveDataDriverContextVariable (movieRepository.findAll (), 1);

mannequin.addAttribute ("movies", reactiveDataDrivenMode);

// basic, anticipate the repository to load all the things and show it.
//model.addAttribute("movies ", movieRepository.findAll ());

return "index";

three.2 Restore the Flux object within the archive.

MovieRepository.java

package deal com.mkyong.reactive.repository;

import com.mkyong.reactive.Movie;
tuontireaktori.core.writer.Flux;

public interface MovieRepository

Flux findAll ();

ReactiveMovieRepository.java

package deal com.mkyong.reactive.repository;

import com.mkyong.reactive.Movie;
import org.springframework.stereotype.Repository;
tuontireaktori.core.writer.Flux;

import java.time.
import java.util.ArrayList;
import java.util.Record;

@Repository
public class ReactiveMovieRepository implements MovieRepository

personal static listing film = new ArrayList <> ();

static
film.add (new movie ("Polar (2019)", 64));
movie.add (new movie ("Iron Man (2008)", 79));
film.add (new movie ("The Shawshank Redemption (1994)", 93));
film.add (new movie ("Forrest Gump (1994)", 83));
movie.add (new film ("Glass (2019)", 70));

@Bypass
public Flux findAll ()
// Simulate a large knowledge record and stream it each two seconds
Reset Flux.fromterable (movie) .delayElements (2 second period);

three.three Movie Mannequin

Movie.java

package deal com.mkyong.reactive;

public class movie

personal language identify;
personal Integer points;

// getter, setter and builder

three.4 Spring Start

MovieWebApplication.java

package deal com.mkyong.reactive;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class MovieWebApplication

public Static void essential (String [] args)
SpringApplication.run (MovieWebApplication.class, args);

4. Thymeleaf

The Thymeleaf mannequin has no special reactive character, only makes use of a traditional loop

assets / templates / index.html

Spring WebFlux + Thymeleaf

Identify Factors [[$movie.name] ] [19659043]]

Determined physique measurement.

software.properties

spring.thymeleaf.reactive.max-piece-measurement = 8192

Finished.

5.Demo

$ mvn spring-boot: run

URL = http: // localhost: 8080
The info is streamed and displayed each 2 seconds in a reactive manner.

Obtain Source

References