【问题标题】:How to build URL in Groovy如何在 Groovy 中构建 URL
【发布时间】:2014-08-04 05:45:54
【问题描述】:

我想,我想在这里使用URIBuilder,但不完全确定...

我有以下代码:

String serverURL = getServerURL(); // ex: "http://somesrv.example.com"
String appURL = getAppURL(); // ex: "http://myapp.example.com"

我现在需要将两者相加,以便生成以下内容:

http://somesrv.example.com/fizz?widget=http://myapp.example.com

但我不只是想使用字符串攻击 (def url = serverURL + "/fizz?widget=" + appURL)。另外我想要 URL 编码等。同样,我认为 URLBuilder 是这里的方式,但不确定。

我见过一个使用 JAX-RS'UriBuilder 的示例:

String url = UriBuilder.fromUri(serverURL).path("fizz").queryParam("widget", appURL).build();

现在我只需要弄清楚如何在 Groovy 中执行此操作?

【问题讨论】:

  • 为什么不直接用 Java 中的方法来做呢?只需删除行尾的分号;)

标签: groovy uribuilder


【解决方案1】:

URIBuilder 是要走的路。

def serverURL = "http://somesrv.example.com"
def appURL = "http://myapp.example.com"

def concat = new URIBuilder(serverURL)
concat.setPath("/fizz")
concat.addQueryParam("widget", appURL)

println concat

输出:

http://somesrv.example.com/fizz?widget=http%3A%2F%2Fmyapp.example.com

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-19
    • 1970-01-01
    • 2022-11-15
    • 2016-05-12
    相关资源
    最近更新 更多