2

我想,我想在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 中做到这一点?

4

1 回答 1

8

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
于 2014-06-13T15:58:16.510 回答