在Java中,我正在使用javax
´framework通过SSE
连接到服务器。为此,我需要构建一个用于访问服务器的URL。服务器上的端点的格式为http://path/queryparam/path/pathparam/path
http://localhost:8080/rest/events?topics=smarthome/items/chromecastchromecast6e63c8ef6787409f05664045a0bae715volumee9fe9f8fb4ff4c26872cba2980093a83/statechanged`
当我尝试使用WebTarget
进行构建时,它总是破坏参数或将其重新排序为:http://path/pathParams/queryparams
http://localhost:8080/rest/events/items/chromecastchromecast6e63c8ef6787409f05664045a0bae715volumee9fe9f8fb4ff4c26872cba2980093a83/statechanged?topics=smarthome
这是我的代码:
public WebTarget buildTarget(String path, String[] pathParams, Object[] pathParamObjects, String[] queryParams, Object[] queryParamObjects){
WebTarget target = webRootTarget
.path(path);
for(int i = 0; i < pathParams.length; i++){
target = target
.path("/{" + pathParams[i] + "}")
.resolveTemplate(pathParams[i], pathParamObjects[i]);
}
for(int i = 0; i < queryParams.length; i++){
target = target
.queryParam(queryParams[i], queryParamObjects[i]);
}
logger.info(target.getUri().toString());
return target;
}
WebTarget wt =requestHelper.buildTarget(
"/events",
new String[0],
new Object[0],
new String[]{"topics"},
new String[]{"smarthome"});
logger.info("wwt "+ wt.getUri());
WebTarget wt2= wt.path("items/chromecastchromecast6e63c8ef6787409f05664045a0bae715volumee9fe9f8fb4ff4c26872cba2980093a83/statechanged");
我尝试了多种不同的方法来强制如上所述构建链接。但是无论webtarget总是更改链接是什么,以便queryparameter站在最后一个位置或斜杠('/')都被%2f替换,这是我猜的UTF代码:
http://localhost:8080/rest/events?topics=smarthome%2Fitems%2Fchromecastchromecast6e63c8ef6787409f05664045a0bae715volumee9fe9f8fb4ff4c26872cba2980093a83%2Fstatechanged%2F
那么有人能告诉我问题是什么以及如何解决吗?预先感谢。