使用 wildfly 26 时无法使用 EJB Remote 读取响应
Failed to read response with EJB Remote when using wildfly 26
我面临的问题是客户端无法使用 Class 实体(ETC 列表、实体)从服务器读取响应。同样对于 String、int、Double 类型的对象,它仍然可以正常工作。
EJBServer 项目
Class 实体:AnimalEntity.java
public class 动物实体 {
private String animalName;
private String animalCountry;
private String animaltype;
private double animalWeigth;
public AnimalEntity(String animalName, String animalCountry, String animaltype, double animalWeigth) {
super();
this.animalName = animalName;
this.animalCountry = animalCountry;
this.animaltype = animaltype;
this.animalWeigth = animalWeigth;
}
public String getAnimalName() {
return animalName;
}
public void setAnimalName(String animalName) {
this.animalName = animalName;
}
..............
}
接口:AnimalServiceRemote.java.
import com.ejb.entity.AnimalEntity;
public interface AnimalServiceRemote {
public String findByName(String strName);
public AnimalEntity Request(String strName);
}
- 豆子Class:AnimalService.java
import jakarta.ejb.LocalBean;
import jakarta.ejb.Remote;
import jakarta.ejb.Stateless;
@Stateless
@LocalBean
@Remote(AnimalServiceRemote.class)
public class AnimalService implements AnimalServiceRemote {
@Override
public String findByName(String strName) {
List<AnimalEntity> lst= new ArrayList<AnimalEntity>();
AnimalEntity ani= new AnimalEntity("Dog", "Japan", "Type1", 30);
AnimalEntity ani1= new AnimalEntity("Cat", "USA", "Type2", 150);
AnimalEntity ani2= new AnimalEntity("Fish", "Ger", "Type3", 3);
lst.add(ani);
lst.add(ani1);
lst.add(ani2);
for (int i = 0; i < lst.size(); i++) {
if (lst.get(i).getAnimalName().equals(strName)){
return "Name: "+ lst.get(i).getAnimalName() +" - Country: "+ lst.get(i).getAnimalCountry() +" - Weigth: "+lst.get(i).getAnimalWeigth();
}
}
return null;
}
@Override
public AnimalEntity Request(String strName) {
List<AnimalEntity> lst= new ArrayList<AnimalEntity>();
AnimalEntity ani= new AnimalEntity("Dog", "Japan", "Type1", 30);
AnimalEntity ani1= new AnimalEntity("Cat", "USA", "Type2", 150);
AnimalEntity ani2= new AnimalEntity("Fish", "Ger", "Type3", 3);
lst.add(ani);
lst.add(ani1);
lst.add(ani2);
AnimalEntity animal= new AnimalEntity();
for (int i = 0; i < lst.size(); i++) {
if (lst.get(i).getAnimalName().equals(strName)){
animal=lst.get(i);
return animal;
}
}
return null;
}
}
AppClient
- 主要Class:Myclient.java
import javax.naming.Context;
import javax.naming.InitialContext;
import com.ejb.entity.AnimalEntity;
import com.ejb.remote.AnimalServiceRemote;
public class Myclient {
public static void main(String[] args) {
try {
System.out.println("Proccessing...!" );
// Setting Config InitContext
Properties props = new Properties();
props.put(Context.INITIAL_CONTEXT_FACTORY, "org.wildfly.naming.client.WildFlyInitialContextFactory");
props.put(Context.PROVIDER_URL, "remote+http://localhost:8080");
props.put("wildfly.naming.client.ejb.context", true);
InitialContext ctx = new InitialContext(props);
AnimalServiceRemote animal= (AnimalServiceRemote) ctx.lookup("ejb:/EJBServer/AnimalService!com.ejb.remote.AnimalServiceRemote");
System.out.println("Lookup completeted..." );
System.out.println(animal.findByName("Cat")); // => It's working normally
AnimalEntity ani= new AnimalEntity();
ani=animal.Request("Fish"); // => error occurs here
ctx.close();
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
with return type as String, Int, Double: it is working normally
with return type List<EntityClass>, EntityClass: it throws an error, I still don't understand why.
错误信息:
16:13:28,617 WARN [org.jboss.ejb.client.remoting] (default task-1) EJBCLIENT000519: Exception occurred when writing EJB response to invocation 61367 over channel Channel ID 79beb44f (inbound) of Remoting connection 38289bc4 to 127.0.0.1/127.0.0.1:58770 of endpoint "desktop-sanolu2" <1d50c57e>: java.io.NotSerializableException: com.ejb.entity.AnimalEntity
at org.jboss.marshalling.river@2.0.12.Final//org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:274)
at org.jboss.marshalling@2.0.12.Final//org.jboss.marshalling.AbstractObjectOutput.writeObject(AbstractObjectOutput.java:58)
at org.jboss.marshalling@2.0.12.Final//org.jboss.marshalling.AbstractMarshaller.writeObject(AbstractMarshaller.java:111)
at org.jboss.ejb-client@4.0.44.Final//org.jboss.ejb.protocol.remote.EJBServerChannel$RemotingInvocationRequest.writeInvocationResult(EJBServerChannel.java:986)
at org.jboss.as.ejb3@26.1.0.Final//org.jboss.as.ejb3.remote.AssociationImpl.lambda$receiveInvocationRequest[=17=](AssociationImpl.java:291)
at org.jboss.as.ejb3@26.1.0.Final//org.jboss.as.ejb3.remote.AssociationImpl.execute(AssociationImpl.java:344)
at org.jboss.as.ejb3@26.1.0.Final//org.jboss.as.ejb3.remote.AssociationImpl.receiveInvocationRequest(AssociationImpl.java:297)
at org.jboss.ejb-client@4.0.44.Final//org.jboss.ejb.protocol.remote.EJBServerChannel$ReceiverImpl.handleInvocationRequest(EJBServerChannel.java:473)
at org.jboss.ejb-client@4.0.44.Final//org.jboss.ejb.protocol.remote.EJBServerChannel$ReceiverImpl.handleMessage(EJBServerChannel.java:208)
at org.jboss.remoting@5.0.24.Final//org.jboss.remoting3.remote.RemoteConnectionChannel.lambda$handleMessageData(RemoteConnectionChannel.java:432)
at org.jboss.remoting@5.0.24.Final//org.jboss.remoting3.EndpointImpl$TrackingExecutor.lambda$execute[=17=](EndpointImpl.java:991)
at org.jboss.threads@2.4.0.Final//org.jboss.threads.ContextClassLoaderSavingRunnable.run(ContextClassLoaderSavingRunnable.java:35)
at org.jboss.threads@2.4.0.Final//org.jboss.threads.EnhancedQueueExecutor.safeRun(EnhancedQueueExecutor.java:1990)
at org.jboss.threads@2.4.0.Final//org.jboss.threads.EnhancedQueueExecutor$ThreadBody.doRunTask(EnhancedQueueExecutor.java:1486)
at org.jboss.threads@2.4.0.Final//org.jboss.threads.EnhancedQueueExecutor$ThreadBody.run(EnhancedQueueExecutor.java:1377)
at org.jboss.xnio@3.8.6.Final//org.xnio.XnioWorker$WorkerThreadFactory.run(XnioWorker.java:1280)
at java.base/java.lang.Thread.run(Thread.java:833)
Caused by: an exception which occurred:
in object com.ejb.entity.AnimalEntity@45251eee
请帮帮我。非常感谢
java.io.NotSerializableException: com.ejb.entity.AnimalEntity
说了所有你必须做的,即你需要实现 Serializable 接口:
public class AnimalEntity implements Serializable
我面临的问题是客户端无法使用 Class 实体(ETC 列表、实体)从服务器读取响应。同样对于 String、int、Double 类型的对象,它仍然可以正常工作。
EJBServer 项目
Class 实体:AnimalEntity.java
public class 动物实体 {
private String animalName; private String animalCountry; private String animaltype; private double animalWeigth; public AnimalEntity(String animalName, String animalCountry, String animaltype, double animalWeigth) { super(); this.animalName = animalName; this.animalCountry = animalCountry; this.animaltype = animaltype; this.animalWeigth = animalWeigth; } public String getAnimalName() { return animalName; } public void setAnimalName(String animalName) { this.animalName = animalName; } ..............
}
接口:AnimalServiceRemote.java.
import com.ejb.entity.AnimalEntity;
public interface AnimalServiceRemote {
public String findByName(String strName);
public AnimalEntity Request(String strName);
}
- 豆子Class:AnimalService.java
import jakarta.ejb.LocalBean; import jakarta.ejb.Remote; import jakarta.ejb.Stateless;
@Stateless
@LocalBean
@Remote(AnimalServiceRemote.class)
public class AnimalService implements AnimalServiceRemote {
@Override
public String findByName(String strName) {
List<AnimalEntity> lst= new ArrayList<AnimalEntity>();
AnimalEntity ani= new AnimalEntity("Dog", "Japan", "Type1", 30);
AnimalEntity ani1= new AnimalEntity("Cat", "USA", "Type2", 150);
AnimalEntity ani2= new AnimalEntity("Fish", "Ger", "Type3", 3);
lst.add(ani);
lst.add(ani1);
lst.add(ani2);
for (int i = 0; i < lst.size(); i++) {
if (lst.get(i).getAnimalName().equals(strName)){
return "Name: "+ lst.get(i).getAnimalName() +" - Country: "+ lst.get(i).getAnimalCountry() +" - Weigth: "+lst.get(i).getAnimalWeigth();
}
}
return null;
}
@Override
public AnimalEntity Request(String strName) {
List<AnimalEntity> lst= new ArrayList<AnimalEntity>();
AnimalEntity ani= new AnimalEntity("Dog", "Japan", "Type1", 30);
AnimalEntity ani1= new AnimalEntity("Cat", "USA", "Type2", 150);
AnimalEntity ani2= new AnimalEntity("Fish", "Ger", "Type3", 3);
lst.add(ani);
lst.add(ani1);
lst.add(ani2);
AnimalEntity animal= new AnimalEntity();
for (int i = 0; i < lst.size(); i++) {
if (lst.get(i).getAnimalName().equals(strName)){
animal=lst.get(i);
return animal;
}
}
return null;
}
}
AppClient
- 主要Class:Myclient.java
import javax.naming.Context; import javax.naming.InitialContext; import com.ejb.entity.AnimalEntity; import com.ejb.remote.AnimalServiceRemote;
public class Myclient {
public static void main(String[] args) {
try {
System.out.println("Proccessing...!" );
// Setting Config InitContext
Properties props = new Properties();
props.put(Context.INITIAL_CONTEXT_FACTORY, "org.wildfly.naming.client.WildFlyInitialContextFactory");
props.put(Context.PROVIDER_URL, "remote+http://localhost:8080");
props.put("wildfly.naming.client.ejb.context", true);
InitialContext ctx = new InitialContext(props);
AnimalServiceRemote animal= (AnimalServiceRemote) ctx.lookup("ejb:/EJBServer/AnimalService!com.ejb.remote.AnimalServiceRemote");
System.out.println("Lookup completeted..." );
System.out.println(animal.findByName("Cat")); // => It's working normally
AnimalEntity ani= new AnimalEntity();
ani=animal.Request("Fish"); // => error occurs here
ctx.close();
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
with return type as String, Int, Double: it is working normally
with return type List<EntityClass>, EntityClass: it throws an error, I still don't understand why.
错误信息:
16:13:28,617 WARN [org.jboss.ejb.client.remoting] (default task-1) EJBCLIENT000519: Exception occurred when writing EJB response to invocation 61367 over channel Channel ID 79beb44f (inbound) of Remoting connection 38289bc4 to 127.0.0.1/127.0.0.1:58770 of endpoint "desktop-sanolu2" <1d50c57e>: java.io.NotSerializableException: com.ejb.entity.AnimalEntity
at org.jboss.marshalling.river@2.0.12.Final//org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:274)
at org.jboss.marshalling@2.0.12.Final//org.jboss.marshalling.AbstractObjectOutput.writeObject(AbstractObjectOutput.java:58)
at org.jboss.marshalling@2.0.12.Final//org.jboss.marshalling.AbstractMarshaller.writeObject(AbstractMarshaller.java:111)
at org.jboss.ejb-client@4.0.44.Final//org.jboss.ejb.protocol.remote.EJBServerChannel$RemotingInvocationRequest.writeInvocationResult(EJBServerChannel.java:986)
at org.jboss.as.ejb3@26.1.0.Final//org.jboss.as.ejb3.remote.AssociationImpl.lambda$receiveInvocationRequest[=17=](AssociationImpl.java:291)
at org.jboss.as.ejb3@26.1.0.Final//org.jboss.as.ejb3.remote.AssociationImpl.execute(AssociationImpl.java:344)
at org.jboss.as.ejb3@26.1.0.Final//org.jboss.as.ejb3.remote.AssociationImpl.receiveInvocationRequest(AssociationImpl.java:297)
at org.jboss.ejb-client@4.0.44.Final//org.jboss.ejb.protocol.remote.EJBServerChannel$ReceiverImpl.handleInvocationRequest(EJBServerChannel.java:473)
at org.jboss.ejb-client@4.0.44.Final//org.jboss.ejb.protocol.remote.EJBServerChannel$ReceiverImpl.handleMessage(EJBServerChannel.java:208)
at org.jboss.remoting@5.0.24.Final//org.jboss.remoting3.remote.RemoteConnectionChannel.lambda$handleMessageData(RemoteConnectionChannel.java:432)
at org.jboss.remoting@5.0.24.Final//org.jboss.remoting3.EndpointImpl$TrackingExecutor.lambda$execute[=17=](EndpointImpl.java:991)
at org.jboss.threads@2.4.0.Final//org.jboss.threads.ContextClassLoaderSavingRunnable.run(ContextClassLoaderSavingRunnable.java:35)
at org.jboss.threads@2.4.0.Final//org.jboss.threads.EnhancedQueueExecutor.safeRun(EnhancedQueueExecutor.java:1990)
at org.jboss.threads@2.4.0.Final//org.jboss.threads.EnhancedQueueExecutor$ThreadBody.doRunTask(EnhancedQueueExecutor.java:1486)
at org.jboss.threads@2.4.0.Final//org.jboss.threads.EnhancedQueueExecutor$ThreadBody.run(EnhancedQueueExecutor.java:1377)
at org.jboss.xnio@3.8.6.Final//org.xnio.XnioWorker$WorkerThreadFactory.run(XnioWorker.java:1280)
at java.base/java.lang.Thread.run(Thread.java:833)
Caused by: an exception which occurred:
in object com.ejb.entity.AnimalEntity@45251eee
请帮帮我。非常感谢
java.io.NotSerializableException: com.ejb.entity.AnimalEntity
说了所有你必须做的,即你需要实现 Serializable 接口:
public class AnimalEntity implements Serializable