java - 如何从 spring mvc Controller 调用外部 api

标签 java spring spring-mvc

你好,我的 Controller 是

@Controller
public class HomeController{ 
@RequestMapping(value = "/inString={name}", method = RequestMethod.GET)

      public @ResponseBody String getGreeting(@PathVariable String name, String result) {

      //below is the url has given.I want to pass the name string to the below url for the input_text parameter
      //http://ipaddress/input?input_text=tell me something&target_val=hi;

       result=the_result_provided_from_the_above_URL ; 
        return result;
}
}

我能够获取路径变量名称。现在我想将名称传递给另一个返回字符串的 URL,并且该字符串将从 Controller 返回。我尝试过,但无法从 Controller 调用 URL .

最佳答案

您可以使用 Apache 的 HttpClient 调用 servlet。这是一个例子:

...
private static final String SERVLETURL = "http://ipaddress/input?input_text=tell";
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpGet httpGet = new HttpGet(SERVLETURL);
CloseableHttpResponse response = httpclient.execute(httpGet);
HttpEntity entity = response.getEntity();
String responseBody = getStringFromInputStream(response.getEntity().getContent());
//Processing here
EntityUtils.consume(entity);
...

关于java - 如何从 spring mvc Controller 调用外部 api,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31472798/

相关文章:

java - 在java中序列化对象

spring - 如何将自定义 SSL 证书注入(inject) springbok bootBuildImage

Java 8 lambda 和抽象类接口(interface)的扩展

java - 使用 MappingJackson2JsonView 支持在 Spring REST 中返回 JSON 响应比 @ResponseBody 注释有什么优势?

java - Thymeleaf - 错误解析模板 "email-inlineimage.html"

java - Jersey : Do I need to have maven dependency for JSR as well? 为什么?

java - WebBindingInitializer 将不会执行

Java 使用正则表达式解析 Wiki 语法

java - spring autowire 不工作 Autowiring 依赖项注入(inject)类型的限定 bean 失败;

java - 使用句子检测器评估工具时,OpenNLP 如何计算漏报?