Get stack trace of Exception through spring rest template
I need the whole stack trace of it in Service 1. How to get it ?
So there are ways to get it , in essence you have to implement.
You can get your relevant exception message/trace in JSON response
from Service 2
. That is , when there is any exception
at Service 2
end then , we can configure response to send relevant exception information.
In this post there are 3 answers explaining different ways to achieve, also this one. Now on :
Does the stack trace even get passed to Service 1 by Spring ?
Normally any unhandled/runtime exception
thrown when processing a web-request
causes the server to return an HTTP 500
response.
So the answer is spring does not transfer the stack trace to Service 1
rather respond with error HTTP 500
and the most likely message of your exception
.
However, any exception that you write yourself can be annotated with the @ResponseStatus
annotation (which supports all the HTTP status codes defined by the HTTP
specification).
When an annotated exception
is thrown from a controller method, and not handled elsewhere, it will automatically cause the appropriate HTTP response
to be returned with the specified status-code and with the message/trace written.
For example,
@ResponseStatus(value=HttpStatus.NOT_FOUND, reason="No such Account") // 404
public class AddressNotFoundException extends RuntimeException {
// ...
}
And here is a controller method using it:
@RequestMapping(value="/account/{id}", method=GET)
public String showOrder(@PathVariable("id") long id, Model model) {
Account account = accountServices.findAccountById(id);
if (account == null) throw new AddressNotFoundException(id);
model.addAttribute(account);
return "accountDetail";
}
A familiar HTTP 404
response will be returned if the URL handled by this method includes an unknown account id.
Hope this helps.
To summarize a straight-forward approach it is not much more than adding Spring Boot's Zipkin
and Sleuth
starters into your pom.xml
s to enable log tracing in both directions between multiple apps…
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-sleuth</artifactId>
<version>${spring-cloud-sleuth.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-sleuth</artifactId>
</dependency>
</dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zipkin</artifactId>
</dependency>
…and configure your log pattern in your application.properties
to reflect the SpanId
s and TraceId
s which Sleuth injected in your requests…
logging.pattern.level=[%X{X-B3-TraceId}/%X{X-B3-SpanId}] %-5p [%t] %C{2} - %m%n
You can also compare your own progress with a running example on Openzipkin's Github account.
Maybe you could give it a try and show us your experiences and progress with it!