介绍Querydsl Web Support实现REST Query Language
文本我们讨论sprin data Querydsl web支持功能。
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>
QueryDsl web support 在spring-data-commons 1.11版本之后有效。
User Repository
先定义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);
}
}
解释:
- 我们重载QuerydslBinderCustomizer customize() 方法,自定义缺省binding。
- 默认为相等比较,我们修改为忽略大小写比较。
- 并且从Predicate中排除用户email。
读者可以参见官方完整文档。
User Controller
下面我们看User Controller:
@RequestMapping(method = RequestMethod.GET, value = "/users")
@ResponseBody
public Iterable<User> findAllByWebQuerydsl(
@QuerydslPredicate(root = User.class) Predicate predicate) {
return userRepository.findAll(predicate);
}
最有趣的是——我们直接从HTTPRequest中获取Predicate,并指定@QuerydslPredicate注解。
测试UrL为:
http://localhost:8080/users?firstName=john
返回数据结构:
[
{
"id":1,
"firstName":"john",
"lastName":"doe",
"email":"john@test.com",
"age":11
}
]
实际测试
下面,我们进行完整的QueryDsl Web Support测试:
@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");
}
}
首先,获取所有用户:
@Test
public void whenGettingListOfUsers_thenCorrect() {
Response response = givenAuth().get("http://localhost:8080/users");
User[] result = response.as(User[].class);
assertEquals(result.length, 2);
}
根据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());
}
根据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);
}
根据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);
}
我们尝试通过email查询用户时,查询被忽略了,因为prodicate中排除了email字段。
总结
本文快速介绍Spring Data Querydsl Web支持,很酷且简单的方法可以直接从HTTP请求中获得Predicate并检索数据。