【发布时间】:2017-01-08 04:09:38
【问题描述】:
谁能帮帮我?事实上,我有一个使用 Spring boot/Spring Data-REST 的项目。在我的自定义存储库界面中,我创建了一个新方法,如下所示:
@Query("SELECT h FROM History h WHERE " +
" (:fromDate IS NULL OR h.date >= :fromDate)" +
" AND (:toDate IS NULL OR h.date <= :toDate)" +
" AND (:ids IS NULL OR h.id IN :ids)")
Page<History> findHistoryByCriteria(
@Param("fromDate") @DateTimeFormat(pattern = "MM/dd/yyyy") Date fromDate,
@Param("toDate") @DateTimeFormat(pattern = "MM/dd/yyyy") Date toDate,
@Param("ids") List<Integer> ids);
当我使用 URL http://{hostname}:{port}/{context}/...?fromDate=08/01/2016&toDate=08/31/2016&ids[]=1&ids 调用它时[]=2,我得到错误的结果。在日志中我得到
TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [5] as [INTEGER] - [null]
TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [6] as [INTEGER] - [null]
如果我尝试使用 http://{hostname}:{port}/{context}/...?fromDate=08/01/2016&toDate=08/31/2016&ids=2,它返回了一个有效的结果。
如果我尝试使用 http://{hostname}:{port}/{context}/...?fromDate=08/01/2016&toDate=08/31/2016&ids=2&ids=3强>,我得到一个例外
nested exception is java.lang.IllegalArgumentException: Parameter value element [1] did not match expected type [java.lang.Integer (n/a)]: org.springframework.dao.InvalidDataAccessApiUsageException: Parameter value element [1] did not match expected type [java.lang.Integer (n/a)]; nested exception is java.lang.IllegalArgumentException: Parameter value element [1] did not match expected type [java.lang.Integer (n/a)]
我应该如何调用包含多值参数(数组)的 URL,或者我应该怎么做才能使其将参数视为数组或集合?
提前谢谢你。
【问题讨论】:
-
终于看到怎么解决了。对于遇到相同问题的每个人,使用 Spring 发送数组值的 URL 格式是 ids=oneId,anotherId 而不是 ids[]=oneId&ids[]=anotherId 或 ids=oneId&ids=anotherId
标签: spring spring-mvc spring-boot spring-data-jpa spring-data-rest