Java 如何将列表项作为查询参数添加到字符串url中?

Java 如何将列表项作为查询参数添加到字符串url中?,java,Java,我有一个包含一些值的表,我正在使用哈希映射来提供键值映射: |type|uri |client | |----|----------------|-------| | |https://test.com|test | |aspx |test | |aspx|https://test.com| | 我想做的是根据提供的值构建一个url。所以基本上下面我要做的就是检查是否有任何值不是空的,将它们添加到一个数组列表中,

我有一个包含一些值的表,我正在使用哈希映射来提供键值映射:

|type|uri             |client |
|----|----------------|-------|
|    |https://test.com|test   |
|aspx                 |test   |
|aspx|https://test.com|       |
我想做的是根据提供的值构建一个url。所以基本上下面我要做的就是检查是否有任何值不是空的,将它们添加到一个数组列表中,就像这样

private static final String CLIENT = "client";
private static final String URI = "uri";
private static final String TYPE = "type";

public void GetAuthorizeMissingRequiredFields(final Table table) {

    final String url = “/test/platform/auth”;

    final List<TableRow> rows = table.getTableRows();
    final Map<String, String> headers = getCurrentHeaders().getMap();
    ArrayList<String> queryParamsList = new ArrayList<>();

    rows.forEach(row -> {

                final String clientValue = row.getCell("client");
                final String uriValue = row.getCell("uri");
                final String typeValue = row.getCell("type");

                if (!clientValue.isEmpty()) {
                    queryParamsList.add(CLIENT + clientValue);
                }

                if (!uriValue.isEmpty()) {
                    queryParamsList.add(URI + uriValue);
                }

                if (!typeValue.isEmpty()) {
                    queryParamsList.add(TYPE + typeValue);
                }

            });

}
情景2:

/test/platform/auth?type=aspx&client=test
情景3:

/test/platform/auth?type=aspx&uri=https://test.com

我的问题是如何将列表项作为查询参数附加到字符串中。

现在,我看到的一个问题是添加到列表中的内容:添加例如
CLIENT+clientValue
(导致“clienttest”),但我认为更好的方法是
CLIENT+“=”+clientValue
(导致“CLIENT=test”)

第二个问题是将所有地图行的内容以该形式附加到
ArrayList
。因此,如果表中有多行,那么列表可以轻松地包含多个客户端、URI和类型

现在,您在示例中故意忽略或忽略了这一点。由于我不知道您的确切目标,我可以建议您将整个
ArrayList
的内容以当前形式附加在下面:

url+=“?”+String.join(“&”,queryParamsList)

或者,或者

url+=“?”+queryParamsList.stream().collect(collector.joining(“&”)

如果要为每行构建单独的url字符串,则需要url字符串列表:

final String URL_CORE = “/test/platform/auth”;
List<String> urls = new ArrayList<>();
rows.forEach(row -> {
    final String clientValue = row.getCell("client");
    final String uriValue = row.getCell("uri");
    final String typeValue = row.getCell("type");
    
    urls.add(URL_CORE + "?" + 
        (clientValue.isEmpty ? "" : CLIENT + "=" + clientValue + "&") +
        (uriValue.isEmpty    ? "" : URI    + "=" + uriValue    + "&") +
        (typeValue.isEmpty   ? "" : TYPE   + "=" + typeValue);
});

最终字符串URL\u CORE=“/test/platform/auth”;
列表URL=新的ArrayList();
行。forEach(行->{
最终字符串clientValue=row.getCell(“客户端”);
最终字符串uriValue=row.getCell(“uri”);
最终字符串类型值=row.getCell(“类型”);
添加(URL_核心+“?”+
(clientValue.isEmpty?“:客户端+”=“+clientValue+”&”)+
(uriValue.isEmpty?”:URI+“=”+uriValue+“&”)+
(typeValue.isEmpty?”:TYPE+“=”+typeValue);
});
final String URL_CORE = “/test/platform/auth”;
List<String> urls = new ArrayList<>();
rows.forEach(row -> {
    final String clientValue = row.getCell("client");
    final String uriValue = row.getCell("uri");
    final String typeValue = row.getCell("type");
    
    urls.add(URL_CORE + "?" + 
        (clientValue.isEmpty ? "" : CLIENT + "=" + clientValue + "&") +
        (uriValue.isEmpty    ? "" : URI    + "=" + uriValue    + "&") +
        (typeValue.isEmpty   ? "" : TYPE   + "=" + typeValue);
});