简体   繁体   中英

Trying to Parse an OData $filter into its name-value pairs using Java

I am building a service that will receive its "query parameters" using OData syntax: instead of the more familiar

?make=Honda&model=Pilot&trimline=LX

The requirement is to accept a single OData filter "$filter" being passed.

?$filter="'make' eq 'Honda' and 'model' eq 'Pilot' and 'trimline' eq 'LX'"

There is no need for considerations of SQL query mapping, JDBC, conversion to JSON, etc. This is literally trying to get only the set of name-value pairs from this syntax. I'm looking at Olingo libraries and seeing things like FilterExpression and UriParser but this seems to be overcomplicating this. Am I missing something?

The $filter String will only have the 'eq' operator for all cases.

I'm assuming there is a simpler approach to doing this. I was hoping there was something in the Olingo libraries where I could get the $filter as described and end up with a HashMap perhaps.

Thanks

Unfortunately Olingo does not offer this feature. In order to parse a filter string with Olingo you need at least an EdmEntityType. Then you could call the parseFilterMethod like this UriParser.parseFilter(null, entityType, filterString).

If you only expect "eq" parameters why not use the String.split() method to split at the String " eq " to get the segments you need. I included the space before and after the eq to prevent the spilt method from splitting a valid filter like $filter=Name eq 'Equador' into 4 segments instead of three. You could even use the split method with a regular expression which is more sophisticated than my simple example.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM