Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add basic support for Spring 5 WebFlux reactive in Spring Boot 2 (#333)
Browse files Browse the repository at this point in the history
ebussieres authored and Eric Bussieres committed Dec 3, 2019
1 parent d74b2db commit 4773093
Showing 32 changed files with 622 additions and 560 deletions.
10 changes: 5 additions & 5 deletions docs/src/orchid/resources/wiki/guide/spring-integration.md
Original file line number Diff line number Diff line change
@@ -35,7 +35,7 @@ public class MvcConfig extends WebMvcConfigurerAdapter {

@Bean
public Loader templateLoader(){
return new ServletLoader(servletContext);
return new ServletLoader(this.servletContext);
}

@Bean
@@ -47,7 +47,7 @@ public class MvcConfig extends WebMvcConfigurerAdapter {
public PebbleEngine pebbleEngine() {
return new PebbleEngine.Builder()
.loader(this.templateLoader())
.extension(springExtension())
.extension(this.springExtension())
.build();
}

@@ -56,7 +56,7 @@ public class MvcConfig extends WebMvcConfigurerAdapter {
PebbleViewResolver viewResolver = new PebbleViewResolver();
viewResolver.setPrefix("/WEB-INF/templates/");
viewResolver.setSuffix(".html");
viewResolver.setPebbleEngine(pebbleEngine());
viewResolver.setPebbleEngine(this.pebbleEngine());
return viewResolver;
}

@@ -75,7 +75,7 @@ public class ProfileController {
@RequestMapping
public ModelAndView getUserProfile(@RequestParam("id") long id) {
ModelAndView mav = new ModelAndView();
mav.addObject("user", userService.getUser(id));
mav.addObject("user", this.userService.getUser(id));
mav.setViewName("profile");
return mav;
}
@@ -168,7 +168,7 @@ To output any error:
A timer in PebbleView is available to output the time taken to process a template. Just add the following config to your log4j.xml

```xml
<Logger name="com.mitchellbosecke.pebble.spring.PebbleView.timer" level="DEBUG" additivity="false">
<Logger name="com.mitchellbosecke.pebble.spring.servlet.PebbleView.timer" level="DEBUG" additivity="false">
<AppenderRef ref="STDOUT" />
</Logger>
```
6 changes: 6 additions & 0 deletions pebble-spring/pebble-spring-boot-starter/pom.xml
Original file line number Diff line number Diff line change
@@ -24,6 +24,12 @@
<version>${boot.version}</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
<version>${boot.version}</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>io.pebbletemplates</groupId>
<artifactId>pebble-spring5</artifactId>
Original file line number Diff line number Diff line change
@@ -4,23 +4,27 @@
import com.mitchellbosecke.pebble.extension.Extension;
import com.mitchellbosecke.pebble.loader.ClasspathLoader;
import com.mitchellbosecke.pebble.loader.Loader;
import com.mitchellbosecke.pebble.spring.PebbleViewResolver;
import com.mitchellbosecke.pebble.spring.extension.SpringExtension;
import java.util.List;
import javax.servlet.Servlet;
import com.mitchellbosecke.pebble.spring.reactive.PebbleReactiveViewResolver;
import com.mitchellbosecke.pebble.spring.servlet.PebbleViewResolver;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication.Type;
import org.springframework.boot.autoconfigure.web.reactive.WebFluxAutoConfiguration;
import org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.List;

@Configuration
@ConditionalOnClass(PebbleEngine.class)
@AutoConfigureAfter(WebMvcAutoConfiguration.class)
@AutoConfigureAfter({WebMvcAutoConfiguration.class, WebFluxAutoConfiguration.class})
@EnableConfigurationProperties(PebbleProperties.class)
public class PebbleAutoConfiguration {

@@ -40,7 +44,6 @@ public Loader<?> pebbleLoader() {
loader.setSuffix(this.properties.getSuffix());
return loader;
}

}

@Configuration
@@ -82,9 +85,8 @@ public PebbleEngine pebbleEngine() {
}

@Configuration
@ConditionalOnWebApplication
@ConditionalOnClass({Servlet.class})
public static class PebbleViewResolverConfiguration {
@ConditionalOnWebApplication(type = Type.SERVLET)
public static class PebbleWebMvcConfiguration {

@Autowired
private PebbleProperties properties;
@@ -108,6 +110,33 @@ public PebbleViewResolver pebbleViewResolver() {
}
}

@Configuration
@ConditionalOnWebApplication(type = Type.REACTIVE)
public static class PebbleReactiveConfiguration {

@Autowired
private PebbleProperties properties;

@Autowired
private PebbleEngine pebbleEngine;

@Bean
@ConditionalOnMissingBean(name = "pebbleReactiveViewResolver")
public PebbleReactiveViewResolver pebbleReactiveViewResolver() {
String prefix = this.properties.getPrefix();
if (this.pebbleEngine.getLoader() instanceof ClasspathLoader) {
// classpathloader doesn't like leading slashes in paths
prefix = stripLeadingSlash(this.properties.getPrefix());
}
PebbleReactiveViewResolver resolver = new PebbleReactiveViewResolver(this.pebbleEngine);
resolver.setPrefix(prefix);
resolver.setSuffix(this.properties.getSuffix());
resolver.setViewNames(this.properties.getViewNames());
resolver.setRequestContextAttribute(this.properties.getRequestContextAttribute());
return resolver;
}
}

private static String stripLeadingSlash(String value) {
if (value == null) {
return null;
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package com.mitchellbosecke.pebble.boot;

import com.mitchellbosecke.pebble.extension.AbstractExtension;
import com.mitchellbosecke.pebble.extension.Extension;
import com.mitchellbosecke.pebble.extension.Function;
import com.mitchellbosecke.pebble.template.EvaluationContext;
import com.mitchellbosecke.pebble.template.PebbleTemplate;

import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Configuration(proxyBeanMethods = false)
public class AppConfig {
@Bean
public MessageSource messageSource() {
ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
messageSource.setBasename("messages");
messageSource.setFallbackToSystemLocale(false);
return messageSource;
}

@Bean
public LocaleResolver localeResolver() {
return new AcceptHeaderLocaleResolver();
}

@Bean
public Extension testExtension() {
return new TestExtension();
}

public static class TestExtension extends AbstractExtension {

@Override
public Map<String, Function> getFunctions() {
Map<String, Function> functions = new HashMap<String, Function>();
functions.put("testFunction", new TestFunction());
return functions;
}

public static class TestFunction implements Function {

@Override
public List<String> getArgumentNames() {
return Collections.emptyList();
}

@Override
public Object execute(Map<String, Object> args, PebbleTemplate self,
EvaluationContext context, int lineNumber) {
return "Tested!";
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,62 +1,12 @@
package com.mitchellbosecke.pebble.boot;

import com.mitchellbosecke.pebble.extension.AbstractExtension;
import com.mitchellbosecke.pebble.extension.Extension;
import com.mitchellbosecke.pebble.extension.Function;
import com.mitchellbosecke.pebble.template.EvaluationContext;
import com.mitchellbosecke.pebble.template.PebbleTemplate;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver;

@SpringBootApplication
@ServletComponentScan
public class Application {

public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}

@Bean
public LocaleResolver localeResolver() {
return new AcceptHeaderLocaleResolver();
}

@Bean
public Extension testExtension() {
return new TestExtension();
}

public static class TestExtension extends AbstractExtension {

@Override
public Map<String, Function> getFunctions() {
Map<String, Function> functions = new HashMap<String, Function>();
functions.put("testFunction", new TestFunction());
return functions;
}

public static class TestFunction implements Function {

@Override
public List<String> getArgumentNames() {
return Collections.emptyList();
}

@Override
public Object execute(Map<String, Object> args, PebbleTemplate self,
EvaluationContext context, int lineNumber) {
return "Tested!";
}
}
}

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.mitchellbosecke.pebble.boot;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@@ -13,17 +12,17 @@ public String hello() {
}

@RequestMapping("/index.action")
public String index(ModelMap model) {
public String index() {
return "index";
}

@RequestMapping("/contextPath.action")
public String contextPath(ModelMap model) {
public String contextPath() {
return "contextPath";
}

@RequestMapping("/extensions.action")
public String extensions(ModelMap model) {
public String extensions() {
return "extensions";
}

Original file line number Diff line number Diff line change
@@ -4,9 +4,6 @@
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.support.ResourceBundleMessageSource;

@SpringBootApplication
public class NonWebApplication {
@@ -17,13 +14,4 @@ public static void main(String[] args) {
.build();
sa.run(args);
}

@Bean
public MessageSource messageSource() {
ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
messageSource.setBasename("messages");
messageSource.setFallbackToSystemLocale(false);
return messageSource;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.mitchellbosecke.pebble.boot;

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

import static org.springframework.boot.WebApplicationType.REACTIVE;

@SpringBootApplication
public class ReactiveApplication {

public static void main(String[] args) {
SpringApplication application = new SpringApplication(Application.class);
application.setWebApplicationType(REACTIVE);
application.run(args);
}
}
Loading

0 comments on commit 4773093

Please sign in to comment.