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.
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>
Add the Moesif dependency to your project's build.gradle file:
dependencies {
compile 'com.moesif.servlet:moesif-servlet-jakarta:2.2.3'
}
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.
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);
}
}
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
-
Clone the repository
git clone https://github.com/Moesif/moesif-servlet
cd moesif-servlet/examples/spring-boot-starter-example-jakarta ```
-
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
-
Compile the example
mvn clean install
-
Run
mvn spring-boot:run
-
Using Postman or CURL, make a few API calls to
http://localhost:8080/api
or the port that Spring Boot is running on. -
Verify the API calls are logged to your Moesif account
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
- Manually clone the git repo
- Invoke
mvn clean install -U -Dgpg.skip
if you haven't done so. - 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
- From terminal/cmd navigate to the root directory of the moesif-servlet-jakarta.
- Invoke
mvn -Dtest=MoesifServletTests test
to run the tests.