title: How to enable async support in a Springboot application packaged and deployed as WAR
body: <p>The following REST endpoint shown below works as expected when my SpringBoot application is run from an executable JAR. That is, it returns the text "My test response" to the client.  However, when I package the same application as a WAR and deploy to Tomcat (8.0.29) it throws the following exception:</p>
  
  <blockquote>
    <p>There was an unexpected error (type=Internal Server Error, status=500).
    Async support must be enabled on a servlet and for all filters involved in async request processing. This is done in Java code using the Servlet API or by adding "true" to servlet and filter declarations in web.xml.</p>
  </blockquote>
  
  <pre><code>package my.rest.controllers;
  
  import java.io.ByteArrayInputStream;
  import java.io.InputStream;
  import java.io.OutputStream;
  import org.springframework.web.bind.annotation.RequestMapping;
  import org.springframework.web.bind.annotation.RequestMethod;
  import org.springframework.web.bind.annotation.RestController;
  
  import javax.servlet.http.HttpServletResponse;
  import org.springframework.web.bind.annotation.ResponseBody;
  import org.springframework.web.servlet.mvc.method.annotation.StreamingResponseBody;
  
  @RestController
  @RequestMapping("/api/file")
  public class FileContentRestController {
  
  static final int BUFFER = 2048;
  
  @RequestMapping(value = "/content", method = RequestMethod.GET)
  @ResponseBody
  public StreamingResponseBody getFileContent(HttpServletResponse response) {
      response.setContentType("text/plain");
      response.setCharacterEncoding("UTF-8");
  
          final InputStream portalFileStream = new ByteArrayInputStream("My test response".getBytes());
          return (OutputStream outputStream) -&gt; {
              int n;
              byte[] buffer = new byte[1024];
              while ((n = portalFileStream.read(buffer)) &gt; -1) {
                  outputStream.write(buffer, 0, n);
              }
              portalFileStream.close();
          };
  
  }
  
  }
  </code></pre>
  
  <p>My understanding from <a href="https://github.com/spring-projects/spring-boot/issues/1665" rel="noreferrer">here</a> and elsewhere is that SpringBoot enables async support on all the filters and servlets registered by SpringBoot.  It would certainly seems to be the case when run from a standalone JAR with the embedded Tomcat container.  </p>
  
  <p><strong>How can I ensure async support is enabled when deploying as a WAR?</strong></p>
  
  <p>My SpringBoot application is configured thus:</p>
  
  <pre><code>package my;
  
  import com.fasterxml.jackson.core.JsonProcessingException;
  import org.springframework.boot.Banner;
  import org.springframework.boot.autoconfigure.SpringBootApplication;
  import org.springframework.boot.builder.SpringApplicationBuilder;
  import org.springframework.boot.web.support.SpringBootServletInitializer;
  import org.springframework.scheduling.annotation.EnableAsync;
  
  @SpringBootApplication
  @EnableAsync
  public class MyRestApp extends SpringBootServletInitializer {
  
      @Override
      protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
          return configureApplication(builder);
      }
  
      public static void main(String[] args) throws JsonProcessingException {
          configureApplication(new SpringApplicationBuilder()).run(args);
      }
  
      private static SpringApplicationBuilder configureApplication(SpringApplicationBuilder builder) {
          return builder.sources(MyRestApp.class).bannerMode(Banner.Mode.OFF);
      }
  
  }
  </code></pre>
  
  <p>with MVC configured thus:</p>
  
  <pre><code>package my;
  
  import org.springframework.context.annotation.Bean;
  import org.springframework.context.annotation.Configuration;
  import org.springframework.core.task.AsyncTaskExecutor;
  import org.springframework.scheduling.annotation.EnableAsync;
  import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
  import org.springframework.web.servlet.config.annotation.AsyncSupportConfigurer;
  import org.springframework.web.servlet.config.annotation.CorsRegistry;
  import org.springframework.web.servlet.config.annotation.EnableWebMvc;
  import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
  import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
  
  @Configuration
  @EnableWebMvc
  @EnableAsync
  public class MyMvcConfiguration extends WebMvcConfigurerAdapter {
  
      @Override
      public void configureAsyncSupport(AsyncSupportConfigurer configurer) {
          configurer.setDefaultTimeout(-1);
          configurer.setTaskExecutor(asyncTaskExecutor());
      }
  
      @Bean
      public AsyncTaskExecutor asyncTaskExecutor() {
          ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
          executor.setCorePoolSize(2);
          executor.setMaxPoolSize(2);
          executor.setQueueCapacity(500);
          executor.setThreadNamePrefix("SpringAsyncThread-");
          executor.initialize();
          return executor;
      }
  
      @Bean
      public WebMvcConfigurer corsConfigurer() {
          return new WebMvcConfigurerAdapter() {
              @Override
              public void addCorsMappings(CorsRegistry registry) {
                  registry.addMapping("/**")
                          .allowedMethods("GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS");
              }
          };
      }
  
  }
  </code></pre>
  
  <p>Finally, the application is built and packaged using Maven with the following POM:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"&gt;
      &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
  
      &lt;groupId&gt;com.acme&lt;/groupId&gt;
      &lt;artifactId&gt;my-rest-app&lt;/artifactId&gt;
      &lt;version&gt;1.0.0-SNAPSHOT&lt;/version&gt;
      &lt;packaging&gt;war&lt;/packaging&gt;
      &lt;name&gt;my-rest-app&lt;/name&gt;
      &lt;description&gt;&lt;/description&gt;
      &lt;parent&gt;
          &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
          &lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;
          &lt;version&gt;1.4.1.RELEASE&lt;/version&gt;
          &lt;relativePath/&gt; &lt;!-- lookup parent from repository --&gt;
      &lt;/parent&gt;
      &lt;properties&gt;
          &lt;project.build.sourceEncoding&gt;UTF-8&lt;/project.build.sourceEncoding&gt;
          &lt;java.version&gt;1.8&lt;/java.version&gt;
      &lt;/properties&gt;
      &lt;dependencies&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
          &lt;/dependency&gt;       
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-tomcat&lt;/artifactId&gt;
              &lt;scope&gt;provided&lt;/scope&gt;
          &lt;/dependency&gt;
      &lt;/dependencies&gt;
      &lt;build&gt;
          &lt;resources&gt;
              &lt;resource&gt;
                  &lt;directory&gt;${project.basedir}/src/main/resources&lt;/directory&gt;
              &lt;/resource&gt;
              &lt;resource&gt;
                  &lt;directory&gt;${project.build.directory}/generated-resources&lt;/directory&gt;
              &lt;/resource&gt;
          &lt;/resources&gt;
          &lt;plugins&gt;
              &lt;plugin&gt;
                  &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
                  &lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;
              &lt;/plugin&gt;
          &lt;/plugins&gt;
      &lt;/build&gt;
  &lt;/project&gt;
  </code></pre>