querydsl web support

This commit is contained in:
DOHA 2015-11-18 13:08:50 +02:00
parent 56701e3fbc
commit becfced8a8
7 changed files with 123 additions and 6 deletions

View File

@ -10,7 +10,7 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.2.6.RELEASE</version>
<version>1.3.0.RELEASE</version>
</parent>
<dependencies>
@ -93,6 +93,13 @@
<artifactId>spring-webmvc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-commons</artifactId>
</dependency>
<!-- deployment -->
<dependency>

View File

@ -1,9 +1,19 @@
package org.baeldung.persistence.dao;
import org.baeldung.persistence.model.MyUser;
import org.baeldung.persistence.model.QMyUser;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.querydsl.QueryDslPredicateExecutor;
import org.springframework.data.querydsl.binding.QuerydslBinderCustomizer;
import org.springframework.data.querydsl.binding.QuerydslBindings;
public interface MyUserRepository extends JpaRepository<MyUser, Long>, QueryDslPredicateExecutor<MyUser> {
import com.mysema.query.types.path.StringPath;
public interface MyUserRepository extends JpaRepository<MyUser, Long>, QueryDslPredicateExecutor<MyUser>, QuerydslBinderCustomizer<QMyUser> {
@Override
default public void customize(final QuerydslBindings bindings, final QMyUser root) {
bindings.bind(String.class).first((final StringPath path, final String value) -> path.containsIgnoreCase(value));
bindings.excluding(root.email);
}
}

View File

@ -25,6 +25,16 @@ public class MyUser {
super();
}
public MyUser(final String firstName, final String lastName, final String email, final int age) {
super();
this.firstName = firstName;
this.lastName = lastName;
this.email = email;
this.age = age;
}
//
public Long getId() {
return id;
}

View File

@ -17,6 +17,7 @@ import org.baeldung.web.util.SearchCriteria;
import org.baeldung.web.util.SearchOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.querydsl.binding.QuerydslPredicate;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
@ -28,11 +29,13 @@ import org.springframework.web.bind.annotation.ResponseStatus;
import com.google.common.base.Joiner;
import com.google.common.base.Preconditions;
import com.mysema.query.types.Predicate;
import com.mysema.query.types.expr.BooleanExpression;
import cz.jirutka.rsql.parser.RSQLParser;
import cz.jirutka.rsql.parser.ast.Node;
//@EnableSpringDataWebSupport
@Controller
public class UserController {
@ -43,7 +46,7 @@ public class UserController {
private UserRepository dao;
@Autowired
private MyUserRepository mydao;
private MyUserRepository myUserRepository;
public UserController() {
super();
@ -92,7 +95,7 @@ public class UserController {
}
}
final BooleanExpression exp = builder.build();
return mydao.findAll(exp);
return myUserRepository.findAll(exp);
}
@RequestMapping(method = RequestMethod.GET, value = "/users/rsql")
@ -103,6 +106,12 @@ public class UserController {
return dao.findAll(spec);
}
@RequestMapping(method = RequestMethod.GET, value = "/api/myusers")
@ResponseBody
public Iterable<MyUser> findAllByWebQuerydsl(@QuerydslPredicate(root = MyUser.class) final Predicate predicate) {
return myUserRepository.findAll(predicate);
}
// API - WRITE
@RequestMapping(method = RequestMethod.POST, value = "/users")
@ -116,7 +125,8 @@ public class UserController {
@ResponseStatus(HttpStatus.CREATED)
public void addMyUser(@RequestBody final MyUser resource) {
Preconditions.checkNotNull(resource);
mydao.save(resource);
myUserRepository.save(resource);
}
}

View File

@ -8,6 +8,7 @@ import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.metrics.CounterService;
import org.springframework.boot.actuate.metrics.Metric;
import org.springframework.boot.actuate.metrics.repository.InMemoryMetricRepository;
import org.springframework.boot.actuate.metrics.repository.MetricRepository;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
@ -15,7 +16,6 @@ import org.springframework.stereotype.Service;
@Service
public class ActuatorMetricService implements IActuatorMetricService {
@Autowired
private MetricRepository repo;
@Autowired
@ -27,6 +27,7 @@ public class ActuatorMetricService implements IActuatorMetricService {
public ActuatorMetricService() {
super();
repo = new InMemoryMetricRepository();
statusMetric = new ArrayList<ArrayList<Integer>>();
statusList = new ArrayList<String>();
}

View File

@ -14,6 +14,8 @@
<http-basic/>
<csrf disabled="true"/>
</http>
<authentication-manager>

View File

@ -0,0 +1,77 @@
package org.baeldung.web;
import static org.junit.Assert.assertEquals;
import org.baeldung.persistence.model.MyUser;
import org.baeldung.spring.Application;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.jayway.restassured.RestAssured;
import com.jayway.restassured.response.Response;
import com.jayway.restassured.specification.RequestSpecification;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)
@WebAppConfiguration
public class MyUserLiveTest {
private ObjectMapper mapper = new ObjectMapper();
private MyUser userJohn = new MyUser("john", "doe", "john@test.com", 11);
private MyUser userTom = new MyUser("tom", "doe", "tom@test.com", 20);
private static boolean setupDataCreated = false;
@Before
public void setupData() throws JsonProcessingException {
if (!setupDataCreated) {
withRequestBody(givenAuth(), userJohn).post("http://localhost:8080/myusers");
withRequestBody(givenAuth(), userTom).post("http://localhost:8080/myusers");
setupDataCreated = true;
}
}
@Test
public void whenGettingListOfUsers_thenCorrect() {
final Response response = givenAuth().get("http://localhost:8080/api/myusers");
final MyUser[] result = response.as(MyUser[].class);
assertEquals(result.length, 2);
}
@Test
public void givenFirstName_whenGettingListOfUsers_thenCorrect() {
final Response response = givenAuth().get("http://localhost:8080/api/myusers?firstName=john");
final MyUser[] result = response.as(MyUser[].class);
assertEquals(result.length, 1);
assertEquals(result[0].getEmail(), userJohn.getEmail());
}
@Test
public void givenPartialLastName_whenGettingListOfUsers_thenCorrect() {
final Response response = givenAuth().get("http://localhost:8080/api/myusers?lastName=do");
final MyUser[] result = response.as(MyUser[].class);
assertEquals(result.length, 2);
}
@Test
public void givenEmail_whenGettingListOfUsers_thenIgnored() {
final Response response = givenAuth().get("http://localhost:8080/api/myusers?email=john");
final MyUser[] result = response.as(MyUser[].class);
assertEquals(result.length, 2);
}
private RequestSpecification givenAuth() {
return RestAssured.given().auth().preemptive().basic("user1", "user1Pass");
}
private RequestSpecification withRequestBody(final RequestSpecification req, final Object obj) throws JsonProcessingException {
return req.contentType(MediaType.APPLICATION_JSON_VALUE).body(mapper.writeValueAsString(obj));
}
}