[英]EJB + Hibernate + REST client LazyInitializationException
[英]How to connect EJB, REST and Client?
我在 netbeans 中有 2 個 java 項目,我想連接它們。 首先是基於Jboss服務器,包含ejb和rest。 EJB 與數據庫和休息服務包對象連接到 xml 並發送到客戶端女巫是標准的基於 Swing 的 gui 應用程序。 問題是我不知道接下來要做什么,因為當我嘗試從服務器接收任何數據時出現空指針異常。 我這樣做對嗎? 也許我的整個想法是錯誤的? 請幫忙。
編輯:我認為故障出在服務器端。 我不知道如何創建休息服務。 在 netbeans 的 WholesaleREST 類中,警告未配置 rest。 我單擊了“使用 Java EE6 規范配置 REST”,但服務器無法部署它並引發錯誤:
Deployment "vfs:///E:/Instalki/jboss/jboss-as-distribution-6.1.0.Final/jboss-6.1.0.Final/server/default/deploy/WholesaleApp.war" is in error due to the following reason(s): org.jboss.deployers.spi.DeploymentException: URL file:/E:/Instalki/jboss/jboss-as-distribution-6.1.0.Final/jboss-6.1.0.Final/server/default/tmp/vfs/automount6dbf7312f2f10b36/WholesaleApp.war-1ad4d6611c73bd02/ deployment failed
這個錯誤並沒有告訴我任何關於它的信息,我不知道該怎么做。 請問有什么想法嗎?
添加的文本結束,下面的其余部分是舊的。
這是我寫的代碼:
EJB 類:
@Stateless
public class WholesaleEJB {
@PersistenceContext(name="WholesaleAppPU")
EntityManager entityManager;
public List<Clients> getClients() {
Query q = entityManager.createQuery("select c from Clients c");
@SuppressWarnings("unchecked")
List<Clients> lista = q.getResultList();
return lista;
}
}
休息課:
@Path("/wholesale")
@Stateless
public class WholesaleREST implements WholesaleInterface{
@EJB
WholesaleEJB bean;
@Override
@GET
@Path("/get")
public String getCars() {
List<Clients> listOfClients = bean.getClients();
StringWriter sw = new StringWriter();
ClientsContainer container = new ClientsContainer(listOfClients);
JAXB.marshal(container, sw);
return sw.toString();
}
}
帶有 get 方法的客戶端類
public class HttpConnector {
public static String doGet(String url) {
try {
URLConnection connection = new URL(url).openConnection();
String charset = "UTF-8";
connection.setRequestProperty("Accept-Charset", charset);
return getResponse(connection);
} catch (Exception ex) {
ex.getMessage();
}
return null;
}
private static String getResponse(URLConnection connection)
throws UnsupportedEncodingException, IOException {
InputStream response = connection.getInputStream();
final char[] buffer = new char[0x10000];
StringBuilder out = new StringBuilder();
Reader in = new InputStreamReader(response, "UTF-8");
int read;
do {
read = in.read(buffer, 0, buffer.length);
if (read>0) {
out.append(buffer, 0, read);
}
} while (read>=0);
return out.toString();
}
}
最后一個從客戶端訪問 ejb 方法的類:
public class ClientRemoteAccess implements ClientInterface{
String url = "http://localhost:8080/WholesaleApp/wholesale";
@Override
public List<Clients> getClients() {
String recivedXML = HttpConnector.doGet(url+"/get");
ClientsContainer container = JAXB.unmarshal(
new StringReader(recivedXML), ClientsContainer.class);
return container.getListOfClients();
}
}
我認為你想要實現的架構是這樣的:
請注意,EJB 和客戶端之間沒有直接通信。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.