1. Overview
1.概述
In this quick tutorial, we’ll discuss the Spring Data Querydsl Web Support.
在这个快速教程中,我们将讨论Spring Data Querydsl Web支持。
This is definitely an interesting alternative to all the other ways we focused on in the main REST Query Language series.
与我们在主要的REST查询语言系列中关注的所有其他方式相比,这绝对是一个有趣的选择。
2. The Maven Config
2.Maven的配置
First, let’s start with our maven configuration:
首先,让我们从maven的配置开始。
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.0.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-commons</artifactId>
</dependency>
<dependency>
<groupId>com.mysema.querydsl</groupId>
<artifactId>querydsl-apt</artifactId>
<version>${querydsl.version}</version>
</dependency>
<dependency>
<groupId>com.mysema.querydsl</groupId>
<artifactId>querydsl-jpa</artifactId>
<version>${querydsl.version}</version>
</dependency>
...
Note that Querydsl web support is available in spring-data-commons since 1.11
请注意,自spring-data-commons 1.11以来,Querydsl网络支持已经可用。
3. The User Repository
3.用户资源库
Next, let’s take a look at our repository:
接下来,让我们来看看我们的资源库。
public interface UserRepository extends
JpaRepository<User, Long>, QueryDslPredicateExecutor<User>, QuerydslBinderCustomizer<QUser> {
@Override
default public void customize(QuerydslBindings bindings, QUser root) {
bindings.bind(String.class).first(
(StringPath path, String value) -> path.containsIgnoreCase(value));
bindings.excluding(root.email);
}
}
Note that:
请注意,。
- We’re overriding QuerydslBinderCustomizer customize() to customize the default binding
- We’re customizing the default equals binding to ignore case for all String properties
- We’re also excluding the user’s email from Predicate resolution
Check out the full documentation here.
查看完整的文档这里。
4. The User Controller
4.用户控制器
Now, let’s take a look at the controller:
现在,让我们看一下控制器。
@RequestMapping(method = RequestMethod.GET, value = "/users")
@ResponseBody
public Iterable<User> findAllByWebQuerydsl(
@QuerydslPredicate(root = User.class) Predicate predicate) {
return userRepository.findAll(predicate);
}
This is the interesting part – notice how we’re obtaining a Predicate directly out of the HttpRequest, using the @QuerydslPredicate annotation.
这是有趣的部分–注意我们如何直接从HttpRequest中获得一个谓词,使用@QuerydslPredicate注释。
Here’s how a URL with this type of query would look like:
下面是具有这种类型的查询的URL的样子。
http://localhost:8080/users?firstName=john
And here’s how a potential response would be structure:
而这里是一个潜在的反应将如何结构。
[
{
"id":1,
"firstName":"john",
"lastName":"doe",
"email":"john@test.com",
"age":11
}
]
5. Live Test
5.现场测试
Finally, let’s test out the new Querydsl Web Support:
最后,让我们测试一下新的Querydsl网络支持。
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)
@WebAppConfiguration
public class UserLiveTest {
private ObjectMapper mapper = new ObjectMapper();
private User userJohn = new User("john", "doe", "john@test.com");
private User userTom = new User("tom", "doe", "tom@test.com");
private static boolean setupDataCreated = false;
@Before
public void setupData() throws JsonProcessingException {
if (!setupDataCreated) {
givenAuth().contentType(MediaType.APPLICATION_JSON_VALUE)
.body(mapper.writeValueAsString(userJohn))
.post("http://localhost:8080/users");
givenAuth().contentType(MediaType.APPLICATION_JSON_VALUE)
.body(mapper.writeValueAsString(userTom))
.post("http://localhost:8080/users");
setupDataCreated = true;
}
}
private RequestSpecification givenAuth() {
return RestAssured.given().auth().preemptive().basic("user1", "user1Pass");
}
}
First, let’s get all users in the system:
首先,让我们得到系统中的所有用户。
@Test
public void whenGettingListOfUsers_thenCorrect() {
Response response = givenAuth().get("http://localhost:8080/users");
User[] result = response.as(User[].class);
assertEquals(result.length, 2);
}
Next, let’s find users by first name:
接下来,让我们通过名字来寻找用户。
@Test
public void givenFirstName_whenGettingListOfUsers_thenCorrect() {
Response response = givenAuth().get("http://localhost:8080/users?firstName=john");
User[] result = response.as(User[].class);
assertEquals(result.length, 1);
assertEquals(result[0].getEmail(), userJohn.getEmail());
}
Next, lest find users by partial last name:
接下来,我想通过部分姓氏找到用户。
@Test
public void givenPartialLastName_whenGettingListOfUsers_thenCorrect() {
Response response = givenAuth().get("http://localhost:8080/users?lastName=do");
User[] result = response.as(User[].class);
assertEquals(result.length, 2);
}
Now, let’s try to find users by email:
现在,让我们尝试通过电子邮件来寻找用户。
@Test
public void givenEmail_whenGettingListOfUsers_thenIgnored() {
Response response = givenAuth().get("http://localhost:8080/users?email=john");
User[] result = response.as(User[].class);
assertEquals(result.length, 2);
}
Note: When we try to find user by email – the query was ignored, because we excluded user’s email from Predicate resolution.
注意:当我们试图通过电子邮件查找用户时–查询被忽略了,因为我们把用户的电子邮件排除在Predicate解析之外。
6. Conclusion
6.结论
In this article we had a quick intro to the Spring Data Querydsl Web Support and a cool, simple way to obtain a Predicate directly out of the HTTP request and using that to retrieve data.
在这篇文章中,我们快速介绍了Spring Data Querydsl Web支持,以及直接从HTTP请求中获取Predicate并使用它来检索数据的一种很酷的简单方法。