2017-05-18 117 views
0

我正在开发使用spring-boot-starter-data-jpa-1.5.2.RELEASE的spring-boot REST服务器。我有以下POJO类层次结构。首先是基类实体:spring-boot/spring-data-jpa通过日期字段自定义查询过滤返回结果没有过滤

@javax.persistence.Entity 
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) 
public abstract class Entity implements Serializable {  

    @Id 
    @Column(name = "id", nullable = false, length = 48) 
    public String id = UNINITIALIZED_ID; 

    /** 
    * The timestamp for when this entity was last updated. 
    */ 
    @Column(columnDefinition = "timestamp with time zone") 
    @Temporal(TemporalType.TIMESTAMP) 
    public Date updateTimestamp = new Date(); 

} 

接下来的具体子类的病人:

@javax.persistence.Entity 
@Table(indexes={@Index(columnList="updateTimestamp")}) 
public class Patient extends Entity { 
... 
} 

我定义我PatientRepository界面的自定义方法如下,以获取患者的updateTimestamp是指定的时间戳之后:

@RepositoryRestResource(collectionResourceRel = "patients", path = "patients") 
public interface PatientRepository extends JpaRepository<Patient, String> { 
    List<Patient> findByUpdateTimestampAfter(@DateTimeFormat(iso=DateTimeFormat.ISO.DATE_TIME)@Param("after")Date after); 
} 

对于一些未知的原因由updateTimestamnp过滤器时,我发出了通过先进的REST客户端,浏览器插件,下面的GET请求不工作:

// url example. DateFormat matches @DateTimeFormat on param in query method. 
GET http://127.0.0.1:8090/patients?after=2030-01-10T00:00:00.000-05:00 

我只期望有updateTimestamp之后要重新指定的时间戳,但我得到的是资源集合中的所有实体,而不是那些实体。

我已经在spring-boot上打开了DEBUG日志记录。下面是我不知道的东西是一个问题:

2017-05-18 10:15:56.127 DEBUG 5292 --- [   main] o.s.d.j.r.query.JpaQueryFactory   : Looking up query for method findByUpdateTimestampAfter 
2017-05-18 10:15:56.132 DEBUG 5292 --- [   main] o.s.d.jpa.repository.query.NamedQuery : Looking up named query Patient.findByUpdateTimestampAfter 
2017-05-18 10:15:56.133 DEBUG 5292 --- [   main] o.s.d.jpa.repository.query.NamedQuery : Did not find named query Patient.findByUpdateTimestampAfter 

基于从@pvpkiran我wroite以下斯波克集成测试和很好的建议表明,aftergetting日期来分析我的PatientRepository.findByUpdateTimestampAfter方法按预期工作:

@SpringBootTest(classes = com.altran.medmap.server.Application.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) 
    @Transactional 

class PatientRepositorySpec extends Specification { 


    @Autowired 
    PatientRepository patientRepository; 


    @Bean 
    @Unroll  
    def 'Should GET #size patients whose updateTimestamp is after: #after'({ 
     given: 
      DateFormat dateFormat = new SimpleDateFormat('MMM d, yyyy h:mm:ss a'); 
      Date afterx = dateFormat.parse(after); 
     when: 
      List<Patient> result = patientRepository.findByUpdateTimestampAfter(after); 
     then: 
      result.size() == size; 
     where: 'result size matches expected' 
      after      | size 
      'Jan 1, 2016 00:00:00 AM' | 2 
      'Jan 1, 2030 00:00:00 AM' | 0 

    }    
} 

REST API仍然返回所有实体的事实仍然是一个未解决的谜团。不是我在Repository接口中的查询方法参数中指定了@DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME),并且在静态URL中使用了相同的格式。有什么建议去哪里?

+0

当你说不工作,这是否意味着没有记录被提取或你是否有任何异常。你有在DB中符合这个标准的记录吗? – pvpkiran

+0

非常抱歉。只是更新了我得到的所有实体,而不是指定时间戳后具有updateTimestamp的子集。 –

+0

删除你的'@ Query'并尝试。 Spring数据足够智能,可以通过方法名称来制定查询。我认为问题在于你用'>'比较的方式。所以把这些复杂性留给框架。只需尝试不用@ Query – pvpkiran

回答

0

我终于想出了导致其余接口不给过滤结果的愚蠢错误。我的网址路径不正确。

//Incorrect URL 
http://127.0.0.1:8090/patients?after=2030-01-10T00:00:00.000-05:00 

//Correct URL based on spring-data-rest default conventions 
http://127.0.0.1:8090/patients/search/findByUpdateTimestampAfter?after=2030-01-10T00:00:00.000-05:00 

请注意,修复URL后,我得到一个日期解析错误。我为这个here创建了一个新问题。

感谢@pvpkiran和@Afridi的帮助。

+0

啊!!!那很好 :) – pvpkiran

相关问题