Class RequestParameters


  • public class RequestParameters
    extends Object
    This class helps to deal with UriInfo and MultivaluedMap from the JAX-RS API. E.g. if you have a REST query operation for a collection URI you can use UriInfo in case you want to support a mixture of optional and required parameters. The methods provided here throw according exceptions such as BadRequestException and already support conversion of values.
    Since:
    2.0.0
    • Constructor Detail

      • RequestParameters

        public RequestParameters​(javax.ws.rs.core.MultivaluedMap<String,​String> parameters)
        The constructor.
        Parameters:
        parameters - is the MultivaluedMap containing the parameters to wrap.
    • 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 of targetType.
        Parameters:
        key - is the key of the parameter to get.
        targetType - is the Class reflecting the type to convert the value to. Supports common Java standard types such as String, Long, Double, BigDecimal, etc.
        required - - true if the value is required and a BadRequestException is thrown if it is not present, false otherwise (if optional).
        Returns:
        the value for the given key converted to the given targetType. May be null if required is false .
        Throws:
        javax.ws.rs.WebApplicationException - if an error occurred. E.g. BadRequestException if a required parameter is missing or InternalServerErrorException if the given targetType is not supported.
      • convertValue

        protected <T> T convertValue​(String value,
                                     Class<T> targetType)
                              throws ParseException
        Converts the given value to the given targetType.
        Type Parameters:
        T - is the generic type of targetType.
        Parameters:
        value - is the value to convert.
        targetType - is the Class reflecting the type to convert the value to.
        Returns:
        the converted value.
        Throws:
        ParseException - if parsing of the given value failed while converting.
      • get

        public String get​(String key)
                   throws javax.ws.rs.BadRequestException
        Gets the parameter as single value with the given key as String.
        Parameters:
        key - is the key 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 (see getList(String)).
      • getFirst

        public String getFirst​(String key)
        Gets the parameter with the given key as String. Unlike get(String) this method will not throw an exception if the parameter is multi-valued but just return the first value.
        Parameters:
        key - is the key 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 the List of all value for the parameter with with the given key. 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 the key of the parameter to get.
        Returns:
        the List with all values of the requested parameter. Will be an empty list if the parameter is not present.
      • fromQuery

        public static RequestParameters fromQuery​(javax.ws.rs.core.UriInfo uriInfo)
        Parameters:
        uriInfo - is the UriInfo.
        Returns:
        a new instance of RequestParameters for UriInfo.getQueryParameters().
      • fromPath

        public static RequestParameters fromPath​(javax.ws.rs.core.UriInfo uriInfo)
        Parameters:
        uriInfo - is the UriInfo.
        Returns:
        a new instance of RequestParameters for UriInfo.getPathParameters().