【问题标题】:Nested Requests in Spring BootSpring Boot 中的嵌套请求
【发布时间】:2017-11-29 14:27:22
【问题描述】:

使用 Spring Boot 创建 rest 服务时,会为特定路径完成 @GetMapping。我尝试从那时起发出一个正常的 HTTP 请求,但我得到了各种异常:org.springframework.web.util.NestedServletException: Handler dispatch failed; nested exception is java.lang.NoClassDefFoundError: org/apache/http/message/TokenParser 或错误:org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.IllegalStateException: Target host is null 等,这取决于我如何尝试绕过它。我原则上理解这个问题,但没有找到解决方案。我附上示例代码:

@GetMapping 看起来像这样:

  @GetMapping("/requesttest")
  public String TestRequest(@RequestParam("requestlink") String requestlink ) throws Exception {
    String requesttext = RHRequest.getHRequest(requestlink);
    return requesttext;
  }

请求类:

public class RHRequest {
  public static String getHRequest(String urlRequest) throws Exception {

    HttpGet httpRequest = new HttpGet(urlRequest);
    HttpClient httpClientRequest = new DefaultHttpClient();
    HttpContext localContextRequest = new BasicHttpContext();
    ResponseHandler<String> handlerRequest = new BasicResponseHandler();
    String responseBodyRequest = null;
    HttpResponse responseHRequest = httpClientRequest.execute(httpRequest, localContextRequest);
    responseBodyRequest = handlerRequest.handleResponse(responseHRequest);
    return responseBodyRequest.toString();
  }
}

【问题讨论】:

  • 您使用哪个版本的 Apache HttpClient?你能提供你的客户端和服务器的 pom.xml 吗?
  • 我使用:编译组:'org.apache.httpcomponents',名称:'httpclient',版本:'4.5.2' 编译组:'org.apache.httpcomponents',名称:'httpcore ',版本:'4.4'。是的,这就是问题所在!

标签: java spring httprequest


【解决方案1】:

问题已解决。

显然,SSLContexts 类已被重新定位到 httpclient 4.4 中的另一个包,以及其他修改。我将我的 httpcore 版本 4.4 和 httpclient 升级到 4.5 并修复了问题。

另一个问题是我用于请求的链接应该已经编码了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-15
    • 2021-10-07
    • 2015-06-17
    • 1970-01-01
    • 2017-04-08
    • 2016-09-13
    • 1970-01-01
    • 2018-11-27
    相关资源
    最近更新 更多