【问题标题】:Amazon Credentials Method not found未找到 Amazon 凭证方法
【发布时间】:2012-06-19 14:38:16
【问题描述】:

所以我的下一个问题是这段代码。似乎没有找到方法,我的眼睛没有受过训练。对此有任何帮助吗?

package packeging;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Calendar;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.amazonaws.HttpMethod;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.auth.BasicAWSCredentials;
import org.apache.http.*;
/**
 * Servlet implementation class Hashtastic
 */
@WebServlet("/Hashtastic")
public class Hashtastic extends HttpServlet {
    private static final long serialVersionUID = 1L;
    private final static String BUCKET_NAME = "idlatestingbucket";//http://s3.amazonaws.com/THESISDB/techy.jpg
    private final static String FILE_NAME = "TestPicture/wallpaper-264411.png";
    private final static String ACCESS_KEY = "Fakepass";
    private final static String SECRET_KEY = "Fakekey";
    /**
     * Default constructor. 
     */
    public Hashtastic() {
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
         PrintWriter out = response.getWriter();
          Calendar cal = Calendar.getInstance();
         cal.add(Calendar.SECOND, 1000);
         Date expDate = cal.getTime();
         out.println(expDate+"\n");

         BasicAWSCredentials cre = new BasicAWSCredentials(ACCESS_KEY, SECRET_KEY);
        AmazonS3 s3 = new AmazonS3Client(cre);
        String url = s3.generatePresignedUrl(BUCKET_NAME, FILE_NAME, expDate, HttpMethod.GET).toString();
        out.println(url);
         out.close();
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
    }

}

我收到此 500 错误。它说它缺少一种方法。我的库中有 jar 和 eclipse 插件。

description The server encountered an internal error () that prevented it from fulfilling this request.

exception 

javax.servlet.ServletException: Servlet execution threw an exception


root cause 

java.lang.NoSuchMethodError: org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager: method <init>()V not found
    com.amazonaws.http.ConnectionManagerFactory.createThreadSafeClientConnManager(ConnectionManagerFactory.java:26)
    com.amazonaws.http.HttpClientFactory.createHttpClient(HttpClientFactory.java:83)
    com.amazonaws.http.AmazonHttpClient.<init>(AmazonHttpClient.java:116)
    com.amazonaws.AmazonWebServiceClient.<init>(AmazonWebServiceClient.java:60)
    com.amazonaws.services.s3.AmazonS3Client.<init>(AmazonS3Client.java:291)
    com.amazonaws.services.s3.AmazonS3Client.<init>(AmazonS3Client.java:273)
    packeging.Hashtastic.doGet(Hashtastic.java:48)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)


note The full stack trace of the root cause is available in the Apache Tomcat/7.0.27 logs.

有什么帮助吗?

【问题讨论】:

    标签: java servlets amazon


    【解决方案1】:

    如果您的类路径中有错误版本的 httpclient jar,或者如果您的类路径中有多个版本的 jar(例如同时具有 httpclient-4.0.1.jar 和 httpclient-4.1),则可能会发生这种情况。 1.jar)。

    这也可能是由另一个包含同一类的不同版本的 jar 引起的。例如,我知道 gwt-dev.jar 包含一个版本的 ThreadSafeClientConnManager。如果是这种情况,问题可能可以通过调整构建路径顺序将 httpclient.jar 放在 gwt-dev.jar 之前(或其他导致问题的 jar)来解决。

    【讨论】:

    • 是的,我大约在您发布的同一时间发现了这一点。无论如何,这就是那个确切的问题,感谢您的帮助!
    • 如果应用程序服务器(如 Websphere 8)包含比 httpclient-4.1.x 更旧的版本,也会发生这种情况,因为在 4.1 中实现了默认构造函数。在这种情况下,您必须将应用程序上的类加载策略从 PARENT_FIRST 更改为 PARENT_LAST,并在 EAR/WAR 中包含 4.1+ JAR。
    【解决方案2】:

    根据对这个完全相同的异常的经验,它很有可能是由 gwt-dev 出现在类路径中的 aws-java-sdk 之前引起的,并且由于 gwt-dev 包含冲突的(就类加载而言)版本org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager

    如果您碰巧使用 Maven,请按如下方式重新排序您的依赖项,并可能向其他维护者添加关于排序重要性的警告。

    <dependency>
       <groupId>com.amazonaws</groupId>
       <artifactId>aws-java-sdk</artifactId>
       <version>1.3.26</version>
    </dependency>
    
    <dependency>
      <groupId>com.google.gwt</groupId>
      <artifactId>gwt-dev</artifactId>
      <version>2.3.0</version>
    </dependency>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-05-06
      • 2019-03-14
      • 1970-01-01
      • 2014-04-10
      • 2019-06-01
      • 2021-09-29
      • 2022-12-16
      • 1970-01-01
      相关资源
      最近更新 更多