我正在使用TomCat 9构建一个动态Web项目,并设置java.lang.ClassNotFoundException:org.apache.http.HttpEntity。
我的项目是进行Oauth调用,它从UI获取客户端ID和秘密(index.html)---> servlet(outPutValue.java)----> Addition.java,它使用客户端凭据调用API,并且应该获取访问令牌,但现在如果我添加IO异常处理,它会抛出以下错误
SEVERE:用于servlet [controller.outPutValue]的Servlet.service() 上下文与路径[/ Calculator]抛出异常[Servlet执行 抛出异常]与根本原因java.lang.ClassNotFoundException: org.apache.http.HttpEntity at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1269) 在 org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1104) 在controller.outPutValue.doPost(outPutValue.java:48)
加法代码 -
public class addition {
private int first;
private int second;
public int getFirst() {
return first;
}
public void setFirst(int first) {
this.first = first;
}
public int getSecond() {
return second;
}
public void setSecond(int second) {
this.second = second;
}
public void add(String first,String second) throws ClientProtocolException, IOException {
System.out.println("This is after coming to addition");
HttpClient client = HttpClientBuilder.create().build();
HttpPost post = new HttpPost("https://api-XYZ.com/oauth2/token");
post.setHeader("Content-Type", "application/x-www-form-urlencoded");
post.setHeader("Accept","application/json");
post.setHeader("Accept-Language", "en-US,en");
List <NameValuePair> body = new ArrayList <NameValuePair>();
body.add(new BasicNameValuePair("client_id",first));
body.add(new BasicNameValuePair("client_secret",second));
body.add(new BasicNameValuePair("grant_type","client_credentials"));
post.setEntity(new UrlEncodedFormEntity(body));
HttpResponse response=null;
response = client.execute(post);
System.out.println(response.getStatusLine().getStatusCode());
String auth_resp=null;
auth_resp = EntityUtils.toString(response.getEntity());
System.out.println(auth_resp);
}
Str --- PFA enter image description here