使用3条信息(方案,基本URL和后缀URL)构建URL / URI

granadaCoder:

我有3条信息构成一个url / uri。

String myscheme = "https";
String basePath = "www.theothercompany.com";
String suffixPath = "/api/v1/things";

如何使用(仅)上述三件事“组成”完整的URI或URL。

我已经经历过以下几个构造函数:https : //docs.oracle.com/javase/8/docs/api/java/net/URI.htmlhttps://docs.oracle.com/javase/8/docs /api/java/net/URL.html

就像我试过这样:

   URI x = new URI(myscheme , basePath , suffixPath);

要么

   URI y = new URI(myscheme , basePath , suffixPath, "");

但我不断得到类似

httpswww.theothercompany.comapi / v1 / things

显然,删除了诸如“://”和“ /”之类的“部分”。

我没有端口,ssp或用户或文本文件...或在上面2个oracle链接的构造函数中看到的其他内容。

我不敢相信这阻碍了我!

以下是一些其他(已经)引入的软件包。我不想添加另一个(例如org.apache.http.client.utils.URIBuilder).....来膨胀依赖项。

import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.reactive.function.client.WebClient;

compile group: 'org.springframework.boot', name: 'spring-boot-starter-webflux', version: springBootVersion

compile group: 'org.apache.commons', name: 'commons-lang3', version: commonsLangVersion

compile 'org.projectreactor:reactor-spring:1.0.1.RELEASE'

附加

好的,按照建议,我修复了这3个部分的标签错误。

   String myScheme = "https";
    String myHost = "www.theothercompany.com";
    String myPath = "/api/v1/things";

    java.net.URL computedFullUrl = null;
    String urlToStringValue = "";
    String urlToExternalFormValue = "";
    String urlToUriStringValue = "";
    try {

        /* this works....or does not exception out */
        computedFullUrl = new java.net.URL(myScheme, myHost, myPath);

        if (null != computedFullUrl) {
            urlToStringValue = computedFullUrl.toString();
            urlToExternalFormValue = computedFullUrl.toExternalForm();
            urlToUriStringValue = computedFullUrl.toURI().toString();
        }
    } catch (Exception ex) {
        //throw new RuntimeException((ex));
        // temporary swallow
        String temp = ex.getMessage();
        ex.printStackTrace();
    }

因此上述工作。

我得到(所有三个“转换回完整字符串”的值):

https://www.theothercompany.com/api/v1/things

我终于弄明白了。

它与URI或URL无关。

当我从属性文件中读取值时,开头的“ /”被从“ / api / v1 / things”中剥离

字符串中的值为“ api / v1 / things”。(当然没有引号)。那是我的问题。

我将把这个问题搁置一旁,以便其他人可以从我的专业中学到东西。

加阿阿

霍尔格:

该问题部分是由于未使用正确的技术术语引起的:

  • www.theothercompany.com主机名,而不是“ basePath”。
  • /api/v1/things路径,而不是“ suffixPath”

显然,您想构造一个分层的 URI,但是您使用的构造函数之一是用来构造一个不透明的 URI¹,它需要参数schemessp(方案的特定部分)和fragment

构造函数的文档恰恰说明了结果:

此构造函数首先使用给定的组件以字符串形式构建URI,如下所示:

  1. 最初,结果字符串为空。
  2. 如果给出了方案,则将其附加到结果后,然后是冒号(':')。
  3. 如果给出了特定于方案的部分,则将其追加。任何字符不是一个合法的URI字符引用
  4. 最后,如果给出了一个片段,则'#'在字符串后附加一个哈希字符(),然后是该片段。引用了不是合法URI字符的任何字符。

导致https:www.theothercompany.com#/api/v1/things

由于要构造组成层次URI schemehostpath第二个构造是正确的:

public URI​(String scheme, String host, String path, String fragment)

从给定的组件构造一个分层的URI。

因此使用new URI(myscheme , basePath , suffixPath, "")导致https://www.theothercompany.com/api/v1/things#

当您将其更改为时new URI(myscheme , basePath , suffixPath, null),您将得到https://www.theothercompany.com/api/v1/things

这证明了为什么命名工厂比重载的构造函数更可取,因为两个构造函数之间的语义差异只是一个参数,并不是很直观。


¹或通过手动指定语法元素来构造分层URI,这很少需要

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章