Class PathTraversal
Internal API: This class is the relocated traversal engine behind the Marshalled* path-addressing
methods and the Swagger/OpenApi $ref resolver. It is public only for cross-module access and is not a
supported public API — do not use it directly.
Nodes in the POJO model are addressed using URLs.
A POJO model is defined as a tree model where nodes consist of consisting of the following:
-
Mapsand Java beans representing JSON objects. -
Collectionsand arrays representing JSON arrays. - Java beans.
Leaves of the tree can be any type of object.
Use get() to retrieve an element from a JSON tree.
Use put() to create (or overwrite) an element in a JSON tree.
Use post() to add an element to a list in a JSON tree.
Use delete() to remove an element from a JSON tree.
Leading slashes in URLs are ignored.
So
Example:
In the special case of collections/arrays of maps/beans, a special XPath-like selector notation can be used in lieu
of index numbers on GET requests to return a map/bean with a specified attribute value.
The syntax is @attr=val, where attr is the attribute name on the child map, and val is the matching value.
Example:
-
Constructor Summary
ConstructorsConstructorDescriptionCreate a new instance of a REST interface over the specified object.PathTraversal(Object o, ReaderParser parser) Create a new instance of a REST interface over the specified object. -
Method Summary
Modifier and TypeMethodDescriptionstatic PathTraversalStatic creator.static PathTraversalcreate(Object o, ReaderParser parser) Static creator.Remove an element from a POJO model.Retrieves the element addressed by the URL.<T> TRetrieves the element addressed by the URL as the specified object type.<T> TRetrieves the element addressed by the URL as the specified object type.getBoolean(String url) Returns the specified entry value converted to aBoolean.getBoolean(String url, Boolean defVal) Returns the specified entry value converted to aBoolean.getClassMeta(String url) Returns the class type of the object at the specified URL.Returns the specified entry value converted to anInteger.Returns the specified entry value converted to anInteger.getJsonList(String url) Returns the specified entry value converted to aJsonList.getJsonList(String url, JsonList defVal) Returns the specified entry value converted to aJsonList.getJsonMap(String url) Returns the specified entry value converted to aMap.getJsonMap(String url, JsonMap defVal) Returns the specified entry value converted to aJsonMap.List<?>Returns the specified entry value converted to aList.List<?>Returns the specified entry value converted to aList.Returns the specified entry value converted to aLong.Returns the specified entry value converted to aLong.Map<?,?> Returns the specified entry value converted to aMap.Map<?,?> Returns the specified entry value converted to aMap.The root object that was passed into the constructor of this method.Returns the specified entry value converted to aString.Returns the specified entry value converted to aString.getWithDefault(String url, Object defVal) Retrieves the element addressed by the URL.<T> TgetWithDefault(String url, T def, Class<T> type) Same asget(String, Class)but returns a default value if the addressed element is null or non-existent.<T> TgetWithDefault(String url, T def, Type type, Type... args) Same asget(String,Type,Type[])but returns a default value if the addressed element is null or non-existent.Adds a value to a list element in a POJO model.Sets/replaces the element addressed by the URL.Call this method to prevent the root object from being overwritten onput("", xxx); calls.toString()
-
Constructor Details
-
PathTraversal
Create a new instance of a REST interface over the specified object.Uses
MarshallingContext.DEFAULTfor working with Java beans.- Parameters:
o- The object to be wrapped.
-
PathTraversal
Create a new instance of a REST interface over the specified object.The parser is used as the bean context.
- Parameters:
o- The object to be wrapped.parser- The parser to use for parsing arguments and converting objects to the correct data type.
-
-
Method Details
-
create
Static creator.- Parameters:
o- The object being wrapped.- Returns:
- A new
PathTraversalobject.
-
create
Static creator.- Parameters:
o- The object being wrapped.parser- The parser to use for parsing arguments and converting objects to the correct data type.- Returns:
- A new
PathTraversalobject.
-
delete
Remove an element from a POJO model.If the element does not exist, no action is taken.
- Parameters:
url- The URL of the element being deleted. Ifnull or blank, the root itself is deleted.- Returns:
- The removed element, or null if that element does not exist.
-
get
Retrieves the element addressed by the URL.- Parameters:
url- The URL of the element to retrieve.
Ifnull or blank, returns the root.- Returns:
- The addressed element, or
null if that element does not exist in the tree.
-
get
Retrieves the element addressed by the URL as the specified object type.Will convert object to the specified type per
MarshallingSession.convertToType(Object, Class).Examples:
PathTraversal
pathTraversal =new PathTraversal(object );// Value converted to a string. Stringstring =pathTraversal .get("path/to/string" , String.class );// Value converted to a bean. MyBeanbean =pathTraversal .get("path/to/bean" , MyBean.class );// Value converted to a bean array. MyBean[]beanArray =pathTraversal .get("path/to/beanarray" , MyBean[].class );// Value converted to a linked-list of objects. Listlist =pathTraversal .get("path/to/list" , LinkedList.class );// Value converted to a map of object keys/values. Mapmap =pathTraversal .get("path/to/map" , TreeMap.class );- Type Parameters:
T- The specified object type.- Parameters:
url- The URL of the element to retrieve. Ifnull or blank, returns the root.type- The specified object type.- Returns:
- The addressed element, or null if that element does not exist in the tree.
-
get
Retrieves the element addressed by the URL as the specified object type.Will convert object to the specified type per
MarshallingSession.convertToType(Object, Class).The type can be a simple type (e.g. beans, strings, numbers) or parameterized type (collections/maps).
Examples:
PathTraversal
pathTraversal =new PathTraversal(object );// Value converted to a linked-list of strings. List<String>list1 =pathTraversal .get("path/to/list1" , LinkedList.class , String.class );// Value converted to a linked-list of beans. List<MyBean>list2 =pathTraversal .get("path/to/list2" , LinkedList.class , MyBean.class );// Value converted to a linked-list of linked-lists of strings. List<List<String>>list3 =pathTraversal .get("path/to/list3" , LinkedList.class , LinkedList.class , String.class );// Value converted to a map of string keys/values. Map<String,String>map1 =pathTraversal .get("path/to/map1" , TreeMap.class , String.class , String.class );// Value converted to a map containing string keys and values of lists containing beans. Map<String,List<MyBean>>map2 =pathTraversal .get("path/to/map2" , TreeMap.class , String.class , List.class , MyBean.class );Collection classes are assumed to be followed by zero or one objects indicating the element type.Map classes are assumed to be followed by zero or two meta objects indicating the key and value types.The array can be arbitrarily long to indicate arbitrarily complex data structures.
Notes:
-
Use the
get(String, Class)method instead if you don't need a parameterized map/collection.
- Type Parameters:
T- The specified object type.- Parameters:
url- The URL of the element to retrieve. Ifnull or blank, returns the root.type- The specified object type.args- The specified object parameter types.- Returns:
- The addressed element, or null if that element does not exist in the tree.
-
Use the
-
getBoolean
Returns the specified entry value converted to aBoolean.Shortcut for
get(Boolean..class , key)- Parameters:
url- The key.- Returns:
- The converted value, or
null if the map contains no mapping for this key. - Throws:
InvalidDataConversionException- If value cannot be converted.
-
getBoolean
Returns the specified entry value converted to aBoolean.Shortcut for
get(Boolean..class , key, defVal)- Parameters:
url- The key.defVal- The default value if the map doesn't contain the specified mapping.- Returns:
- The converted value, or the default value if the map contains no mapping for this key.
- Throws:
InvalidDataConversionException- If value cannot be converted.
-
getClassMeta
Returns the class type of the object at the specified URL.- Parameters:
url- The URL.- Returns:
- The class type, or
null if the addressed element does not exist in the tree.
-
getInt
Returns the specified entry value converted to anInteger.Shortcut for
get(Integer..class , key)- Parameters:
url- The key.- Returns:
- The converted value, or
null if the map contains no mapping for this key. - Throws:
InvalidDataConversionException- If value cannot be converted.
-
getInt
Returns the specified entry value converted to anInteger.Shortcut for
get(Integer..class , key, defVal)- Parameters:
url- The key.defVal- The default value if the map doesn't contain the specified mapping.- Returns:
- The converted value, or the default value if the map contains no mapping for this key.
- Throws:
InvalidDataConversionException- If value cannot be converted.
-
getJsonList
Returns the specified entry value converted to aJsonList.Shortcut for
get(JsonList..class , key)- Parameters:
url- The key.- Returns:
- The converted value, or
null if the map contains no mapping for this key. - Throws:
InvalidDataConversionException- If value cannot be converted.
-
getJsonList
Returns the specified entry value converted to aJsonList.Shortcut for
get(JsonList..class , key, defVal)- Parameters:
url- The key.defVal- The default value if the map doesn't contain the specified mapping.- Returns:
- The converted value, or the default value if the map contains no mapping for this key.
- Throws:
InvalidDataConversionException- If value cannot be converted.
-
getJsonMap
Returns the specified entry value converted to aMap.Shortcut for
get(JsonMap..class , key)- Parameters:
url- The key.- Returns:
- The converted value, or
null if the map contains no mapping for this key. - Throws:
InvalidDataConversionException- If value cannot be converted.
-
getJsonMap
Returns the specified entry value converted to aJsonMap.Shortcut for
get(JsonMap..class , key, defVal)- Parameters:
url- The key.defVal- The default value if the map doesn't contain the specified mapping.- Returns:
- The converted value, or the default value if the map contains no mapping for this key.
- Throws:
InvalidDataConversionException- If value cannot be converted.
-
getList
Returns the specified entry value converted to aList.Shortcut for
get(List..class , key)- Parameters:
url- The key.- Returns:
- The converted value, or
null if the map contains no mapping for this key. - Throws:
InvalidDataConversionException- If value cannot be converted.
-
getList
Returns the specified entry value converted to aList.Shortcut for
get(List..class , key, defVal)- Parameters:
url- The key.defVal- The default value if the map doesn't contain the specified mapping.- Returns:
- The converted value, or the default value if the map contains no mapping for this key.
- Throws:
InvalidDataConversionException- If value cannot be converted.
-
getLong
Returns the specified entry value converted to aLong.Shortcut for
get(Long..class , key)- Parameters:
url- The key.- Returns:
- The converted value, or
null if the map contains no mapping for this key. - Throws:
InvalidDataConversionException- If value cannot be converted.
-
getLong
Returns the specified entry value converted to aLong.Shortcut for
get(Long..class , key, defVal)- Parameters:
url- The key.defVal- The default value if the map doesn't contain the specified mapping.- Returns:
- The converted value, or the default value if the map contains no mapping for this key.
- Throws:
InvalidDataConversionException- If value cannot be converted.
-
getMap
Returns the specified entry value converted to aMap.Shortcut for
get(Map..class , key)- Parameters:
url- The key.- Returns:
- The converted value, or
null if the map contains no mapping for this key. - Throws:
InvalidDataConversionException- If value cannot be converted.
-
getMap
Returns the specified entry value converted to aMap.Shortcut for
get(Map..class , key, defVal)- Parameters:
url- The key.defVal- The default value if the map doesn't contain the specified mapping.- Returns:
- The converted value, or the default value if the map contains no mapping for this key.
- Throws:
InvalidDataConversionException- If value cannot be converted.
-
getRootObject
The root object that was passed into the constructor of this method.- Returns:
- The root object.
-
getString
Returns the specified entry value converted to aString.Shortcut for
get(String..class , key)- Parameters:
url- The key.- Returns:
- The converted value, or
null if the map contains no mapping for this key.
-
getString
Returns the specified entry value converted to aString.Shortcut for
get(String..class , key, defVal)- Parameters:
url- The key.defVal- The default value if the map doesn't contain the specified mapping.- Returns:
- The converted value, or the default value if the map contains no mapping for this key.
-
getWithDefault
Retrieves the element addressed by the URL.- Parameters:
url- The URL of the element to retrieve.
Ifnull or blank, returns the root.defVal- The default value if the map doesn't contain the specified mapping.- Returns:
- The addressed element, or null if that element does not exist in the tree.
-
getWithDefault
Same asget(String, Class)but returns a default value if the addressed element is null or non-existent.- Type Parameters:
T- The specified object type.- Parameters:
url- The URL of the element to retrieve. Ifnull or blank, returns the root.def- The default value if addressed item does not exist.type- The specified object type.- Returns:
- The addressed element, or null if that element does not exist in the tree.
-
getWithDefault
Same asget(String,Type,Type[])but returns a default value if the addressed element is null or non-existent.- Type Parameters:
T- The specified object type.- Parameters:
url- The URL of the element to retrieve. Ifnull or blank, returns the root.def- The default value if addressed item does not exist.type- The specified object type.args- The specified object parameter types.- Returns:
- The addressed element, or null if that element does not exist in the tree.
-
post
Adds a value to a list element in a POJO model.The URL is the address of the list being added to.
If the list does not already exist, it will be created.
This method expands the POJO model as necessary to create the new element.
Notes:
- Parameters:
url- The URL of the element being added to. Ifnull or blank, the root itself (assuming it's one of the types specified above) is added to.val- The value being added.- Returns:
- The URL of the element that was added, or
null if the value was added to a non-indexable (non-List) collection.
-
put
Sets/replaces the element addressed by the URL.This method expands the POJO model as necessary to create the new element.
- Parameters:
url- The URL of the element to create. Ifnull or blank, the root itself is replaced with the specified value.val- The value being set. Value can be of any type.- Returns:
- The previously addressed element, or
null if the element did not previously exist.
-
setRootLocked
Call this method to prevent the root object from being overwritten onput("", xxx); calls.- Returns:
- This object.
-
toString
-