【发布时间】: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