Spring
How to configure Rollbar with Java Spring
Rollbar offers error monitoring integration for Spring apps. This guide will show you how to install and configure Rollbar for Spring. It will also provide examples of how to use the SDK along with advanced configurations.
Installation
Configure Gradle dependencies
Depending on your project type, add the appropriate dependency for gradle.
Spring Boot
If you have a Spring Boot project add the following to your dependencies
implementation('com.rollbar:rollbar-spring-boot-webmvc:1.9.0')
Spring Web MVC
For a Spring Web MVC project add the following to your dependencies
implementation('com.rollbar:rollbar-spring-webmvc:1.9.0')
Get your Access Token
Log into your Rollbar account dashboard. Go to Settings, then Project Access Tokens, and copy the post_server_item
access token. Make sure to keep this token secure.
Configure the Rollbar Bean
For a very simple configuration you can create a new RollbarConfig class such as the one below and add it to your project. Make sure to set your ACCESS_TOKEN and add your project package to the @ComponentScan.
@Configuration()
@EnableWebMvc
@ComponentScan({
// ADD YOUR PROJECT PACKAGE HERE
"com.rollbar.spring"
})
public class RollbarConfig {
/**
* Register a Rollbar bean to configure App with Rollbar.
*/
@Bean
public Rollbar rollbar() {
return new Rollbar(getRollbarConfigs("<ACCESS_TOKEN>"));
}
private Config getRollbarConfigs(String accessToken) {
// Reference ConfigBuilder.java for all the properties you can set for Rollbar
return RollbarSpringConfigBuilder.withAccessToken(accessToken)
.environment("development")
.codeVersion("1.0.0")
.build();
}
}
Usage
In the installation section, we registered a Rollbar bean and loaded the com.rollbar.spring components. At this point any exceptions raised by Spring will be handled by Rollbar. We also offer several methods to track other items at your convenience.
Logging a Specific Message
You can log your own messages anywhere in your app. For example, to log a debug
message:
rollbar.debug("Here is some debug message");
Add Context Information
You can pass user information as context like this:
rollbar.error(e);
HashMap<String,Object> map=new HashMap<String, Object>();
map.put("Id","123");
map.put("User Name","John Doe");
map.put("Email","[email protected]");
rollbar.log(e,map);
Exception Logging
If you wish to report data from the HttpServletRequest
, you may do so using the Rollbar error reporting methods. For example:
@Override
public String buildLogMessage(Exception e, HttpServletRequest req) {
System.out.println("Exception : "+e.toString());
rollbar.error(e);
return "MVC exception: " + e.getLocalizedMessage();
}
You can also report an error to Rollbar with a specific exception block. For example, to log any Exception
add the following:
try {
String test = null;
test.toString();
} catch(Exception e) {
rollbar.error(e,"This is a null pointer exception");
}
Additional Configuration Options
The Rollbar object allows for additional configs. Reference the config object here in our JavaDoc to learn what you can configure.
Examples
Here are some examples which demonstrate how to add the configuration into the Rollbar object.
Configure a new Request Provider
Add a RequestProvider
to capture metadata about the request, such as the remote IP.
// Example add a `RequestProvider` to capture metadata about the request
RequestProvider requestProvider = new RequestProvider
.Builder()
// To use a header to resolve the source IP address uncomment below line.
//.userIpHeaderName("X-Forwarded-For")
.build();
return new Rollbar(RollbarSpringConfigBuilder.withAccessToken("ACCESS_TOKEN")
.environment("development")
.codeVersion("1.0.1")
// Add the custom requestProvider to the Rollbar instance
.request(requestProvider)
.build());
Configure a proxy with Rollbar
return new Rollbar(RollbarSpringConfigBuilder.withAccessToken("ACCESS_TOKEN")
.environment("development")
.codeVersion("1.0.1")
.proxy(JAVA_PROXY_OBJECT)
.build());
JavaDocs
You can reference the Spring Boot and Spring WebMVC Java Docs here:
Updated 28 days ago