Integration Guide - Java Servlet (Spring, Jersey)

Introduction

moesif-springrequest is a Java SDK for capturing outgoing API traffic and sending to Moesif for analysis.

The SDK is implemented as a Spring Request Interceptor.

For more info, visit Moesif’s Developer Docs

How to install

Maven users

Add the Moesif dependency to your project’s pom.xml file:

<!-- Include jcenter repository if you don't already have it. -->
<repositories>
    <repository>
        <id>jcenter</id>
        <url>https://jcenter.bintray.com/</url>
    </repository>
</repositories>
    
<dependency>
    <groupId>com.moesif.springrequest</groupId>
    <artifactId>moesif-springrequest</artifactId>
    <version>1.0.0</version>
</dependency>

Gradle users

Add the Moesif dependency to your project’s build.gradle file:

// Include jcenter repository if you don't already have it.
repositories {
    jcenter()
}
 
dependencies {   
    compile 'com.moesif.springrequest:moesif-springrequest:1.0.0'
}

How to use

1. Inject The Moesif Interceptor

RestTemplate template = new RestTemplate();

final List<ClientHttpRequestInterceptor> interceptors = new ArrayList<ClientHttpRequestInterceptor>();

interceptors.add(new MoesifSpringRequestInterceptor("{{props.appId}}"));

template.setInterceptors(interceptors);

2. Make HTTP requests using the same RestTemplate

template.exchange(
  "https://jsonplaceholder.typicode.com/posts",
  HttpMethod.POST,
  "{\"id\": \"1\"}",
  String.class
);

Configuration options

To configure the filter, extend the MoesifRequestConfiguration class to override a few config params.

1. public boolean skip(HttpRequest request, ClientHttpResponse response)

Return true if you want to skip logging a request to Moesif i.e. to skip boring requests like health probes.

  @Override
  public boolean skip(HttpRequest request, ClientHttpResponse response) {
    // Skip logging health probes
    return request.getURI().toString().contains("health/probe");
  }

2. public EventModel maskContent(EventModel eventModel)

If you want to remove any sensitive data in the HTTP headers or body before sending to Moesif, you can do so with maskContent

3. public String identifyUser(HttpRequest request, ClientHttpResponse response)

Highly recommended. Even though Moesif automatically detects the end userId if possible, setting this configuration ensures the highest accuracy with user attribution.

  @Override
  public String identifyUser(HttpRequest request, ClientHttpResponse response) {
    return request.getHeaders().getFirst("user");
  }

4. public String getSessionToken(HttpRequest request, ClientHttpResponse response)

Moesif automatically detects the end user’s session token or API key, but you can manually define the token for finer control.

  @Override
  public String getSessionToken(HttpRequest request, ClientHttpResponse response) {
    return request.getHeaders().getFirst("Authorization");
  }

6. public String getApiVersion(HttpRequest request, ClientHttpResponse response)

You can optionally add an API version to the event.

  @Override
  public String getApiVersion(HttpRequest request, ClientHttpResponse response) {
    return request.getHeaders().getFirst("X-Api-Version");
  }

Building moesif-springrequest locally

If you are contributing to moesif-springrequest, you can build it locally and install in local Maven Repo:

cd moesif-springrequest
mvn clean install

Enable Debug Messsages

@Override
public boolean debug = true;

Other integrations

To view more more documentation on integration options, please visit the Integration Options Documentation.