如何在Groovy中构建URL
我想,我想在这里使用,但不完全确定 我有以下代码:如何在Groovy中构建URL,groovy,uribuilder,Groovy,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 但我不
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
但我不想只使用字符串攻击(defurl=serverURL+“/fizz?widget=“+appURL
)。此外,我还希望URL编码等。我认为URLBuilder
是一种方法,但不确定
我看到了一个使用JAX-RS的例子,UriBuilder
:
String url = UriBuilder.fromUri(serverURL).path("fizz").queryParam("widget", appURL).build();
现在我只需要弄清楚如何在Groovy中实现这一点。
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
那么,为什么不采用与Java相同的方法呢?只需删除行末尾的分号;)