Merge pull request #6961 from pkoli/master
BAEL-2886 Mongodb spring session tutorial
This commit is contained in:
commit
e5f3b0f1e6
|
@ -0,0 +1,43 @@
|
||||||
|
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.http.HttpEntity;
|
||||||
|
import org.springframework.http.HttpHeaders;
|
||||||
|
import org.springframework.http.HttpMethod;
|
||||||
|
import org.springframework.session.data.mongo.MongoOperationsSessionRepository;
|
||||||
|
import org.springframework.test.context.junit4.SpringRunner;
|
||||||
|
import springsessionmongodb.SpringSessionMongoDBApplication;
|
||||||
|
|
||||||
|
import java.util.Base64;
|
||||||
|
|
||||||
|
|
||||||
|
@RunWith(SpringRunner.class)
|
||||||
|
@SpringBootTest(classes = SpringSessionMongoDBApplication.class, webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
|
||||||
|
public class SpringSessionMongoDBIntegrationTest {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private MongoOperationsSessionRepository repository;
|
||||||
|
|
||||||
|
private TestRestTemplate restTemplate = new TestRestTemplate();
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenEndpointIsCalledTwiceAndResponseIsReturned_whenMongoDBIsQueriedForCount_thenCountMustBeSame() {
|
||||||
|
HttpEntity<String> response = restTemplate.
|
||||||
|
exchange("http://localhost:" + 8080, 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());
|
||||||
|
}
|
||||||
|
|
||||||
|
private String getSessionId(String set_cookie) {
|
||||||
|
return new String(Base64.getDecoder().decode(set_cookie.split(";")[0].split("=")[1]));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -17,6 +17,7 @@
|
||||||
<modules>
|
<modules>
|
||||||
<module>spring-session-jdbc</module>
|
<module>spring-session-jdbc</module>
|
||||||
<module>spring-session-redis</module>
|
<module>spring-session-redis</module>
|
||||||
|
<module>spring-session-mongodb</module>
|
||||||
</modules>
|
</modules>
|
||||||
|
|
||||||
</project>
|
</project>
|
|
@ -0,0 +1,4 @@
|
||||||
|
This module is for Spring Session with MONGO DB tutorial.
|
||||||
|
Jira BAEL-2886
|
||||||
|
|
||||||
|
### Relevant Articles:
|
|
@ -0,0 +1,57 @@
|
||||||
|
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<groupId>com.baeldung</groupId>
|
||||||
|
<artifactId>spring-session-mongodb</artifactId>
|
||||||
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
|
<name>spring-session-mongodb</name>
|
||||||
|
<packaging>jar</packaging>
|
||||||
|
<description>Spring Session with MongoDB tutorial</description>
|
||||||
|
|
||||||
|
<parent>
|
||||||
|
<artifactId>parent-boot-2</artifactId>
|
||||||
|
<groupId>com.baeldung</groupId>
|
||||||
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
|
<relativePath>../../parent-boot-2</relativePath>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-web</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.session</groupId>
|
||||||
|
<artifactId>spring-session-data-mongodb</artifactId>
|
||||||
|
<version>${spring-session-data-mongodb.version}</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-data-mongodb</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-test</artifactId>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<spring-session-data-mongodb.version>2.1.3.RELEASE</spring-session-data-mongodb.version>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
|
||||||
|
</project>
|
|
@ -0,0 +1,12 @@
|
||||||
|
package com.baeldung.springsessionmongodb;
|
||||||
|
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
|
||||||
|
@SpringBootApplication
|
||||||
|
public class SpringSessionMongoDBApplication {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SpringApplication.run(SpringSessionMongoDBApplication.class, args);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,28 @@
|
||||||
|
package com.baeldung.springsessionmongodb.controller;
|
||||||
|
|
||||||
|
import javax.servlet.http.HttpSession;
|
||||||
|
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
public class SpringSessionMongoDBController {
|
||||||
|
|
||||||
|
@GetMapping("/")
|
||||||
|
public ResponseEntity<Integer> count(HttpSession session) {
|
||||||
|
|
||||||
|
Integer counter = (Integer) session.getAttribute("count");
|
||||||
|
|
||||||
|
if (counter == null) {
|
||||||
|
counter = 1;
|
||||||
|
} else {
|
||||||
|
counter += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
session.setAttribute("count", counter);
|
||||||
|
|
||||||
|
return ResponseEntity.ok(counter);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,6 @@
|
||||||
|
spring.session.store-type=mongodb
|
||||||
|
server.port=8080
|
||||||
|
|
||||||
|
spring.data.mongodb.host=localhost
|
||||||
|
spring.data.mongodb.port=27017
|
||||||
|
spring.data.mongodb.database=springboot-mongo
|
|
@ -0,0 +1,13 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<configuration>
|
||||||
|
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||||
|
<encoder>
|
||||||
|
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||||
|
</pattern>
|
||||||
|
</encoder>
|
||||||
|
</appender>
|
||||||
|
|
||||||
|
<root level="INFO">
|
||||||
|
<appender-ref ref="STDOUT" />
|
||||||
|
</root>
|
||||||
|
</configuration>
|
|
@ -0,0 +1,42 @@
|
||||||
|
package com.baeldung.springsessionmongodb;
|
||||||
|
|
||||||
|
import java.util.Base64;
|
||||||
|
|
||||||
|
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.http.HttpEntity;
|
||||||
|
import org.springframework.http.HttpHeaders;
|
||||||
|
import org.springframework.http.HttpMethod;
|
||||||
|
import org.springframework.session.data.mongo.MongoOperationsSessionRepository;
|
||||||
|
import org.springframework.test.context.junit4.SpringRunner;
|
||||||
|
|
||||||
|
|
||||||
|
@RunWith(SpringRunner.class)
|
||||||
|
@SpringBootTest(classes = SpringSessionMongoDBApplication.class, webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
|
||||||
|
public class SpringSessionMongoDBIntegrationTest {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private MongoOperationsSessionRepository repository;
|
||||||
|
|
||||||
|
private TestRestTemplate restTemplate = new TestRestTemplate();
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenEndpointIsCalledTwiceAndResponseIsReturned_whenMongoDBIsQueriedForCount_thenCountMustBeSame() {
|
||||||
|
HttpEntity<String> response = restTemplate.
|
||||||
|
exchange("http://localhost:" + 8080, 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());
|
||||||
|
}
|
||||||
|
|
||||||
|
private String getSessionId(String cookie) {
|
||||||
|
return new String(Base64.getDecoder().decode(cookie.split(";")[0].split("=")[1]));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,17 @@
|
||||||
|
package org.baeldung;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
import org.springframework.test.context.junit4.SpringRunner;
|
||||||
|
|
||||||
|
import com.baeldung.springsessionmongodb.SpringSessionMongoDBApplication;
|
||||||
|
|
||||||
|
@RunWith(SpringRunner.class)
|
||||||
|
@SpringBootTest(classes = SpringSessionMongoDBApplication.class)
|
||||||
|
public class SpringContextIntegrationTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenSpringContextIsBootstrapped_thenNoExceptions() {
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue