Spring boot error handling with Rollbar

Jhamukul
4 min readJan 8, 2022

Rollbar is a cloud-based bug tracking and monitoring solution that caters to organizations of all sizes. Rollbar supports multiple programming languages and frameworks like Java, Java Spring, JavaScript, Python, . NET, Drupal, Wordpress and Pyramid. The Rollbar service will alert you of problems with your code and help you understand them in a ways never possible before.

Lets configure rollbar with springboot. don’t worry its easy to configure, so relax.

Step 1: Create Rollbar Account

After signup you ll get home page like this

figure 1: Rollbar home page post sign up

Step 2: Click on Projects

Figure 2: Post click on projects

Step 3: Add new project

Check top right corner, click on “New Project” button

figure 3: Add project page

I am adding project name as “RollbarSpringbootDemo”. you can add project name along with timezone etc etc.

And click on “Create Project

Step 4: Select SDK (like Java Spring, Go etc)

figure 4: Select and add sdk page

It is mandate to select sdk with project.

For Spring boot or Spring select “Java Spring” and click on “continue”

figure 5: sdk selection page

Step 5: Final Project step page

figure 6: project setup final page

This is final project setup page leave it and go back to http://rollbar.com

Rollbar console now looks like

figure 7: rollbar console

Step 6: Get Project Access Token

figure 8: project access token detail page

To get project access token click on side bar “projects” button and “select your project” and later “project access token” button. Project access token help you to connect spring boot application to the specific rollbar project.

Copy “post_server_item” token

Step 6: Create a Spring boot web project

Step 7: Add rollbar dependency to the pom.xml/build.gradle

https://mvnrepository.com/artifact/com.rollbar/rollbar-spring-boot-webmvc

implementation 'com.rollbar:rollbar-spring-boot-webmvc:1.8.1'

Step 8: Add Rollbar bean

We need post_server_item token right here to connect to rollbar project. If you don’t have, Please check step 6 and set token in withAccessToken(token)

@Configuration
public class RollbarConfiguration {
private final String token = "7dea12a58a414d7ab69456ea2c280ae9";
@Bean(name = "rollbar")
public Rollbar getRollbar() {
return new Rollbar(getConfig());
}

public Config getConfig() {
return ConfigBuilder.withAccessToken(token)
.environment().build();
}

}

Step 9: Add Rollbar Utility/Service to Spring boot application

Create a concrete class and add methods as per your requirements

public interface ExternalErrorHandlerService {
void sendError(Exception e);
void sendError(Exception e, String message);
void sendInfo(Exception e);
void sendInfo(Exception e, String message);
void sendDebug(Exception e);
void sendDebug(Exception e, String message);
}

Rollbar utility or service

package com.rollbar.rollbardemo.services.error.impl;

import com.rollbar.notifier.Rollbar;
import com.rollbar.rollbardemo.services.error.ExternalErrorHandlerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

@Service
public class RollbarErrorHandlerService implements ExternalErrorHandlerService {
private final Rollbar rollbar;

@Autowired
public RollbarErrorHandlerService(@Qualifier("rollbar") Rollbar rollbar) {
this.rollbar = rollbar;
}

@Override
public void sendError(Exception e) {
rollbar.error(e);
}

@Override
public void sendError(Exception e, String message) {
rollbar.error(e,message);
}

@Override
public void sendInfo(Exception e) {
rollbar.info(e);
}

@Override
public void sendInfo(Exception e, String message) {
rollbar.info(e,message);
}

@Override
public void sendDebug(Exception e) {
rollbar.debug(e);
}

@Override
public void sendDebug(Exception e, String message) {
rollbar.debug(e,message);
}
}

Congrats !!! Now your application ready to use rollbar dashboard

Step 10: Select project and enviroment

figure 9: Select projects and environments
figure 10: Rollbar dashboard after selecting project

You can run sample application from github.

You will get sample api documentation which push errors into your rollbar dashboard.

figure 11: Github dummy project swagger-ui.html

Thanks for reading !!!

--

--