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 thebaseproperty is used, any of its URI parts will be overridden by any of the individual properties, if set. The exception is thepathproperty, which will append the path parts to the path of thebaseURL that was specified.
The UriBuilder returns aURIobject, which can be assigned to theuriproperty 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:UriBuilderis typically used with staticbuildmethods, but if the gradual method is used (as in the example above),UriBuilderwill 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")