2012-03-20 65 views
1

我有這方面的工作代碼:亞馬遜的Java SDK與新澤西州的REST - 無法權威性

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); 
     } 
    } 
} 

而且沒有任何問題,我可以看到寫在控制檯的所有者。

在Web服務的項目,我有:

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"; 
    } 
} 

當我部署和測試w^EB服務,網頁顯示:


類型: 異常報告

描述: 服務器遇到阻止其完成此請求一個內部錯誤()。

異常: javax.servlet.ServletException:顯示java.lang.NullPointerException

根本原因: 顯示java.lang.NullPointerException


在控制檯上:

ADVERTENCIA:EJB5184:一系統異常在EJB AWS調用期間發生,方法:public java.lang.String WSAV.entities.AWS.test() ADVERTENCIA:javax.ejb.TransactionRolledbackLocalExcepti上:從異常拋出豆

{...}

ADVERTENCIA:EJB5184:在EJB校長,方法調用期間發生系統異常:公共java.lang.String中WSAV.resources.Principal.getTest( ) ADVERTENCIA:javax.ejb.EJBTransactionRolledbackException

{...}

ADVERTENCIA:StandardWrapperValve [ServletAdaptor]:PWC1406:Servlet.service()進行的servlet ServletAdaptor拋出異常


我迷路了不知道它可能是什麼或我該怎麼做......也許創建另一個Java文件並單獨執行此項目,但我需要AWS在一個項目中工作......

謝謝!

回答

0

註釋@EJB coulb只能是託管類。 異常意味着您的AWS類不符合所有與EJB相關的要求。

0

問題在於您的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());           
}      
}