我在netbeans中有2个Java项目,我想将它们连接起来。首先基于Jboss服务器,包含ejb和rest。 EJB与databse连接,并将其余的Service Pack对象打包为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();
}
}
最佳答案
我认为您想要实现的架构是这样的:
common-model:在这里放置域模型,例如JPA实体和类ClientsContainer
restfull服务:取决于公共模型,并包含与数据库进行通信的EJB / JPA层以及将数据作为资源公开的RESTful Web服务。
restful-client:依赖于通用模型并通过HTTP与restfull-service通信的swing富客户端。
注意,EJB与客户端之间没有直接通信。
关于java - 如何连接EJB,REST和客户端?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32614481/