I've been facing this error for a long time, I was trying to configure jetty in my demo project where I was trying to persist sessions in MongoDB.
This is the Main Error
Caused by: java.lang.IllegalAccessError: class com.mongodb.Bytes cannot access its superclass org.bson.BSON (com.mongodb.Bytes and org.bson.BSON are in unnamed module of loader 'app')at java.base/java.lang.ClassLoader.defineClass1(Native Method) ~[na:na]
My Config Class
package com.example.demo.config;
import org.eclipse.jetty.nosql.mongodb.MongoSessionDataStoreFactory; import org.eclipse.jetty.server.Server; import org.eclipse.jetty.server.session.DefaultSessionCache; import org.eclipse.jetty.server.session.SessionCache; import org.eclipse.jetty.server.session.SessionHandler; import org.springframework.boot.web.embedded.jetty.JettyServerCustomizer; import org.springframework.boot.web.embedded.jetty.JettyServletWebServerFactory; import org.springframework.boot.web.server.WebServerFactoryCustomizer; import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration;@Configuration public class JettyConfig {
@Bean
public WebServerFactoryCustomizer<ConfigurableServletWebServerFactory
> webServerFactoryCustomizer() {
return factory -> {
if (factory instanceof JettyServletWebServerFactory) {
((JettyServletWebServerFactory) factory).addServerCustomizers(jettyServerCustomizer());
}
};
}
@Bean
public JettyServerCustomizer jettyServerCustomizer() {
return (Server server) -> {
SessionHandler sessionHandler = null;
try {
sessionHandler = sqlSessionHandler();
} catch (Exception e) {
throw new RuntimeException(e);
}
server.setSessionIdManager(sessionHandler.getSessionIdManager());
server.setHandler(sessionHandler);
};
}
public static SessionHandler sqlSessionHandler() {
SessionHandler sessionHandler = new SessionHandler();
try {
SessionCache sessionCache = new DefaultSessionCache(sessionHandler);
sessionCache.setSessionDataStore(
mongoDataStoreFactory("localhost","bson", "sessions").getSessionDataStore(sessionHandler)
);
sessionHandler.setSessionCache(sessionCache);
sessionHandler.setHttpOnly(true);
} catch (Exception e) {
e.printStackTrace();
}
return sessionHandler;
}
private static MongoSessionDataStoreFactory mongoDataStoreFactory(String host, String dbName, String collectionName) {
MongoSessionDataStoreFactory mongoSessionDataStoreFactory = new MongoSessionDataStoreFactory();
mongoSessionDataStoreFactory.setHost(host);
mongoSessionDataStoreFactory.setDbName(dbName);
mongoSessionDataStoreFactory.setCollectionName(collectionName);
return mongoSessionDataStoreFactory;
}
}
My Pom File
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for bson</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-nosql</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<image>
<builder>paketobuildpacks/builder-jammy-base:latest</builder>
</image>
</configuration>
</plugin>
</plugins>
</build>
I did alot of research about this but no luck yet in resolving the error. Here i am using old version of spring boot and java as i have to resolve this error in my old project. I'm thinking that there is a conflict between driver from jetty noSql dependency and spring boot starter mongoDb Dependency