Class RequestParameters
- java.lang.Object
-
- com.devonfw.module.rest.service.api.RequestParameters
-
public class RequestParameters extends Object
This class helps to deal withUriInfoandMultivaluedMapfrom the JAX-RS API. E.g. if you have a REST query operation for a collection URI you can useUriInfoin case you want to support a mixture of optional and required parameters. The methods provided here throw according exceptions such asBadRequestExceptionand already support conversion of values.- Since:
- 2.0.0
-
-
Field Summary
Fields Modifier and Type Field Description private javax.ws.rs.core.MultivaluedMap<String,String>parameters
-
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> TconvertValue(String value, Class<T> targetType)Converts the givenvalueto the giventargetType.static RequestParametersfromPath(javax.ws.rs.core.UriInfo uriInfo)static RequestParametersfromQuery(javax.ws.rs.core.UriInfo uriInfo)Stringget(String key)Gets the parameter as single value with the givenkeyasString.<T> Tget(String key, Class<T> targetType, boolean required)Gets the single parameter in a generic and flexible way.StringgetFirst(String key)Gets the parameter with the givenkeyasString.List<String>getList(String key)Gets theListof 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 thekeyof the parameter to get.targetType- is theClassreflecting the type to convert the value to. Supports common Java standard types such asString,Long,Double,BigDecimal, etc.required- -trueif the value is required and aBadRequestExceptionis thrown if it is not present,falseotherwise (if optional).- Returns:
- the value for the given
keyconverted to the giventargetType. May benullifrequiredisfalse. - Throws:
javax.ws.rs.WebApplicationException- if an error occurred. E.g.BadRequestExceptionif a required parameter is missing orInternalServerErrorExceptionif the giventargetTypeis not supported.
-
convertValue
protected <T> T convertValue(String value, Class<T> targetType) throws ParseException
Converts the givenvalueto the giventargetType.- Type Parameters:
T- is the generic type oftargetType.- Parameters:
value- is the value to convert.targetType- is theClassreflecting the type to convert the value to.- Returns:
- the converted value.
- Throws:
ParseException- if parsing of the givenvaluefailed while converting.
-
get
public String get(String key) throws javax.ws.rs.BadRequestException
Gets the parameter as single value with the givenkeyasString.- Parameters:
key- is thekeyof the parameter to get.- Returns:
- the requested parameter. Will be
nullif 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 givenkeyasString. Unlikeget(String)this method will not throw an exception if the parameter is multi-valued but just return the first value.- Parameters:
key- is thekeyof the parameter to get.- Returns:
- the first value of the requested parameter. Will be
nullif the parameter is not present.
-
getList
public List<String> getList(String key)
Gets theListof 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 thekeyof the parameter to get.- Returns:
- the
Listwith all values of the requested parameter. Will be anempty listif 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
RequestParametersforUriInfo.getQueryParameters().
-
fromPath
public static RequestParameters fromPath(javax.ws.rs.core.UriInfo uriInfo)
- Parameters:
uriInfo- is theUriInfo.- Returns:
- a new instance of
RequestParametersforUriInfo.getPathParameters().
-
-