Skip to content

Latest commit

 

History

History

moesif-servlet-jakarta

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 

Moesif Servlet Jakarta SDK

Built For Latest Version Software License Source Code

Introduction

moesif-servlet-jakarta is a Jakarta Servlet Filter that logs incoming API calls and sends to Moesif for API analytics and monitoring.

The SDK is implemented as a Jakarta Servlet Filter without importing framework specific dependencies. Any framework built on Java Jakarta Servlet API such as Spring, Struts, Jersey, etc can use this SDK with minimal configuration.

Source Code on GitHub

How to install

Maven users

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

<dependency>
    <groupId>com.moesif.servlet</groupId>
    <artifactId>moesif-servlet-jakarta</artifactId>
    <version>2.2.3</version>
</dependency>

Gradle users

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

dependencies {   
    compile 'com.moesif.servlet:moesif-servlet-jakarta:2.2.3'
}

How to use

Different Java web frameworks have different way of configuring filters. Go to your specific framework's instructions below:

Your Moesif Application Id can be found in the Moesif Portal. After signing up for a Moesif account, your Moesif Application Id will be displayed during the onboarding steps.

You can always find your Moesif Application Id at any time by logging into the Moesif Portal, click on the top right menu, and then clicking Installation.

Spring Boot Starter Example Jakarta

In your Spring configuration file, install the Moesif Filter object.

import com.moesif.servlet.MoesifFilter;

import jakarta.servlet.Filter;
import org.springframework.web.servlet.config.annotation.*;
import org.springframework.context.annotation.*;
import org.springframework.http.converter.*;

@Configuration
public class MyConfig implements WebMvcConfigurer {

  @Bean
  public Filter moesifFilter() {
    return new MoesifFilter("Your Moesif Application Id");
  }
}

To customize the filter, pass in a object that implements MoesifConfiguration such as MoesifConfigurationAdapter For details regarding MoesifConfiguration, see the configuration options.

@Configuration
public class MyConfig implements WebMvcConfigurer {

  MoesifConfiguration config = new MoesifConfigurationAdapter() {
    @Override
    public String getSessionToken(HttpServletRequest request, HttpServletResponse response) {
      return request.getHeader("Authorization");
    }
  };

  @Bean
  public Filter moesifFilter() {
    return new MoesifFilter("Your Moesif Application Id", config);
  }
}

Running the Spring Boot example

In order to run this example you will need to have Java 17+ and Maven installed.

Before starting, check that your maven version is 3.0.x or above:

mvn -v
  1. Clone the repository

    git clone https://github.com/Moesif/moesif-servlet

cd moesif-servlet/examples/spring-boot-starter-example-jakarta ```

  1. Update MyConfig to use your own Moesif ApplicationId (Register for an account on moesif.com)

    vim src/main/java/com/moesif/servlet/spring/MyConfig.java
  2. Compile the example

    mvn clean install
  3. Run

    mvn  spring-boot:run
  4. Using Postman or CURL, make a few API calls to http://localhost:8080/api or the port that Spring Boot is running on.

  5. Verify the API calls are logged to your Moesif account

Building moesif-servlet locally

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

cd moesif-servlet-jakarta
mvn clean install

How to test

  1. Manually clone the git repo
  2. Invoke mvn clean install -U -Dgpg.skip if you haven't done so.
  3. Add your own application id to 'src/test/java/com/moesif/servlet/MoesifServletTests.java'. You can find your Moesif Application Id from Moesif Dashboard -> Top Right Menu -> Installation
  4. From terminal/cmd navigate to the root directory of the moesif-servlet-jakarta.
  5. Invoke mvn -Dtest=MoesifServletTests test to run the tests.