Skip to content

Commit becfced

Browse files
committed
querydsl web support
1 parent 56701e3 commit becfced

File tree

7 files changed

+123
-6
lines changed

7 files changed

+123
-6
lines changed

spring-security-rest-full/pom.xml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<parent>
1111
<groupId>org.springframework.boot</groupId>
1212
<artifactId>spring-boot-starter-parent</artifactId>
13-
<version>1.2.6.RELEASE</version>
13+
<version>1.3.0.RELEASE</version>
1414
</parent>
1515

1616
<dependencies>
@@ -92,6 +92,13 @@
9292
<groupId>org.springframework</groupId>
9393
<artifactId>spring-webmvc</artifactId>
9494
</dependency>
95+
96+
<dependency>
97+
<groupId>org.springframework.data</groupId>
98+
<artifactId>spring-data-commons</artifactId>
99+
</dependency>
100+
101+
95102

96103
<!-- deployment -->
97104

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,19 @@
11
package org.baeldung.persistence.dao;
22

33
import org.baeldung.persistence.model.MyUser;
4+
import org.baeldung.persistence.model.QMyUser;
45
import org.springframework.data.jpa.repository.JpaRepository;
56
import org.springframework.data.querydsl.QueryDslPredicateExecutor;
7+
import org.springframework.data.querydsl.binding.QuerydslBinderCustomizer;
8+
import org.springframework.data.querydsl.binding.QuerydslBindings;
69

7-
public interface MyUserRepository extends JpaRepository<MyUser, Long>, QueryDslPredicateExecutor<MyUser> {
10+
import com.mysema.query.types.path.StringPath;
11+
12+
public interface MyUserRepository extends JpaRepository<MyUser, Long>, QueryDslPredicateExecutor<MyUser>, QuerydslBinderCustomizer<QMyUser> {
13+
@Override
14+
default public void customize(final QuerydslBindings bindings, final QMyUser root) {
15+
bindings.bind(String.class).first((final StringPath path, final String value) -> path.containsIgnoreCase(value));
16+
bindings.excluding(root.email);
17+
}
818

919
}

spring-security-rest-full/src/main/java/org/baeldung/persistence/model/MyUser.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,16 @@ public MyUser() {
2525
super();
2626
}
2727

28+
public MyUser(final String firstName, final String lastName, final String email, final int age) {
29+
super();
30+
this.firstName = firstName;
31+
this.lastName = lastName;
32+
this.email = email;
33+
this.age = age;
34+
}
35+
36+
//
37+
2838
public Long getId() {
2939
return id;
3040
}

spring-security-rest-full/src/main/java/org/baeldung/web/controller/UserController.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import org.baeldung.web.util.SearchOperation;
1818
import org.springframework.beans.factory.annotation.Autowired;
1919
import org.springframework.data.jpa.domain.Specification;
20+
import org.springframework.data.querydsl.binding.QuerydslPredicate;
2021
import org.springframework.http.HttpStatus;
2122
import org.springframework.stereotype.Controller;
2223
import org.springframework.web.bind.annotation.RequestBody;
@@ -28,11 +29,13 @@
2829

2930
import com.google.common.base.Joiner;
3031
import com.google.common.base.Preconditions;
32+
import com.mysema.query.types.Predicate;
3133
import com.mysema.query.types.expr.BooleanExpression;
3234

3335
import cz.jirutka.rsql.parser.RSQLParser;
3436
import cz.jirutka.rsql.parser.ast.Node;
3537

38+
//@EnableSpringDataWebSupport
3639
@Controller
3740
public class UserController {
3841

@@ -43,7 +46,7 @@ public class UserController {
4346
private UserRepository dao;
4447

4548
@Autowired
46-
private MyUserRepository mydao;
49+
private MyUserRepository myUserRepository;
4750

4851
public UserController() {
4952
super();
@@ -92,7 +95,7 @@ public Iterable<MyUser> findAllByQuerydsl(@RequestParam(value = "search") final
9295
}
9396
}
9497
final BooleanExpression exp = builder.build();
95-
return mydao.findAll(exp);
98+
return myUserRepository.findAll(exp);
9699
}
97100

98101
@RequestMapping(method = RequestMethod.GET, value = "/users/rsql")
@@ -103,6 +106,12 @@ public List<User> findAllByRsql(@RequestParam(value = "search") final String sea
103106
return dao.findAll(spec);
104107
}
105108

109+
@RequestMapping(method = RequestMethod.GET, value = "/api/myusers")
110+
@ResponseBody
111+
public Iterable<MyUser> findAllByWebQuerydsl(@QuerydslPredicate(root = MyUser.class) final Predicate predicate) {
112+
return myUserRepository.findAll(predicate);
113+
}
114+
106115
// API - WRITE
107116

108117
@RequestMapping(method = RequestMethod.POST, value = "/users")
@@ -116,7 +125,8 @@ public void create(@RequestBody final User resource) {
116125
@ResponseStatus(HttpStatus.CREATED)
117126
public void addMyUser(@RequestBody final MyUser resource) {
118127
Preconditions.checkNotNull(resource);
119-
mydao.save(resource);
128+
myUserRepository.save(resource);
129+
120130
}
121131

122132
}

spring-security-rest-full/src/main/java/org/baeldung/web/metric/ActuatorMetricService.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@
88
import org.springframework.beans.factory.annotation.Autowired;
99
import org.springframework.boot.actuate.metrics.CounterService;
1010
import org.springframework.boot.actuate.metrics.Metric;
11+
import org.springframework.boot.actuate.metrics.repository.InMemoryMetricRepository;
1112
import org.springframework.boot.actuate.metrics.repository.MetricRepository;
1213
import org.springframework.scheduling.annotation.Scheduled;
1314
import org.springframework.stereotype.Service;
1415

1516
@Service
1617
public class ActuatorMetricService implements IActuatorMetricService {
1718

18-
@Autowired
1919
private MetricRepository repo;
2020

2121
@Autowired
@@ -27,6 +27,7 @@ public class ActuatorMetricService implements IActuatorMetricService {
2727

2828
public ActuatorMetricService() {
2929
super();
30+
repo = new InMemoryMetricRepository();
3031
statusMetric = new ArrayList<ArrayList<Integer>>();
3132
statusList = new ArrayList<String>();
3233
}

spring-security-rest-full/src/main/resources/webSecurityConfig.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
<http-basic/>
1616

17+
<csrf disabled="true"/>
18+
1719
</http>
1820

1921
<authentication-manager>
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package org.baeldung.web;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import org.baeldung.persistence.model.MyUser;
6+
import org.baeldung.spring.Application;
7+
import org.junit.Before;
8+
import org.junit.Test;
9+
import org.junit.runner.RunWith;
10+
import org.springframework.boot.test.SpringApplicationConfiguration;
11+
import org.springframework.http.MediaType;
12+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
13+
import org.springframework.test.context.web.WebAppConfiguration;
14+
15+
import com.fasterxml.jackson.core.JsonProcessingException;
16+
import com.fasterxml.jackson.databind.ObjectMapper;
17+
import com.jayway.restassured.RestAssured;
18+
import com.jayway.restassured.response.Response;
19+
import com.jayway.restassured.specification.RequestSpecification;
20+
21+
@RunWith(SpringJUnit4ClassRunner.class)
22+
@SpringApplicationConfiguration(classes = Application.class)
23+
@WebAppConfiguration
24+
public class MyUserLiveTest {
25+
26+
private ObjectMapper mapper = new ObjectMapper();
27+
private MyUser userJohn = new MyUser("john", "doe", "[email protected]", 11);
28+
private MyUser userTom = new MyUser("tom", "doe", "[email protected]", 20);
29+
30+
private static boolean setupDataCreated = false;
31+
32+
@Before
33+
public void setupData() throws JsonProcessingException {
34+
if (!setupDataCreated) {
35+
withRequestBody(givenAuth(), userJohn).post("http://localhost:8080/myusers");
36+
withRequestBody(givenAuth(), userTom).post("http://localhost:8080/myusers");
37+
setupDataCreated = true;
38+
}
39+
}
40+
41+
@Test
42+
public void whenGettingListOfUsers_thenCorrect() {
43+
final Response response = givenAuth().get("http://localhost:8080/api/myusers");
44+
final MyUser[] result = response.as(MyUser[].class);
45+
assertEquals(result.length, 2);
46+
}
47+
48+
@Test
49+
public void givenFirstName_whenGettingListOfUsers_thenCorrect() {
50+
final Response response = givenAuth().get("http://localhost:8080/api/myusers?firstName=john");
51+
final MyUser[] result = response.as(MyUser[].class);
52+
assertEquals(result.length, 1);
53+
assertEquals(result[0].getEmail(), userJohn.getEmail());
54+
}
55+
56+
@Test
57+
public void givenPartialLastName_whenGettingListOfUsers_thenCorrect() {
58+
final Response response = givenAuth().get("http://localhost:8080/api/myusers?lastName=do");
59+
final MyUser[] result = response.as(MyUser[].class);
60+
assertEquals(result.length, 2);
61+
}
62+
63+
@Test
64+
public void givenEmail_whenGettingListOfUsers_thenIgnored() {
65+
final Response response = givenAuth().get("http://localhost:8080/api/myusers?email=john");
66+
final MyUser[] result = response.as(MyUser[].class);
67+
assertEquals(result.length, 2);
68+
}
69+
70+
private RequestSpecification givenAuth() {
71+
return RestAssured.given().auth().preemptive().basic("user1", "user1Pass");
72+
}
73+
74+
private RequestSpecification withRequestBody(final RequestSpecification req, final Object obj) throws JsonProcessingException {
75+
return req.contentType(MediaType.APPLICATION_JSON_VALUE).body(mapper.writeValueAsString(obj));
76+
}
77+
}

0 commit comments

Comments
 (0)