44import org .springframework .boot .SpringApplication ;
55import org .springframework .boot .autoconfigure .SpringBootApplication ;
66import org .springframework .boot .web .support .SpringBootServletInitializer ;
7+ import org .springframework .context .annotation .Bean ;
78import 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