获取值并将其作为参数传递给Java函数

huangapple go评论75阅读模式
英文:

Get Value and pass as parameter to function in Java

问题

ResponseEntity<String> respEntity = null;
try {
    respEntity = getConfiguredRestTemplate().exchange(uri.toString()
            , HttpMethod.GET
            , entity
            , new ParameterizedTypeReference<String>() {
            });
    log.debug("URL to retrieve a document : {}", respEntity.getBody());
}

// The respEntity.getBody() returns {"url":"https://aps-fst"}

// I want to send only the value - https://aps-fst as parameter to a function
// to download the content in the URL. How to extract only the URL value and
// pass it as parameter of type URL / String ?

英文:
 ResponseEntity&lt;String&gt; respEntity = null;
        try {
            respEntity = getConfiguredRestTemplate().exchange(uri.toString()
                    , HttpMethod.GET
                    , entity
                    , new ParameterizedTypeReference&lt;String&gt;() {
                    });
            log.debug(&quot;URL to retrieve a document : {}&quot;, respEntity.getBody());
}

The respEntity.getBody() returns {"url":"https://aps-fst"}

I want to send only the value - https://aps-fst as parameter to a function to download the content in the URL. How to extract only the URL value and pass it as parameter of type URL / String ?

答案1

得分: 0

你可以使用 jackson 中的 ObjectMapper,将响应体转换为一个映射(map),从中可以取出 url 的值。你可以在这里找到一个示例:链接

英文:

You can use ObjectMapper from jackson and have the response body transformed into a map from which you can take the url value. You can find an example here.

答案2

得分: -1

   String jsonString = respEntity.getBody();
   JSONObject obj = new JSONObject(jsonString);
   String s3urlvalue = obj.getString("url");
   log.debug("S3 URL  to retrieve a document : {}", s3urlvalue);

我能够通过上述代码获得值
英文:
  String jsonString = respEntity.getBody();
   JSONObject obj = new JSONObject(jsonString);
   String s3urlvalue = obj.getString(&quot;url&quot;);
   log.debug(&quot;S3 URL  to retrieve a document : {} &quot;, s3urlvalue);

I am able to get value with above code

huangapple
  • 本文由 发表于 2020年8月28日 20:38:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/63633960.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定