public final class PojoRest extends Object
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.
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
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.
Constructor and Description |
---|
PojoRest(Object o)
Create a new instance of a REST interface over the specified object.
|
PojoRest(Object o,
ReaderParser parser)
Create a new instance of a REST interface over the specified object.
|
Modifier and Type | Method and Description |
---|---|
Object |
delete(String url)
Remove an element from a POJO model.
|
Object |
get(String url)
Retrieves the element addressed by the URL.
|
<T> T |
get(String url,
Class<T> type)
Retrieves the element addressed by the URL as the specified object type.
|
<T> T |
get(String url,
Type type,
Type... args)
Retrieves the element addressed by the URL as the specified object type.
|
Boolean |
getBoolean(String url)
Returns the specified entry value converted to a
Boolean . |
Boolean |
getBoolean(String url,
Boolean defVal)
Returns the specified entry value converted to a
Boolean . |
ClassMeta |
getClassMeta(String url)
Returns the class type of the object at the specified URL.
|
Integer |
getInt(String url)
Returns the specified entry value converted to an
Integer . |
Integer |
getInt(String url,
Integer defVal)
Returns the specified entry value converted to an
Integer . |
List<?> |
getList(String url)
Returns the specified entry value converted to a
List . |
List<?> |
getList(String url,
List<?> defVal)
Returns the specified entry value converted to a
List . |
Long |
getLong(String url)
Returns the specified entry value converted to a
Long . |
Long |
getLong(String url,
Long defVal)
Returns the specified entry value converted to a
Long . |
Map<?,?> |
getMap(String url)
Returns the specified entry value converted to a
Map . |
Map<?,?> |
getMap(String url,
Map<?,?> defVal)
Returns the specified entry value converted to a
Map . |
ObjectList |
getObjectList(String url)
Returns the specified entry value converted to a
ObjectList . |
ObjectList |
getObjectList(String url,
ObjectList defVal)
Returns the specified entry value converted to a
ObjectList . |
ObjectMap |
getObjectMap(String url)
Returns the specified entry value converted to a
Map . |
ObjectMap |
getObjectMap(String url,
ObjectMap defVal)
Returns the specified entry value converted to a
ObjectMap . |
Collection<String> |
getPublicMethods(String url)
Returns the list of available methods that can be passed to the
invokeMethod(String, String, String)
for the object addressed by the specified URL. |
Object |
getRootObject()
The root object that was passed into the constructor of this method.
|
String |
getString(String url)
Returns the specified entry value converted to a
String . |
String |
getString(String url,
String defVal)
Returns the specified entry value converted to a
String . |
Object |
getWithDefault(String url,
Object defVal)
Retrieves the element addressed by the URL.
|
<T> T |
getWithDefault(String url,
T def,
Class<T> type)
Same as
get(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 as
get(String,Type,Type[]) but returns a default value if the addressed element is null or non-existent. |
Object |
invokeMethod(String url,
String method,
String args)
Executes the specified method with the specified parameters on the specified object.
|
String |
post(String url,
Object val)
Adds a value to a list element in a POJO model.
|
Object |
put(String url,
Object val)
Sets/replaces the element addressed by the URL.
|
PojoRest |
setRootLocked()
Call this method to prevent the root object from being overwritten on
|
String |
toString() |
public PojoRest(Object o)
Uses BeanContext.DEFAULT
for working with Java beans.
o
- The object to be wrapped.public PojoRest(Object o, ReaderParser parser)
The parser is used as the bean context.
o
- The object to be wrapped.parser
- The parser to use for parsing arguments and converting objects to the correct data type.public PojoRest setRootLocked()
public Object getRootObject()
public Object get(String url)
url
- The URL of the element to retrieve.
public Object getWithDefault(String url, Object defVal)
url
- The URL of the element to retrieve.
defVal
- The default value if the map doesn't contain the specified mapping.public <T> T get(String url, Class<T> type)
Will convert object to the specified type per BeanSession.convertToType(Object, Class)
.
PojoRest r =
T
- The specified object type.url
- The URL of the element to retrieve.
If type
- The specified object type.public <T> T get(String url, Type type, Type... args)
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).
PojoMap r =
The array can be arbitrarily long to indicate arbitrarily complex data structures.
get(String, Class)
method instead if you don't need a parameterized map/collection.
T
- The specified object type.url
- The URL of the element to retrieve.
If type
- The specified object type.args
- The specified object parameter types.public <T> T getWithDefault(String url, T def, Class<T> type)
get(String, Class)
but returns a default value if the addressed element is null or non-existent.T
- The specified object type.url
- The URL of the element to retrieve.
If def
- The default value if addressed item does not exist.type
- The specified object type.public <T> T getWithDefault(String url, T def, Type type, Type... args)
get(String,Type,Type[])
but returns a default value if the addressed element is null or non-existent.T
- The specified object type.url
- The URL of the element to retrieve.
If def
- The default value if addressed item does not exist.type
- The specified object type.args
- The specified object parameter types.public String getString(String url)
String
.
Shortcut for get(String.
.
url
- The key.public String getString(String url, String defVal)
String
.
Shortcut for get(String.
.
url
- The key.defVal
- The default value if the map doesn't contain the specified mapping.public Integer getInt(String url)
Integer
.
Shortcut for get(Integer.
.
url
- The key.InvalidDataConversionException
- If value cannot be converted.public Integer getInt(String url, Integer defVal)
Integer
.
Shortcut for get(Integer.
.
url
- The key.defVal
- The default value if the map doesn't contain the specified mapping.InvalidDataConversionException
- If value cannot be converted.public Long getLong(String url)
Long
.
Shortcut for get(Long.
.
url
- The key.InvalidDataConversionException
- If value cannot be converted.public Long getLong(String url, Long defVal)
Long
.
Shortcut for get(Long.
.
url
- The key.defVal
- The default value if the map doesn't contain the specified mapping.InvalidDataConversionException
- If value cannot be converted.public Boolean getBoolean(String url)
Boolean
.
Shortcut for get(Boolean.
.
url
- The key.InvalidDataConversionException
- If value cannot be converted.public Boolean getBoolean(String url, Boolean defVal)
Boolean
.
Shortcut for get(Boolean.
.
url
- The key.defVal
- The default value if the map doesn't contain the specified mapping.InvalidDataConversionException
- If value cannot be converted.public Map<?,?> getMap(String url)
Map
.
Shortcut for get(Map.
.
url
- The key.InvalidDataConversionException
- If value cannot be converted.public Map<?,?> getMap(String url, Map<?,?> defVal)
Map
.
Shortcut for get(Map.
.
url
- The key.defVal
- The default value if the map doesn't contain the specified mapping.InvalidDataConversionException
- If value cannot be converted.public List<?> getList(String url)
List
.
Shortcut for get(List.
.
url
- The key.InvalidDataConversionException
- If value cannot be converted.public List<?> getList(String url, List<?> defVal)
List
.
Shortcut for get(List.
.
url
- The key.defVal
- The default value if the map doesn't contain the specified mapping.InvalidDataConversionException
- If value cannot be converted.public ObjectMap getObjectMap(String url)
Map
.
Shortcut for get(ObjectMap.
.
url
- The key.InvalidDataConversionException
- If value cannot be converted.public ObjectMap getObjectMap(String url, ObjectMap defVal)
ObjectMap
.
Shortcut for get(ObjectMap.
.
url
- The key.defVal
- The default value if the map doesn't contain the specified mapping.InvalidDataConversionException
- If value cannot be converted.public ObjectList getObjectList(String url)
ObjectList
.
Shortcut for get(ObjectList.
.
url
- The key.InvalidDataConversionException
- If value cannot be converted.public ObjectList getObjectList(String url, ObjectList defVal)
ObjectList
.
Shortcut for get(ObjectList.
.
url
- The key.defVal
- The default value if the map doesn't contain the specified mapping.InvalidDataConversionException
- If value cannot be converted.public Object invokeMethod(String url, String method, String args) throws ExecutableException, ParseException, IOException
url
- The URL of the element to retrieve.method
- The method signature.
Can be any of the following formats:
As a rule, use the simplest format needed to uniquely resolve a method.
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.ExecutableException
- Exception occurred on invoked constructor/method/field.ParseException
- Malformed input encountered.IOException
- Thrown by underlying stream.public Collection<String> getPublicMethods(String url)
invokeMethod(String, String, String)
for the object addressed by the specified URL.url
- The URL.public ClassMeta getClassMeta(String url)
url
- The URL.public Object put(String url, Object val)
This method expands the POJO model as necessary to create the new element.
url
- The URL of the element to create.
If val
- The value being set. Value can be of any type.public String post(String url, Object val)
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.
url
- The URL of the element being added to.
If val
- The value being added.public Object delete(String url)
If the element does not exist, no action is taken.
url
- The URL of the element being deleted.
If Copyright © 2016–2019 The Apache Software Foundation. All rights reserved.