Skip to content

Commit df2c6ab

Browse files
committed
Fix Spring Data Querydsl example.
The io.github.jpenren:thymeleaf-spring-data-dialect dependency was out of sync with thymeleaf causing an error when you tried to access the index page. Additionally Added a TestApplication and revamped the TestContainers usage.
1 parent 4caa29c commit df2c6ab

File tree

6 files changed

+99
-35
lines changed

6 files changed

+99
-35
lines changed

web/querydsl/README.md

+2-3
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@ This example shows some of the Spring Data Querydsl integration features with Sp
44

55
## Quickstart
66

7-
1. Install MongoDB (http://www.mongodb.org/downloads, unzip, run `mkdir data`, run `bin/mongod --dbpath=data`)
8-
2. Build and run the app (`mvn spring-boot:run`)
9-
4. Access app directly via its UI (`http://localhost:8080/`).
7+
1. Build and run the app (`mvn spring-boot:test-run`)
8+
2. Access app directly via its UI (`http://localhost:8080/`).
109

1110
## Interesting bits
1211

web/querydsl/pom.xml

+15-3
Original file line numberDiff line numberDiff line change
@@ -58,20 +58,20 @@
5858
<dependency>
5959
<groupId>io.github.jpenren</groupId>
6060
<artifactId>thymeleaf-spring-data-dialect</artifactId>
61-
<version>3.3.0</version>
61+
<version>3.6.0</version>
6262
</dependency>
6363

6464
<dependency>
6565
<groupId>org.webjars</groupId>
6666
<artifactId>jquery</artifactId>
67-
<version>2.1.3</version>
67+
<version>3.7.1</version>
6868
<scope>runtime</scope>
6969
</dependency>
7070

7171
<dependency>
7272
<groupId>org.webjars</groupId>
7373
<artifactId>bootstrap</artifactId>
74-
<version>3.3.4</version>
74+
<version>5.3.3</version>
7575
<scope>runtime</scope>
7676
</dependency>
7777

@@ -94,6 +94,18 @@
9494
<scope>test</scope>
9595
</dependency>
9696

97+
<dependency>
98+
<groupId>org.testcontainers</groupId>
99+
<artifactId>junit-jupiter</artifactId>
100+
<scope>test</scope>
101+
</dependency>
102+
103+
<dependency>
104+
<groupId>org.springframework.boot</groupId>
105+
<artifactId>spring-boot-testcontainers</artifactId>
106+
<scope>test</scope>
107+
</dependency>
108+
97109
</dependencies>
98110

99111
<build>

web/querydsl/src/main/resources/templates/index.html

+10-6
Original file line numberDiff line numberDiff line change
@@ -23,23 +23,27 @@ <h3 class="panel-title">Search</h3>
2323

2424
<div class="form-group col-sm-6">
2525
<label for="firstname" class="control-label">Firstname:</label>
26-
<input id="firstname" name="firstname" th:value="${#httpServletRequest.getParameter('firstname')}" type="text" class="form-control" autofocus="autofocus" />
26+
<input id="firstname" name="firstname" th:value="${param.firstname}" type="text" class="form-control" autofocus="autofocus" />
2727
</div>
2828
<div class="form-group col-sm-6">
2929
<label for="lastname" class="control-label">Lastname:</label>
30-
<input id="lastname" name="lastname" th:value="${#httpServletRequest.getParameter('lastname')}" type="text" class="form-control" autofocus="autofocus" />
30+
<input id="lastname" name="lastname" th:value="${param.lastname}" type="text" class="form-control" autofocus="autofocus" />
3131
</div>
3232
<div class="form-group col-sm-6">
3333
<label for="address.city" class="control-label">City:</label>
34-
<input id="address.city" name="address.city" th:value="${#httpServletRequest.getParameter('address.city')}" type="text" class="form-control" />
34+
<input id="address.city" name="address.city" th:value="${param['address.city']}" type="text"
35+
class="form-control" />
3536
</div>
3637
<div class="form-group col-sm-6">
3738
<label for="address.street" class="control-label">Street:</label>
38-
<input id="address.street" name="address.street" th:value="${#httpServletRequest.getParameter('address.street')}" type="text" class="form-control" />
39+
<input id="address.street" name="address.street" th:value="${param['address.street']}"
40+
type="text"
41+
class="form-control" />
3942
</div>
4043
<div class="form-group col-sm-6">
4144
<label for="nationality" class="control-label">Nationality:</label>
42-
<input id="nationality" name="nationality" th:value="${#httpServletRequest.getParameter('nationality')}" type="text" class="form-control" />
45+
<input id="nationality" name="nationality" th:value="${param.nationality}" type="text"
46+
class="form-control" />
4347
</div>
4448
<div class="form-group col-sm-12">
4549
<button id="submit" type="submit" class="btn btn-default">Submit</button>
@@ -75,7 +79,7 @@ <h3 class="panel-title">Search</h3>
7579
<tr th:each="user : ${users}">
7680
<td th:text="${users.number * users.size + userStat.index + 1} + '.'">1.</td>
7781
<td class="text-center">
78-
<img th:src="${user.picture.small}" class="img-circle" />
82+
<img th:src="${user.picture.small}" class="rounded-circle" />
7983
</td>
8084
<td th:text="${user.firstname}">Firstname</td>
8185
<td th:text="${user.lastname}">Lastname</td>
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017-2021 the original author or authors.
2+
* Copyright 2017-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -15,44 +15,26 @@
1515
*/
1616
package example.users;
1717

18-
1918
import org.junit.jupiter.api.Test;
20-
2119
import org.springframework.boot.test.context.SpringBootTest;
22-
import org.springframework.test.context.DynamicPropertyRegistry;
23-
import org.springframework.test.context.DynamicPropertySource;
20+
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
2421
import org.testcontainers.containers.MongoDBContainer;
2522
import org.testcontainers.junit.jupiter.Container;
2623
import org.testcontainers.junit.jupiter.Testcontainers;
27-
import org.testcontainers.utility.DockerImageName;
2824

2925
/**
3026
* @author Oliver Gierke
3127
* @author Divya Srivastava
28+
* @author Tim Sparg
3229
*/
3330
@Testcontainers
3431
@SpringBootTest
3532
class ApplicationTests {
3633

37-
@Container //
38-
private static MongoDBContainer mongoDBContainer = MongoContainers.getDefaultContainer();
39-
40-
@DynamicPropertySource
41-
static void setProperties(DynamicPropertyRegistry registry) {
42-
registry.add("spring.data.mongodb.uri", mongoDBContainer::getReplicaSetUrl);
43-
}
34+
@Container
35+
@ServiceConnection static MongoDBContainer mongoDBContainer = new MongoDBContainer("mongo:7");
4436

4537
@Test
4638
void contextBootstraps() {}
4739

48-
static class MongoContainers {
49-
50-
private static final String IMAGE_NAME = "mongo:5.0";
51-
private static final String IMAGE_NAME_PROPERTY = "mongo.default.image.name";
52-
53-
public static MongoDBContainer getDefaultContainer() {
54-
return new MongoDBContainer(DockerImageName.parse(System.getProperty(IMAGE_NAME_PROPERTY, IMAGE_NAME)))
55-
.withReuse(true);
56-
}
57-
}
5840
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright 2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package example.users;
17+
18+
import org.springframework.boot.SpringApplication;
19+
20+
/**
21+
* @author Tim Sparg
22+
*/
23+
public class TestApplication {
24+
25+
public static void main(String[] args) {
26+
SpringApplication.from(Application::main).with(TestcontainersConfiguration.class).run(args);
27+
}
28+
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright 2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package example.users;
17+
18+
import org.springframework.boot.devtools.restart.RestartScope;
19+
import org.springframework.boot.test.context.TestConfiguration;
20+
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
21+
import org.springframework.context.annotation.Bean;
22+
import org.testcontainers.containers.MongoDBContainer;
23+
import org.testcontainers.utility.DockerImageName;
24+
25+
/**
26+
* @author Tim Sparg
27+
*/
28+
@TestConfiguration(proxyBeanMethods = false)
29+
public class TestcontainersConfiguration {
30+
31+
@Bean
32+
@ServiceConnection
33+
@RestartScope
34+
MongoDBContainer mongoDbContainer() {
35+
return new MongoDBContainer(DockerImageName.parse("mongo:7"));
36+
}
37+
38+
}

0 commit comments

Comments
 (0)