0
   @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);
}
njari
  • 198
  • 1
  • 12
  • @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize | Add required false to this as well – aleshka-batman Aug 23 '21 at 12:49
  • Hey, thanks! I added that property there. However, doesn't change the response I'm receiving. – njari Aug 23 '21 at 12:52
  • 1
    The exception is on the repository ...`org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMethodMetadataPostProcessor.java:149) ` , could you also post your JpaRepository code and the code you have bypassed with `......` in your `viewAllCustomers`? – pleft Aug 23 '21 at 12:53
  • Does this answer your question? [@RequestParam in Spring MVC handling optional parameters](https://stackoverflow.com/questions/22373696/requestparam-in-spring-mvc-handling-optional-parameters) – Hakob Hakobyan Aug 23 '21 at 12:55
  • 1
    @pleft I just added some more code along with my entity class and the repository. – njari Aug 23 '21 at 13:04
  • @HakobHakobyan not really, since I am actually using `default` as suggested in that post. – njari Aug 23 '21 at 13:05
  • 4
    Try replacing `PageRequest` with `Pageable` in your repository. – slauth Aug 23 '21 at 13:36
  • the url ```api/v1/referral-panel/customers/?userId=3001502``` should be ```api/v1/referral-panel/customers?userId=3001502``` – Padua Aug 23 '21 at 19:33
  • as @pleft rightly noticed, it was actually an error on the repository and as suggested by slauth, replacing PageRequest with Pageable solved it . Thanks! – njari Aug 24 '21 at 07:33

0 Answers0