【问题标题】:Amazon Java SDK with Jersey's REST - Can't auth带有 Jersey REST 的 Amazon Java SDK - 无法验证
【发布时间】:2012-03-20 20:39:34
【问题描述】:

我有这个工作代码:

package javaapplication7;

import com.amazonaws.auth.PropertiesCredentials;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

public class JavaApplication7 {

    public static void main(String[] args){
        try {
            AmazonS3 s3 = new AmazonS3Client(new PropertiesCredentials(
                    JavaApplication7.class.getResourceAsStream("/AwsCredentials.properties")));
            System.out.println("OWNER: " + s3.getS3AccountOwner().toString());
        } catch (IOException ex) {
            Logger.getLogger(JavaApplication7.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

没有问题,我可以在控制台中看到所有者。

在我的网络服务项目中:

Principal.java

package WSAV.resources;

import WSAV.entities.AWS;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;

@Stateless
@Path("/amazonws")
public class Principal {

    @EJB
    private AWS aws;

    @GET
    @Path("/AWS")
    @Produces({"text/html", "text/plain"})
    public String getTest() {
        return aws.test();
    }
}

AWS.java

package WSAV.entities;

import com.amazonaws.auth.PropertiesCredentials;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.Singleton;

@Singleton
public class AWS {

    public AWS() {
    }

    public String test() {

        try {
            AmazonS3 s3 = new AmazonS3Client(new PropertiesCredentials(
                    AWS.class.getResourceAsStream("/AwsCredentials.properties")));
        } catch (IOException ex) {
            Logger.getLogger(AWS.class.getName()).log(Level.SEVERE, null, ex);
        }

    return "testing";  
    }
}

当我部署和测试网络服务时,网络显示:


类型: 异常报告

说明: 服务器遇到一个内部错误 () 阻止它完成此请求。

例外: javax.servlet.ServletException: java.lang.NullPointerException

根本原因: java.lang.NullPointerException


在控制台上:

ADVERTENCIA:EJB5184:在 EJB AWS 上调用期间发生系统异常,方法:public java.lang.String WSAV.entities.AWS.test() 广告:javax.ejb.TransactionRolledbackLocalException:从 bean 抛出的异常

{...}

广告:EJB5184:在调用 EJB 主体期间发生系统异常,方法:public java.lang.String WSAV.resources.Principal.getTest() 广告:javax.ejb.EJBTransactionRolledbackException

{...}

广告:StandardWrapperValve[ServletAdaptor]:PWC1406:servlet ServletAdaptor 的 Servlet.service() 抛出异常


我迷路了,不知道它可能是什么或我能做什么......也许创建另一个 Java 文件并与这个项目分开执行,但我需要 AWS 只在一个项目中工作......

谢谢!

【问题讨论】:

    标签: java rest amazon-web-services jersey authentication


    【解决方案1】:

    带有@EJB 注释的只能是托管类。 异常意味着您的 AWS 类不满足所有与 EJB 相关的要求。

    【讨论】:

      【解决方案2】:

      问题在于您的 AWS EJB bean 不符合 EJB 标准。 这里有一篇关于 REST 和 EJB 3.1 的好文章。 http://netbeans.dzone.com/articles/how-to-combine-rest-and-ejb-31

      我使用相同的步骤来调整您的代码。希望它对你有用。

      AWS.java

      @Singleton 
      public class AWS { 
      
      private AmazonS3 s3Client;
      
      @PostConstruct
      public void init() { 
      
          try { 
              s3Client= new AmazonS3Client(new PropertiesCredentials( 
                      AWS.class.getResourceAsStream("/AwsCredentials.properties"))); 
          } catch (IOException ex) { 
              Logger.getLogger(AWS.class.getName()).log(Level.SEVERE, null, ex); 
          }   
      } 
      
      public AmazonS3 getS3Client() {
      return s3Client;
      }
      
      } 
      

      Principal.java

      @Stateless                       
      @Path("/amazonws")                       
      public class Principal {                       
      
      @EJB                       
      private AWS aws;                       
      
      @GET                       
      @Path("/AWS")                       
      @Produces({"text/html", "text/plain"})                       
      public String getTest() {                       
          AmazonS3 s3=aws.getS3Client();  
          return s3.getS3AccountOwner().toString());                                        
      }                       
      }     
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-05-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-04-09
        相关资源
        最近更新 更多