5 UriBuilder - Reference Documentation
Authors: Bud Byrd
Version: 2.0.2
5 UriBuilder
The plugin also provides aUriBuilder
class to make building URLs with correct escaping simpler. This builder follows the same building conventions as the Jersey Request Builder.
5.1 Properties
Property | Description | Type |
---|---|---|
base | Base URL to start from. | String |
host | Host name part of the URL. | String |
port | Remote host port number. | Integer |
path | A list of path parts to add to the URL. | List |
query | A key/value pair map of query parameters to add to the URL. | Map |
scheme | The protocol scheme to use (http, https, etc). | String |
fragment | Fragment part of the URL (index.html#fragment). | String |
Note that if thebase
property is used, any of its URI parts will be overridden by any of the individual properties, if set. The exception is thepath
property, which will append the path parts to the path of thebase
URL that was specified.
The UriBuilder returns aURI
object, which can be assigned to theuri
property of the Jersey Request Builder.
5.2 Examples
Building a URI with the DSL-based syntax:// Produces "http://grails.org/doc/2.1.1/guide/introduction.html#coreFeatures"
UriBuilder.build {
base = 'http://grails.org'
path = ['doc', '2.1.1', 'guide', 'introduction.html']
fragment = 'coreFeatures'
}
// Produces "http://grails.org/doc/2.1.1/guide/introduction.html#coreFeatures" UriBuilder uriBuilder = new UriBuilder() uriBuilder.base = 'http://grails.org' uriBuilder.path = ['doc', '2.1.1', 'guide', 'introduction.html'] uriBuilder.fragment = 'coreFeatures' uriBuilder.build()
The URI builder supports path substitution as a convenience feature. You can not build a URI this way, but this allows authors to use a predefined URL "pattern" to replace path pieces with values determined at runtime. For example:UriBuilder
is typically used with staticbuild
methods, but if the gradual method is used (as in the example above),UriBuilder
will need to be instantiated.
// Produces "http://example.com/webapp/resource/item/id/5497" UriBuilder.build("http://example.com/webapp/resource/{resource}/id/{id}", "item", "5497")