Class RequestParameters
- java.lang.Object
-
- com.devonfw.module.rest.service.api.RequestParameters
-
public class RequestParameters extends Object
This class helps to deal withUriInfo
andMultivaluedMap
from the JAX-RS API. E.g. if you have a REST query operation for a collection URI you can useUriInfo
in case you want to support a mixture of optional and required parameters. The methods provided here throw according exceptions such asBadRequestException
and already support conversion of values.- Since:
- 2.0.0
-
-
Constructor Summary
Constructors Constructor Description RequestParameters(javax.ws.rs.core.MultivaluedMap<String,String> parameters)
The constructor.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected <T> T
convertValue(String value, Class<T> targetType)
Converts the givenvalue
to the giventargetType
.static RequestParameters
fromPath(javax.ws.rs.core.UriInfo uriInfo)
static RequestParameters
fromQuery(javax.ws.rs.core.UriInfo uriInfo)
String
get(String key)
Gets the parameter as single value with the givenkey
asString
.<T> T
get(String key, Class<T> targetType, boolean required)
Gets the single parameter in a generic and flexible way.String
getFirst(String key)
Gets the parameter with the givenkey
asString
.List<String>
getList(String key)
Gets theList
of all value for the parameter with with the givenkey
.
-
-
-
Method Detail
-
get
public <T> T get(String key, Class<T> targetType, boolean required) throws javax.ws.rs.WebApplicationException
Gets the single parameter in a generic and flexible way.- Type Parameters:
T
- is the generic type oftargetType
.- Parameters:
key
- is thekey
of the parameter to get.targetType
- is theClass
reflecting the type to convert the value to. Supports common Java standard types such asString
,Long
,Double
,BigDecimal
, etc.required
- -true
if the value is required and aBadRequestException
is thrown if it is not present,false
otherwise (if optional).- Returns:
- the value for the given
key
converted to the giventargetType
. May benull
ifrequired
isfalse
. - Throws:
javax.ws.rs.WebApplicationException
- if an error occurred. E.g.BadRequestException
if a required parameter is missing orInternalServerErrorException
if the giventargetType
is not supported.
-
convertValue
protected <T> T convertValue(String value, Class<T> targetType) throws ParseException
Converts the givenvalue
to the giventargetType
.- Type Parameters:
T
- is the generic type oftargetType
.- Parameters:
value
- is the value to convert.targetType
- is theClass
reflecting the type to convert the value to.- Returns:
- the converted value.
- Throws:
ParseException
- if parsing of the givenvalue
failed while converting.
-
get
public String get(String key) throws javax.ws.rs.BadRequestException
Gets the parameter as single value with the givenkey
asString
.- Parameters:
key
- is thekey
of the parameter to get.- Returns:
- the requested parameter. Will be
null
if the parameter is not present. - Throws:
javax.ws.rs.BadRequestException
- if the parameter is defined multiple times (seegetList(String)
).
-
getFirst
public String getFirst(String key)
Gets the parameter with the givenkey
asString
. Unlikeget(String)
this method will not throw an exception if the parameter is multi-valued but just return the first value.- Parameters:
key
- is thekey
of the parameter to get.- Returns:
- the first value of the requested parameter. Will be
null
if the parameter is not present.
-
getList
public List<String> getList(String key)
Gets theList
of all value for the parameter with with the givenkey
. In general you should avoid multi-valued parameters (e.g. http://host/path?query=a&query=b). The JAX-RS API supports this exotic case as first citizen so we expose it here but only use it if you know exactly what you are doing.- Parameters:
key
- is thekey
of the parameter to get.- Returns:
- the
List
with all values of the requested parameter. Will be anempty list
if the parameter is not present.
-
fromQuery
public static RequestParameters fromQuery(javax.ws.rs.core.UriInfo uriInfo)
- Parameters:
uriInfo
- is theUriInfo
.- Returns:
- a new instance of
RequestParameters
forUriInfo.getQueryParameters()
.
-
fromPath
public static RequestParameters fromPath(javax.ws.rs.core.UriInfo uriInfo)
- Parameters:
uriInfo
- is theUriInfo
.- Returns:
- a new instance of
RequestParameters
forUriInfo.getPathParameters()
.
-
-