I am experiencing what I consider a strange behaviour for a Java application. I am using Spring Boot 1.4.1 to develop a REST Service. Due to a bug in my code, an invocation to the service results in an OutOfMemoryError
.
Surprisingly, the service responds to the request that generates the error with the following message:
{
"timestamp": 1487862480405,
"status": 500,
"error": "Internal Server Error",
"exception": "java.lang.OutOfMemoryError",
"message": "No message available",
"path": "/entity/exportCsv"
}
So far so good. What is more surprisingly is that the REST service does not shutdown after experiencing the Error
. Someone said that after an Error
the best thing to do is to properly log it and shutdown everything. The presence of an error should mean that the system is in an unrecoverable state.
Why does Spring MVC adopt such a strange policy in case of Error
? Is it possible to force the exit from the application (the REST service) after an Error
happened?
If you want to reproduce the above use case, just use the following code.
@RestController
@RequestMapping("/entity")
class Controller {
@RequestMapping(value = "/exportCsv", method = RequestMethod.GET)
ResponseEntity exportCsv() {
if (true)
throw new OutOfMemoryError();
return null;
}
}
Thanks to all.
EDIT: if you do think that catching an Error
is a normal way to develop Java applications, please, try to have a look to these references:
- When to catch java.lang.Error?
- Why Catching Throwable or Error is bad?
- And many other...
OutOfMemoryError
. Related to this, I am asking if someone knows why Spring implements the above policy. Thanks. – Sanmiguel