Skip to content

Commit bee5e0b

Browse files
committed
GH-1138 Fix serverless web container initialization
Resolves #1138
1 parent d38ab94 commit bee5e0b

File tree

6 files changed

+46
-28
lines changed

6 files changed

+46
-28
lines changed

spring-cloud-function-adapters/spring-cloud-function-serverless-web/pom.xml

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
<groupId>org.springframework</groupId>
2222
<artifactId>spring-webmvc</artifactId>
2323
</dependency>
24-
24+
2525
<!-- -->
2626
<dependency>
2727
<groupId>org.springframework.boot</groupId>
@@ -34,26 +34,31 @@
3434
<scope>test</scope>
3535
</dependency>
3636
<!-- -->
37-
37+
3838
<dependency>
39-
<groupId>jakarta.servlet</groupId>
40-
<artifactId>jakarta.servlet-api</artifactId>
41-
<scope>provided</scope>
39+
<groupId>jakarta.servlet</groupId>
40+
<artifactId>jakarta.servlet-api</artifactId>
41+
<scope>provided</scope>
4242
</dependency>
4343
<dependency>
4444
<groupId>org.springframework.boot</groupId>
4545
<artifactId>spring-boot-starter-test</artifactId>
4646
<scope>test</scope>
4747
</dependency>
48+
<dependency>
49+
<groupId>org.springframework.boot</groupId>
50+
<artifactId>spring-boot-starter-freemarker</artifactId>
51+
<scope>test</scope>
52+
</dependency>
4853
<dependency>
4954
<groupId>org.springframework.boot</groupId>
5055
<artifactId>spring-boot-starter-web</artifactId>
5156
<exclusions>
52-
<exclusion>
53-
<groupId>org.springframework.boot</groupId>
54-
<artifactId>spring-boot-starter-tomcat</artifactId>
55-
</exclusion>
56-
</exclusions>
57+
<exclusion>
58+
<groupId>org.springframework.boot</groupId>
59+
<artifactId>spring-boot-starter-tomcat</artifactId>
60+
</exclusion>
61+
</exclusions>
5762
<scope>test</scope>
5863
</dependency>
5964
</dependencies>

spring-cloud-function-adapters/spring-cloud-function-serverless-web/src/main/java/org/springframework/cloud/function/serverless/web/ServerlessAutoConfiguration.java

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import org.springframework.beans.BeansException;
2323
import org.springframework.beans.factory.InitializingBean;
2424
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
25-
import org.springframework.boot.autoconfigure.web.servlet.DispatcherServletRegistrationBean;
2625
import org.springframework.boot.web.context.ConfigurableWebServerApplicationContext;
2726
import org.springframework.boot.web.server.WebServer;
2827
import org.springframework.boot.web.server.WebServerException;
@@ -67,16 +66,6 @@ public void stop() throws WebServerException {
6766

6867
@Override
6968
public void start() throws WebServerException {
70-
if (applicationContext instanceof ServletWebServerApplicationContext servletApplicationContet) {
71-
DispatcherServlet dispatcher = applicationContext.getBean(DispatcherServlet.class);
72-
try {
73-
dispatcher.init(new ProxyServletConfig(servletApplicationContet.getServletContext()));
74-
logger.info("Initalized DispatcherServlet");
75-
}
76-
catch (Exception e) {
77-
throw new IllegalStateException("Faild to create Spring MVC DispatcherServlet proxy", e);
78-
}
79-
}
8069
}
8170

8271
@Override
@@ -97,10 +86,18 @@ public void afterPropertiesSet() throws Exception {
9786
logger.info("Configuring Serverless Web Container");
9887
ServerlessServletContext servletContext = new ServerlessServletContext();
9988
servletApplicationContet.setServletContext(servletContext);
100-
for (ServletContextInitializer beans : new ServletContextInitializerBeans(this.applicationContext)) {
101-
if (!(beans instanceof DispatcherServletRegistrationBean)) {
102-
beans.onStartup(servletContext);
103-
}
89+
DispatcherServlet dispatcher = applicationContext.getBean(DispatcherServlet.class);
90+
try {
91+
logger.info("Initializing DispatcherServlet");
92+
dispatcher.init(new ProxyServletConfig(servletApplicationContet.getServletContext()));
93+
logger.info("Initalized DispatcherServlet");
94+
}
95+
catch (Exception e) {
96+
throw new IllegalStateException("Faild to create Spring MVC DispatcherServlet proxy", e);
97+
}
98+
for (ServletContextInitializer initializer : new ServletContextInitializerBeans(this.applicationContext)) {
99+
System.out.println("==> INITIALIZING " + initializer);
100+
initializer.onStartup(servletContext);
104101
}
105102
}
106103
}

spring-cloud-function-adapters/spring-cloud-function-serverless-web/src/main/java/org/springframework/cloud/function/serverless/web/ServerlessMVC.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030

3131
import jakarta.servlet.AsyncContext;
32+
import jakarta.servlet.DispatcherType;
3233
import jakarta.servlet.Filter;
3334
import jakarta.servlet.FilterChain;
3435
import jakarta.servlet.FilterConfig;
@@ -156,8 +157,6 @@ public void stop() {
156157
* @see org.springframework.test.web.servlet.result.MockMvcResultMatchers
157158
*/
158159
public void service(HttpServletRequest request, HttpServletResponse response) throws Exception {
159-
//this.waitForContext();
160-
//contextStartupLatch.await(this.initializatioinTimeout, TimeUnit.MILLISECONDS);
161160
Assert.state(this.waitForContext(), "Failed to initialize Application within the specified time of " + this.initializatioinTimeout + " milliseconds. "
162161
+ "If you need to increase it, please set " + INIT_TIMEOUT + " environment variable");
163162
this.service(request, response, (CountDownLatch) null);
@@ -269,6 +268,10 @@ public void doFilter(ServletRequest request, ServletResponse response) throws IO
269268

270269
this.request = request;
271270
this.response = response;
271+
272+
if (!response.isCommitted() && request.getDispatcherType() != DispatcherType.ASYNC) {
273+
response.flushBuffer();
274+
}
272275
}
273276

274277
/**

spring-cloud-function-adapters/spring-cloud-function-serverless-web/src/test/java/org/springframework/cloud/function/serverless/web/RequestResponseTests.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,14 @@ public void after() {
5757
this.mvc.stop();
5858
}
5959

60+
@Test
61+
public void validateFreemarker() throws Exception {
62+
HttpServletRequest request = new ServerlessHttpServletRequest(null, "GET", "/index");
63+
ServerlessHttpServletResponse response = new ServerlessHttpServletResponse();
64+
mvc.service(request, response);
65+
assertThat(response.getContentAsString()).contains("<h1> hello from freemarker </h1>");
66+
}
67+
6068
@Test
6169
public void validateAccessDeniedWithCustomHandler() throws Exception {
6270
HttpServletRequest request = new ServerlessHttpServletRequest(null, "GET", "/foo/deny");

spring-cloud-function-adapters/spring-cloud-function-serverless-web/src/test/java/org/springframework/cloud/function/test/app/PetStoreSpringAppConfig.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353

5454

5555
@Configuration
56-
@Import({ PetsController.class })
56+
@Import({ PetsController.class, FreemarkerController.class })
5757
@EnableWebSecurity
5858
@EnableAutoConfiguration
5959
public class PetStoreSpringAppConfig {
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<h1> hello from freemarker </h1>
2+
3+
<#list 1..10 as x>
4+
${x}
5+
</#list>

0 commit comments

Comments
 (0)