@RequestMapping(value = "/referral-panel/customers", method = RequestMethod.GET)
public ResponseEntity<?> viewAllCustomers(@RequestParam(name = "userId") Long userId, @RequestParam(name = "pageNo", required = false, defaultValue = "0") Integer pageNo , @RequestParam(name = "pageSize", required = false, defaultValue = "10") Integer pageSize)
{
List<RewardEntity> list = rewardEntityRepository.findAllByReferrerUserId(userId, PageRequest.of(pageNo, pageSize));
//process list
........
.......
}
I have an api defined like this. I'm calling it with the following url : http://localhost:8080/my-service/api/v1/referral-panel/customers/?userId=3001502
where I'm only passing the userId. The other two params are both optional. However, I'm getting this exception :
org.springframework.dao.InvalidDataAccessApiUsageException: At least 2 parameter(s) provided but only 1 parameter(s) present in query.; nested exception is java.lang.IllegalArgumentException: At least 2 parameter(s) provided but only 1 parameter(s) present in query.
at org.springframework.orm.jpa.EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(EntityManagerFactoryUtils.java:374)
at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:257)
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:528)
at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:61)
at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:242)
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:153)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMethodMetadataPostProcessor.java:149)
Why does it say that there are at least 2 params? It looks like Spring recognises one of them as an optional param but not the other.
I have used the default property before and I know you can do it for one param with no issues. Is there an issue with having two optional parameters?
Additional :
My rewardEntity class looks like :
@Data
@Entity
@AllArgsConstructor
@NoArgsConstructor
@Table(name = "referral_reward")
public class RewardEntity {
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name ="referrer_user_id")
private Long referrerUserId;
@Column(name = "referree_user_id")
private Long referreeUserId;
@Column(name ="event")
private String event;
@Column(name ="reward_type")
private String rewardType;
@Column(name = "amount")
private Integer amount;
@Column(name = "status")
private String status = "PENDING";
and my JPA repository looks like :
public interface RewardEntityRepository extends JpaRepository<RewardEntity, Long> {
List<RewardEntity> findAllByReferrerUserId(Long referrerUserId, PageRequest of);
}