2014-06-13 42 views
1

我想,我想在這裏,但不能完全肯定使用URIBuilder ...如何構建URL在Groovy

我有以下代碼:

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中做到這一點?

+0

爲什麼不這樣做同樣的方式在Java中呢?只需在行尾添加分號;) – topr

回答

4

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