JAVA-32063: Migrate spring-session-mongodb to spring boot 3 (#16184)

This commit is contained in:
Harry9656 2024-03-28 19:36:23 +01:00 committed by GitHub
parent ec6524f294
commit 1f5e5f0752
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 54 additions and 31 deletions

View File

@ -11,9 +11,9 @@
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-boot-2</artifactId>
<artifactId>parent-boot-3</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../../../parent-boot-2</relativePath>
<relativePath>../../../parent-boot-3</relativePath>
</parent>
<dependencies>
@ -36,8 +36,8 @@
</dependency>
<dependency>
<groupId>de.flapdoodle.embed</groupId>
<artifactId>de.flapdoodle.embed.mongo</artifactId>
<scope>test</scope>
<artifactId>de.flapdoodle.embed.mongo.spring3x</artifactId>
<version>${embedded-mongodb.version}</version>
</dependency>
</dependencies>
@ -50,4 +50,8 @@
</plugins>
</build>
<properties>
<embedded-mongodb.version>4.12.2</embedded-mongodb.version>
</properties>
</project>

View File

@ -0,0 +1,22 @@
package com.baeldung.springsessionmongodb;
import java.time.Duration;
import org.springframework.context.annotation.Bean;
import org.springframework.session.data.mongo.JdkMongoSessionConverter;
import org.springframework.session.data.mongo.config.annotation.web.http.EnableMongoHttpSession;
import org.springframework.session.web.http.DefaultCookieSerializer;
@EnableMongoHttpSession
public class HttpSessionConfig {
@Bean
public JdkMongoSessionConverter jdkMongoSessionConverter() {
return new JdkMongoSessionConverter(Duration.ofMinutes(30));
}
@Bean
public DefaultCookieSerializer customCookieSerializer() {
DefaultCookieSerializer cookieSerializer = new DefaultCookieSerializer();
cookieSerializer.setUseHttpOnlyCookie(false);
return cookieSerializer;
}
}

View File

@ -4,7 +4,7 @@ import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpSession;
import jakarta.servlet.http.HttpSession;
@RestController
public class SpringSessionMongoDBController {

View File

@ -1,4 +1,4 @@
spring.session.store-type=mongodb
#spring.session.store-type=mongodb
server.port=8080
spring.data.mongodb.host=localhost

View File

@ -1,16 +1,15 @@
package com.baeldung;
import com.baeldung.springsessionmongodb.SpringSessionMongoDBApplication;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = SpringSessionMongoDBApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
import com.baeldung.springsessionmongodb.SpringSessionMongoDBApplication;
@SpringBootTest(classes = SpringSessionMongoDBApplication.class)
public class SpringContextTest {
@Test
public void whenSpringContextIsBootstrapped_thenNoExceptions() {
// Ensuring the context is spring boot application is started.
}
}

View File

@ -1,24 +1,21 @@
package com.baeldung.springsessionmongodb;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.session.data.mongo.MongoIndexedSessionRepository;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.Base64;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.boot.test.web.server.LocalServerPort;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.session.Session;
import org.springframework.session.data.mongo.MongoIndexedSessionRepository;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = SpringSessionMongoDBApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class SpringSessionMongoDBIntegrationTest {
class SpringSessionMongoDBIntegrationTest {
@LocalServerPort
private int port;
@ -26,17 +23,18 @@ public class SpringSessionMongoDBIntegrationTest {
@Autowired
private MongoIndexedSessionRepository repository;
private TestRestTemplate restTemplate = new TestRestTemplate();
private final TestRestTemplate restTemplate = new TestRestTemplate();
@Test
public void givenEndpointIsCalledTwiceAndResponseIsReturned_whenMongoDBIsQueriedForCount_thenCountMustBeSame() {
void givenEndpointIsCalledTwiceAndResponseIsReturned_whenMongoDBIsQueriedForCount_thenCountMustBeSame() {
HttpEntity<String> response = restTemplate
.exchange("http://localhost:" + port, HttpMethod.GET, null, String.class);
HttpHeaders headers = response.getHeaders();
String set_cookie = headers.getFirst(HttpHeaders.SET_COOKIE);
Assert.assertEquals(response.getBody(),
repository.findById(getSessionId(set_cookie)).getAttribute("count").toString());
Session sessionById = repository.findById(getSessionId(set_cookie));
Assertions.assertEquals(response.getBody(), sessionById.getAttribute("count").toString());
}
private String getSessionId(String cookie) {

View File

@ -1,4 +1,4 @@
#To use a randomly allocated free port during tests to avoid port conflict across tests
spring.data.mongodb.port=0
de.flapdoodle.mongodb.embedded.version=5.0.5
spring.mongodb.embedded.version=4.4.9