Class AppUrlBuilder
RestPathBuilder could be used directly).
The default server creates two contexts: main (including rest) on 8080 and admin context on 8081. For simple server, there would be only one context where admin would be available on sub-url "/admin".
Url-related configurations:
server.applicationContextPath- main context pathserver.rootPath- rest mapping url (main context path is also applied for rest)server.adminContextPath- admin context path
By default, class would create "localhost" based urls (because there is no way to know server host).
To build for a particular host use forHost(String) - this way server ports will be applied.
Application could also be behind a proxy (like apache) which hides real server port. In this case use
forProxy(String) - server port would not be applied. Also, apache might be configured to
serve application for a sub-path like "http://myhost.com/myapp/" - use this as a proxy url to build
correct application urls.
- Since:
- 26.09.2025
- See Also:
-
Constructor Summary
ConstructorsModifierConstructorDescriptionAppUrlBuilder(io.dropwizard.core.setup.Environment environment) Default constructor for "localhost"-based urls.AppUrlBuilder(Supplier<io.dropwizard.core.setup.Environment> environment) Special constructor for instance creation before application startup.protectedAppUrlBuilder(Supplier<io.dropwizard.core.setup.Environment> environment, String host, boolean proxied) Create an app url builder. -
Method Summary
Modifier and TypeMethodDescriptionBuild url relative to the admin context (server.adminContextPath).Build url relative to the main context (server.applicationContextPath).Create builder instance for specific host.Create builder instance for proxied application.Admin context is configured withserver.adminContextPathand it is "/" by default ("/admin" for simple server).intGet admin application context port.Main context is configured withserver.applicationContextPathand it is "/" by default.intGet main application context port.Rest context is configured withserver.rootPathand it is "/" by default.<K> RestPathBuilder<K>Build url relative to application rest mapping.Build url relative to application rest mapping.<K> RestPathBuilder<K>Manual rest path building.Build url relative to application root ("/").
-
Constructor Details
-
AppUrlBuilder
@Inject public AppUrlBuilder(io.dropwizard.core.setup.Environment environment) Default constructor for "localhost"-based urls. It would be possible to create specialized builders withforHost(String)orforProxy(String)methods.Note: method annotated with
Injectto be able to obtain builder directly from guice context.- Parameters:
environment- environment instance
-
AppUrlBuilder
Special constructor for instance creation before application startup.- Parameters:
environment- environment supplier
-
AppUrlBuilder
protected AppUrlBuilder(Supplier<io.dropwizard.core.setup.Environment> environment, String host, boolean proxied) Create an app url builder.- Parameters:
environment- environment objecthost- required hostproxied- true if server is proxied (port already declared in host)
-
-
Method Details
-
forHost
Create builder instance for specific host. The host must contain protocol and the host name like "https://myhost.com" and builder will append correct port and build urls relatively.- Parameters:
host- application host (and protocol)- Returns:
- builder instance
-
forProxy
Create builder instance for proxied application. In this case, real application port is not used (most commonly, apache would redirect port 80 into application port). Also, apache could be configured to serve application from a sub-path like "http://myhost.com/myapp/" (provided host myst point to the application root).- Parameters:
proxy- proxied host (and protocol)- Returns:
- builder instance
-
root
Build url relative to application root ("/"). No server configurations applied in this case.Warning: don't mix root urls with the main context urls as application main context could differ.
To get root url:
#root("/"),- Parameters:
path- target path, relative to application root (could contain String.format() placeholders: %s)args- variables for path placeholders (String.format() arguments)- Returns:
- application path url
-
rest
Build url relative to application rest mapping. Configured main context pathserver.applicationContextPathand rest pathserver.rootPathwould be prepended automatically.To get rest root url:
#rest("/"),- Parameters:
path- target path, relative to rest root (could contain String.format() placeholders: %s)args- variables for path placeholders (String.format() arguments)- Returns:
- application resource url
- See Also:
-
rest
Build url relative to application rest mapping. Configured main context pathserver.applicationContextPathand rest pathserver.rootPathwould be prepended automatically.Resource path is applied from resource
Pathannotation. Resource method path could be declared with a method call:RestPathBuilder.method(ru.vyarus.dropwizard.guice.url.util.Caller)(in this case all non-null arguments mapped as query and path params would be applied automatically). If resource-method-based declaration not used, then it might be required to provide manually values for declared path params (ResourceParamsBuilder.pathParam(String, Object)) and (optionally) apply query params.Example:
rest(MyResource.class).method(res -> res.doGet("abc", 12).build()), wherepublic Response doGet(@PathParam("par1") String par1, @QueryParam("qp") int qpand so path param "par1" and query param "qp" would be registered automatically (assuming path param used in resource path).If target method is in sub-resource, use
RestPathBuilder.subResource(String, Class, Object...)to append its path and switch context class.Direct resource class and method usage allows to get refactoring-safe urls.
- Type Parameters:
K- resource type- Parameters:
resource- resource class- Returns:
- resource path builder
-
rest
Manual rest path building. Useful for complex cases with matrix params in the middle.- Type Parameters:
K- resource type- Parameters:
path- path builderresource- target resource class- Returns:
- resource path builder
-
app
Build url relative to the main context (server.applicationContextPath).To get main context root url:
#main("/"),- Parameters:
path- target path, relative to the main context (could contain String.format() placeholders: %s)args- variables for path placeholders (String.format() arguments)- Returns:
- application path url
-
admin
Build url relative to the admin context (server.adminContextPath).To get admin context root url:
#admin("/"),- Parameters:
path- target path, relative to the admin context (could contain String.format() placeholders: %s)args- variables for path placeholders (String.format() arguments)- Returns:
- application path url
-
getAppPort
public int getAppPort()Get main application context port.- Returns:
- the actual port the connector is listening to, or -1 if it has not been opened, or -2 if it has been closed.
-
getAdminPort
public int getAdminPort()Get admin application context port.- Returns:
- the actual port the connector is listening to, or -1 if it has not been opened, or -2 if it has been closed.
-
getRestContextPath
Rest context is configured withserver.rootPathand it is "/" by default. Also, rest is mapped under the main context, so if main context mapping changed withserver.applicationContextPathit must also be counted. The returned path counts both and so is relative to the server root.- Returns:
- rest context (with a context path), relative to the server root path
-
getAppContextPath
Main context is configured withserver.applicationContextPathand it is "/" by default.- Returns:
- main context (with a context path), relative to the server root path
-
getAdminContextPath
Admin context is configured withserver.adminContextPathand it is "/" by default ("/admin" for simple server). Note that by default, admin is on different port.- Returns:
- main context (with a context path), relative to the server root path
-