Last active
August 10, 2021 08:12
-
-
Save mwisnicki/6df71b2b5f7712306fca27bf34c24e94 to your computer and use it in GitHub Desktop.
Testing streaming abort error handling in Tomcat and Spring Boot
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package demo; | |
import org.apache.catalina.startup.Tomcat; | |
import org.springframework.boot.SpringApplication; | |
import org.springframework.boot.autoconfigure.SpringBootApplication; | |
import org.springframework.boot.web.servlet.ServletRegistrationBean; | |
import org.springframework.context.annotation.Bean; | |
import org.springframework.web.bind.annotation.GetMapping; | |
import org.springframework.web.bind.annotation.RestController; | |
import javax.servlet.ServletException; | |
import javax.servlet.http.HttpServlet; | |
import javax.servlet.http.HttpServletRequest; | |
import javax.servlet.http.HttpServletResponse; | |
import java.io.File; | |
import java.io.IOException; | |
@SpringBootApplication | |
@RestController | |
public class AbortedStreamingDemo { | |
static void respond(HttpServletResponse resp) throws IOException { | |
resp.setStatus(200); | |
resp.setHeader("Content-Disposition", "attachment"); | |
resp.setContentType("application/octet-stream"); | |
var out = resp.getOutputStream(); | |
out.flush(); | |
if (true) throw new RuntimeException("simulated error"); | |
// status is already sent here, can't change it! | |
} | |
static class DemoServlet extends HttpServlet { | |
@Override | |
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { | |
respond(resp); | |
} | |
} | |
@GetMapping("/controller") | |
void serve(HttpServletResponse resp) throws IOException { | |
respond(resp); | |
} | |
@Bean | |
ServletRegistrationBean<HttpServlet> demoServletServletRegistrationBean() { | |
return new ServletRegistrationBean<>(new DemoServlet(), "/servlet"); | |
} | |
static class SpringApp { | |
public static void main(String[] args) { | |
SpringApplication.run(AbortedStreamingDemo.class, args); | |
} | |
} | |
static class TomcatApp { | |
public static void main(String[] args) throws Exception { | |
var tomcat = new Tomcat(); | |
tomcat.setPort(8080); | |
tomcat.addContext("", new File(".").getAbsolutePath()); | |
tomcat.addServlet("", "demo", new DemoServlet()).addMapping("/servlet"); | |
tomcat.getConnector(); // FFS | |
tomcat.start(); | |
tomcat.getServer().await(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> | |
<modelVersion>4.0.0</modelVersion> | |
<parent> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-starter-parent</artifactId> | |
<version>2.5.2</version> | |
<relativePath/> <!-- lookup parent from repository --> | |
</parent> | |
<groupId>com.example</groupId> | |
<artifactId>demo</artifactId> | |
<version>0.0.1-SNAPSHOT</version> | |
<name>demo</name> | |
<description>Demo project for Spring Boot</description> | |
<properties> | |
<java.version>11</java.version> | |
</properties> | |
<dependencies> | |
<dependency> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-starter-web</artifactId> | |
</dependency> | |
</dependencies> | |
<build> | |
<plugins> | |
<plugin> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-maven-plugin</artifactId> | |
</plugin> | |
</plugins> | |
</build> | |
</project> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
With tomcat the response to
curl -iv --raw http://localhost:8080/servlet
is an error as expected:But in Spring Boot both endpoints respond with terminal "0":