Class ObjectRest
Provides the ability to perform standard REST operations (GET, PUT, POST, DELETE) against nodes in a POJO model. 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:
-
Maps
and Java beans representing JSON objects. -
Collections
and 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:
See Also:
-
Constructor Summary
ConstructorDescriptionObjectRest
(Object o) Create a new instance of a REST interface over the specified object.ObjectRest
(Object o, ReaderParser parser) Create a new instance of a REST interface over the specified object. -
Method Summary
Modifier and TypeMethodDescriptionstatic ObjectRest
Static creator.static ObjectRest
create
(Object o, ReaderParser parser) Static creator.Remove an element from a POJO model.Retrieves the element addressed by the URL.<T> T
Retrieves the element addressed by the URL as the specified object type.<T> T
Retrieves 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
.getPublicMethods
(String url) Returns the list of available methods that can be passed to theinvokeMethod(String, String, String)
for the object addressed by the specified URL.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> T
getWithDefault
(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> T
getWithDefault
(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.invokeMethod
(String url, String method, String args) Executes the specified method with the specified parameters on the specified object.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
-
ObjectRest
Create a new instance of a REST interface over the specified object.Uses
BeanContext.DEFAULT
for working with Java beans.- Parameters:
o
- The object to be wrapped.
-
ObjectRest
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
ObjectRest
object.
-
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
ObjectRest
object.
-
setRootLocked
Call this method to prevent the root object from being overwritten onput("", xxx); calls.- Returns:
- This object.
-
getRootObject
The root object that was passed into the constructor of this method.- Returns:
- The root object.
-
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.
-
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.
-
get
Retrieves the element addressed by the URL as the specified object type.Will convert object to the specified type per
BeanSession.convertToType(Object, Class)
.Examples:
ObjectRest
objectRest =new ObjectRest(object );// Value converted to a string. Stringstring =objectRest .get("path/to/string" , String.class );// Value converted to a bean. MyBeanbean =objectRest .get("path/to/bean" , MyBean.class );// Value converted to a bean array. MyBean[]beanArray =objectRest .get("path/to/beanarray" , MyBean[].class );// Value converted to a linked-list of objects. Listlist =objectRest .get("path/to/list" , LinkedList.class );// Value converted to a map of object keys/values. Mapmap =objectRest .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
BeanSession.convertToType(Object, Class)
.The type can be a simple type (e.g. beans, strings, numbers) or parameterized type (collections/maps).
Examples:
ObjectRest
objectRest =new ObjectRest(object );// Value converted to a linked-list of strings. List<String>list1 =objectRest .get("path/to/list1" , LinkedList.class , String.class );// Value converted to a linked-list of beans. List<MyBean>list2 =objectRest .get("path/to/list2" , LinkedList.class , MyBean.class );// Value converted to a linked-list of linked-lists of strings. List<List<String>>list3 =objectRest .get("path/to/list3" , LinkedList.class , LinkedList.class , String.class );// Value converted to a map of string keys/values. Map<String,String>map1 =objectRest .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 =objectRest .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
-
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.
-
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.
-
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.
-
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.
-
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.
-
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.
-
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.
-
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.
-
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.
-
invokeMethod
public Object invokeMethod(String url, String method, String args) throws ExecutableException, ParseException, IOException Executes the specified method with the specified parameters on the specified object.- Parameters:
url
- The URL of the element to retrieve.method
- The method signature.Can be any of the following formats:
-
Method name only. e.g.
"myMethod" . -
Method name with class names. e.g.
"myMethod(String,int)" . -
Method name with fully-qualified class names. e.g.
"myMethod(java.util.String,int)" .
As a rule, use the simplest format needed to uniquely resolve a method.
-
Method name only. e.g.
args
- The arguments to pass as parameters to the method. These will automatically be converted to the appropriate object type if possible. This must be an array, like a JSON array.- Returns:
- The returned object from the method call.
- Throws:
ExecutableException
- Exception occurred on invoked constructor/method/field.ParseException
- Malformed input encountered.IOException
- Thrown by underlying stream.
-
getPublicMethods
Returns the list of available methods that can be passed to theinvokeMethod(String, String, String)
for the object addressed by the specified URL.- Parameters:
url
- The URL.- Returns:
- The list of methods.
-
getClassMeta
Returns the class type of the object at the specified URL.- Parameters:
url
- The URL.- Returns:
- The class type.
-
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 the element did not previously exist.
-
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.
-
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.
-
toString
-