How to do it...

Let us now use the reactive view implementation that can render reactive stream data using Spring Boot:

  1. Just as in the previous recipe, the only requirement for creating a full-blown reactive Spring 5 application is Spring Boot's webflux starter POM dependency. Also include the embedded Tomcat server as our official reactive server:
<dependency> 
   <groupId>org.springframework.boot</groupId> 
   <artifactId>spring-boot-starter-webflux</artifactId> 
   <exclusions> 
         <exclusion> 
             <groupId>org.springframework.boot</groupId> 
             <artifactId>spring-boot-starter-reactor-netty 
</artifactId> 
         </exclusion> 
   </exclusions> 
</dependency> 
<dependency> 
        <groupId>org.springframework.boot</groupId> 
        <artifactId>spring-boot-starter-tomcat</artifactId> 
</dependency> 
Avoid adding spring -boot-starter -web since the use of non-reactive components of Spring 5 will strictly not recommend the use of the @EnableWebMvc annotation in our context definitions.
  1. Implement logging mechanism for this project similar to ch09.
  2. Spring 5 has a reactive support for templating engines such as FreeMarker, Groovy, Thymeleaf, Velocity, and Mustache, which means these can be the appropriate options to render results from a reactive platform. Since the use of JSP is strictly prohibited, reactive FreeMarker and Thymeleaf will be configured to render data:
<dependency> 
  <groupId>org.freemarker</groupId> 
  <artifactId>freemarker</artifactId> 
</dependency> 
<dependency> 
  <groupId>org.springframework</groupId> 
  <artifactId>spring-context-support</artifactId> 
</dependency> 
<dependency> 
  <groupId>org.springframework.boot</groupId> 
  <artifactId>spring-boot-starter-thymeleaf</artifactId> 
</dependency> 
  1. Create the bootstrap class similar to ch09 inside a similar package org.packt.spring.boot:
@SpringBootApplication 
public class HRBootApplication extends  
 SpringBootServletInitializer  { 
 
   @Override 
    protected SpringApplicationBuilder  
      configure(SpringApplicationBuilder application) { 
        return application.sources(HRBootApplication.class); 
    } 
    
    public static void main(String[] args) throws Exception { 
        SpringApplication.run(HRBootApplication.class, args); 
    } 
} 
  1. Create an application.properties file in src/main/resources and add the following properties for the embedded Tomcat server and MySQL database connectivity:
server.port=8093 
server.servlet.context-path=/ch09-flux 
 
spring.datasource.driverClassName=com.mysql.jdbc.Driver 
spring.datasource.url=jdbc:mysql://localhost:3306/hrs?autoReconnect=true&useSSL=true&serverSslCert=classpath:config/spring5packt.crt 
spring.datasource.username=root 
spring.datasource.password=spring5mysql 
spring.datasource.hikari.connection-timeout=60000 
This project will be deployed and executed as a standalone reactive web application using a reactive embedded Tomcat server which will execute its entire request transactions through HTTP using port 8093 .
  1. Next, create reactive context definition classes that will inject all the reactive beans of FreeMarker and Thymeleaf. These @Configuration classes have an annotation, @EnableWebFlux to import all the Spring WebFlux configurations including the reactive web ApplicationContext:
@Configuration 
@EnableWebFlux 
public class WebFluxConfig  {  // refer to sources  } 
There is no need to inherit or implement configuration APIs.
  1. Spring 5 has a built-in support for reactive FreeMarker engine that uses the org.springframework.web.reactive.result.view.freemarker.FreeMarkerViewResolver and org.springframework.web.reactive.result.view.freemarker.FreeMarkerConfigurer. There is no need to inject all these bean objects to WebFluxConfig container.
  2. Likewise, Spring 5 provides injected beans of reactive APIs of Thymeleaf engine such as the org.thymeleaf.spring5.ISpringWebFluxTemplateEngine and org.thymeleaf.spring5.view.reactive.ThymeleafReactiveViewResolver beans. Because of @EnableWebFlux all these beans are already loaded into the container so custom injection is not needed anymore.
  3. Create a controller inside a package org.packt.spring.boot.controller that will just execute our configuration:
@Controller 
public class FluxController { 
    
   private Logger logger =  
      LoggerFactory.getLogger(FluxController.class); 
    
    @RequestMapping("/sampleFtl") 
       public String home(ModelMap model) { 
        model.addAttribute("title", "Reactive FreeMarker Result"); 
        model.addAttribute("message", "Built-in Configuration"); 
        logger.info("exceuting HelloController"); 
        return "sampleFtl"; 
    } 
     
   @RequestMapping("/thymeleaf/sampleThyme") 
   public String welcome(ModelMap model) { 
      model.addAttribute("title", "Reactive Thymeleaf Result"); 
       model.addAttribute("message", "Built-in Configuration"); 
      return "sampleThyme"; 
   } 
 
} 
  1. Built-in support is different from an auto-configuration mechanism; thus, all default properties of Thymeleaf and FreeMarker reactive view resolvers and template engines are not open to change with this version of Spring Boot. ThymeleafAutoConfiguration and FreeMarkerAutoConfiguration properties are only applied only to their non-reactive APIs.
  2. Store all templates in src/main/resources/templates because there is no other means that we can configure its template location.
  3. Save all files. Run the clean spring-boot:run command to execute ch09-flux as a standalone application. Open a browser and execute http://localhost:8093/ch09-flux/sampleFtl and http://localhost:8093/ch09-flux/thymeleaf/sampleThyme.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.74.66