Skip to Content

Treblle Docs

Integrate TreblleSDKsJavaTreblle with JavaX

Treblle with JavaX

Requirements

  • JavaX Servlet API 3.1.0
  • JavaX RS API 2.1.1
  • Java 8+

Installation

Add the following dependency to your project:

Maven

<dependency> <groupId>com.treblle</groupId> <artifactId>treblle-javax</artifactId> <version>1.0.2</version> </dependency>

Gradle

implementation 'com.treblle:treblle-javax:1.0.2'

Tip

For all available versions and metadata, visit the Maven Central Repository .

Getting started

First, create a FREE account on treblle.com  to get an API key and SDK Token.

Basic Configuration

To set up Treblle in your JavaX application, you must configure the filter to intercept requests and responses. Here’s a basic configuration example:

// Import necessary classes import com.treblle.javax.filter.TreblleFilter; import javax.servlet.FilterRegistration; import javax.servlet.ServletContext; // In your web application initializer or servlet context listener public void onStartup(ServletContext servletContext) { // Register the Treblle filter FilterRegistration.Dynamic treblleFilter = servletContext.addFilter("treblleFilter", TreblleFilter.class); // Set initialization parameters treblleFilter.setInitParameter("apiKey", "<YOUR_API_KEY>"); treblleFilter.setInitParameter("sdkToken", "<YOUR_SDK_TOKEN>"); // Add filter mapping - apply to all API endpoints treblleFilter.addMappingForUrlPatterns(null, false, "/*"); }

JAX-RS Configuration

If you’re using JAX-RS, you can register the Treblle filter as follows:

// Import necessary classes import com.treblle.javax.filter.TreblleContainerFilter; import javax.ws.rs.core.Application; import java.util.Set; // In your JAX-RS application class public class MyApplication extends Application { @Override public Set<Object> getSingletons() { Set<Object> singletons = new HashSet<>(); // Create and configure the Treblle filter TreblleContainerFilter filter = new TreblleContainerFilter(); filter.setApiKey("<YOUR_API_KEY>"); filter.setsdkToken("<YOUR_SDK_TOKEN>"); // Add the filter to your application singletons.add(filter); return singletons; } }

That’s it! Your API requests and responses are now being sent to your Treblle Dashboard. You’ll get features like auto-documentation, real-time request/response monitoring, error tracking, and more.

Filter Configuration Options

The Treblle filter supports the following configuration options:

// Set the API key required for authentication with Treblle filter.setInitParameter("apiKey", "<YOUR_API_KEY>"); // Set the SDK Token where your API data will be sent filter.setInitParameter("sdkToken", "<YOUR_SDK_TOKEN>"); // Optional: Specify custom URL patterns (comma-separated) to monitor filter.setInitParameter("urlPatterns", "/api/v1/*,/api/v2/*"); // Optional: Add additional field masking keywords (comma-separated) filter.setInitParameter("maskingKeywords", "secretField,sensitiveData"); // Optional: Enable debug mode for additional logging filter.setInitParameter("debug", "true"); // Optional: Set custom Treblle endpoint filter.setInitParameter("endpoint", "https://rocknrolla.treblle.com");

Running Treblle only for specific URL patterns

By default, Treblle applies its filter to all requests (/*). If you want to run Treblle only for specific URL patterns, you can specify them during filter registration:

// Register the filter only for API endpoints treblleFilter.addMappingForUrlPatterns(null, false, "/api/*");

Or use the ‘urlPatterns’ configuration parameter:

treblleFilter.setInitParameter("urlPatterns", "/api/v1/*,/api/v2/*");

Note

Configure URL patterns carefully to ensure you’re only monitoring the endpoints you need, which can help reduce overhead and keep your Treblle dashboard focused on relevant traffic.

Last updated on