Added timestamp to Person for testing Date conversion.
This commit is contained in:
@@ -29,7 +29,7 @@ class QueryMethodsSpec extends BaseSpec {
|
||||
|
||||
then:
|
||||
response.statusCode == HttpStatus.OK
|
||||
body.links.size() == 4
|
||||
body.links.size() == 5
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package org.springframework.data.rest.test.webmvc;
|
||||
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import javax.persistence.CascadeType;
|
||||
@@ -8,6 +10,7 @@ import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.MapKey;
|
||||
import javax.persistence.OneToMany;
|
||||
import javax.persistence.PrePersist;
|
||||
import javax.persistence.Version;
|
||||
|
||||
import org.codehaus.jackson.annotate.JsonManagedReference;
|
||||
@@ -28,6 +31,7 @@ public class Person {
|
||||
@OneToMany(cascade = CascadeType.REMOVE)
|
||||
@MapKey(name = "type")
|
||||
private Map<String, Profile> profiles;
|
||||
private Date created;
|
||||
|
||||
public Person() {
|
||||
}
|
||||
@@ -90,4 +94,13 @@ public class Person {
|
||||
this.profiles = profiles;
|
||||
}
|
||||
|
||||
public Date getCreated() {
|
||||
return created;
|
||||
}
|
||||
|
||||
@PrePersist
|
||||
private void setCreated() {
|
||||
this.created = Calendar.getInstance().getTime();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package org.springframework.data.rest.test.webmvc;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.data.domain.Page;
|
||||
@@ -7,6 +8,7 @@ import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import org.springframework.data.repository.PagingAndSortingRepository;
|
||||
import org.springframework.data.repository.query.Param;
|
||||
import org.springframework.data.rest.repository.annotation.ConvertWith;
|
||||
import org.springframework.data.rest.repository.annotation.RestResource;
|
||||
|
||||
/**
|
||||
@@ -35,4 +37,8 @@ public interface PersonRepository extends PagingAndSortingRepository<Person, Lon
|
||||
@Query("select p from Person p where p.id in(:ids)")
|
||||
@RestResource(path = "id") Page<Person> findById(@Param("ids") List<Long> ids, Pageable pageable);
|
||||
|
||||
@RestResource(path = "created") List<Person> findByCreatedGreaterThan(
|
||||
@Param("startDate") @ConvertWith(StringToISODateConverter.class) Date startDate
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
@@ -10,8 +10,7 @@ import org.springframework.validation.Validator;
|
||||
/**
|
||||
* @author Jon Brisbin <jbrisbin@vmware.com>
|
||||
*/
|
||||
public class PersonValidator
|
||||
implements Validator {
|
||||
public class PersonValidator implements Validator {
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(PersonValidator.class);
|
||||
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
package org.springframework.data.rest.test.webmvc;
|
||||
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
|
||||
import org.springframework.core.convert.converter.Converter;
|
||||
|
||||
/**
|
||||
* @author Jon Brisbin
|
||||
*/
|
||||
public class StringToISODateConverter implements Converter<String[], Date> {
|
||||
@Override public Date convert(String[] s) {
|
||||
if(s.length == 1) {
|
||||
try {
|
||||
return new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS").parse(s[0]);
|
||||
} catch(ParseException e) {
|
||||
throw new IllegalArgumentException(e);
|
||||
}
|
||||
}
|
||||
|
||||
throw new IllegalArgumentException("Can only parse a single date in the parameter.");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user