我面临的问题是,客户端无法从带有类实体(ETC List,Entity)的服务器读取响应.同样,对于字符串、int、Double类型的对象,它仍然正常工作.

Project EJBServer

  1. 类实体:动物性.JAVA

    公共动物性{

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

    }

  2. 界面:AnimalServiceRemote.Java .

import com.ejb.entity.AnimalEntity;

 public interface AnimalServiceRemote {

    public String findByName(String strName);
    public AnimalEntity Request(String strName); 
}
  1. 豆类:动物服务.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

  1. 主类: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.

Error Message:

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$1.writeInvocationResult(EJBServerChannel.java:986)
    at org.jboss.as.ejb3@26.1.0.Final//org.jboss.as.ejb3.remote.AssociationImpl.lambda$receiveInvocationRequest$0(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$3(RemoteConnectionChannel.java:432)
    at org.jboss.remoting@5.0.24.Final//org.jboss.remoting3.EndpointImpl$TrackingExecutor.lambda$execute$0(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$1$1.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说明了所有您必须做的事情,即您需要实现可序列化接口:

public class AnimalEntity implements Serializable

Java相关问答推荐

为什么Java中的两个日期有差异?

为什么我要创建一个单独的互斥体/锁对象?

如何才能使我的程序不会要求两次输入?

如何在Java中从XML中获取特定的 node ,然后将其删除?

当返回Mono<;Something>;时,不会调用Mono<;void>;.flatMap

Bean定义不是从Spring ApplationConext.xml文件加载的

使用UTC时区将startDatetime转换为本地时间

基于接口的投影、原生查询和枚举

Java Mooc.fi Part 12_01.Hideout -返回和删除方法

匹配一组字符或另一组字符

为什么我的回收视图会显示重复的列表?

使用htmlunit和java单击按钮

Java泛型类方法的静态返回类型是否被类型擦除?

如何在透视表中添加对计数列的筛选?

为什么在下面的Java泛型方法中没有类型限制?

使用for循环时出现堆栈溢出错误,但如果使用if块执行相同的操作,则不会产生错误

从12小时开始的日期模式

JXBrowser是否支持加载Chrome扩展?

如何在Selenium上继续使用最新的WebDriver版本

由于可为null,无法在kotlin中实现java接口