cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

CAP Java: Handle $count query in Custom Handler

christoffer_fuss
Participant
0 Likes
3,099

Hello community,

we are using CAP Java and want to use the $count query in a custom handler:

Query:
http://localhost:8080/api/play/Books/$count

We are using the CqnAnalyzer to check if this is a $count query or not. Normally we return a list of Books but we don't know, how we can return the response for a $count query.

If we return the List of Books as well we are getting the error:

So what is the correct way to return the result for $count?
In the documentation I just found how to pass the inlinecount by using the ResultBuilder.

Result r = selectedRows(asList(row)).inlineCount(inlineCount).result();


Best regards and many thanks,
Chris

Accepted Solutions (1)

Accepted Solutions (1)

marcbecker
Product and Topic Expert
Product and Topic Expert

The $count query from OData is done by sending a CqnSelect containing only a count(*) function with alias count. Therefore the expected result structure would be something like this:

Arrays.asList(Collections.singletonMap("count", value));

You can adapt your method signature to use return type List<Map<String, Object>>

christoffer_fuss
Participant
0 Likes

Thanks Marc, it works fine 🙂 Maybe you can add this in the documentation as well.

Best regards,

Chris

marcbecker
Product and Topic Expert
Product and Topic Expert
0 Likes

I opened a PR to improve this in our docs: https://github.com/cap-js/docs/pull/495

Answers (0)