Skip to content

Commit f80dc5d

Browse files
committed
Added method to SpringBoot container handler to address #88.
1 parent 0ea2444 commit f80dc5d

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

aws-serverless-java-container-spring/src/main/java/com/amazonaws/serverless/proxy/spring/SpringBootLambdaContainerHandler.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ protected void handleRequest(AwsProxyHttpServletRequest containerRequest, AwsHtt
113113

114114
// wire up the application context on the first invocation
115115
if (!initialized) {
116+
System.setProperty("spring.profiles.active", String.join(",", springProfiles));
116117
SpringServletContainerInitializer springServletContainerInitializer = new SpringServletContainerInitializer();
117118
LinkedHashSet<Class<?>> webAppInitializers = new LinkedHashSet<>();
118119
webAppInitializers.add(springBootInitializer);

samples/springboot/pet-store/src/main/java/com/amazonaws/serverless/sample/springboot/Application.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,17 @@
44
import org.springframework.boot.SpringApplication;
55
import org.springframework.boot.autoconfigure.SpringBootApplication;
66
import org.springframework.boot.web.support.SpringBootServletInitializer;
7+
import org.springframework.context.annotation.Bean;
78
import org.springframework.context.annotation.ComponentScan;
9+
import org.springframework.web.servlet.HandlerAdapter;
10+
import org.springframework.web.servlet.HandlerExceptionResolver;
11+
import org.springframework.web.servlet.HandlerMapping;
12+
import org.springframework.web.servlet.ModelAndView;
13+
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;
14+
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
15+
16+
import javax.servlet.http.HttpServletRequest;
17+
import javax.servlet.http.HttpServletResponse;
818

919

1020
@SpringBootApplication
@@ -15,6 +25,40 @@ public class Application extends SpringBootServletInitializer {
1525
@Value("${logging.level.root:OFF}")
1626
String message = "";
1727

28+
/*
29+
* Create required HandlerMapping, to avoid several default HandlerMapping instances being created
30+
*/
31+
@Bean
32+
public HandlerMapping handlerMapping() {
33+
return new RequestMappingHandlerMapping();
34+
}
35+
36+
/*
37+
* Create required HandlerAdapter, to avoid several default HandlerAdapter instances being created
38+
*/
39+
@Bean
40+
public HandlerAdapter handlerAdapter() {
41+
return new RequestMappingHandlerAdapter();
42+
}
43+
44+
/*
45+
* optimization - avoids creating default exception resolvers; not required as the serverless container handles
46+
* all exceptions
47+
*
48+
* By default, an ExceptionHandlerExceptionResolver is created which creates many dependent object, including
49+
* an expensive ObjectMapper instance.
50+
*/
51+
@Bean
52+
public HandlerExceptionResolver handlerExceptionResolver() {
53+
return new HandlerExceptionResolver() {
54+
55+
@Override
56+
public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
57+
return null;
58+
}
59+
};
60+
}
61+
1862
public static void main(String[] args) {
1963
SpringApplication.run(Application.class, args);
2064
}

0 commit comments

Comments
 (0)