设计了三个dubbo服务java
testDubboxNoSerialCallback 返回值没有序列化spring
testDubboxSerialCallback 返回值序列化app
testDubboxNoSerialParam 参数没有序列化ide
dubbo服务接口测试
package com.zhu.example.dubbox; import com.zhu.example.entity.NoSerialEntity; import com.zhu.example.entity.SerialEntity; public interface DubboxProviderNoSerial { public NoSerialEntity testDubboxNoSerialCallback(); public SerialEntity testDubboxSerialCallback(); public SerialEntity testDubboxNoSerialParam(NoSerialEntity entity); }
dubbo服务实现this
package com.zhu.example.dubbox; import com.zhu.example.entity.NoSerialEntity; import com.zhu.example.entity.SerialEntity; public class DubboxProviderNoSerialImpl implements DubboxProviderNoSerial { /* (non-Javadoc) * @see com.zhu.example.dubbox.DubboxProviderNoSerial#testDubboxNoSerialParam() */ public NoSerialEntity testDubboxNoSerialCallback() { NoSerialEntity test=new NoSerialEntity(); test.setName("test"); return test; } /* (non-Javadoc) * @see com.zhu.example.dubbox.DubboxProviderNoSerial#testDubboxSerialCallback() */ public SerialEntity testDubboxSerialCallback() { SerialEntity test=new SerialEntity(); test.setName("test"); return test; } /* (non-Javadoc) * @see com.zhu.example.dubbox.DubboxProviderNoSerial#testDubboxNoSerialParam(com.zhu.example.entity.NoSerialEntity) */ public SerialEntity testDubboxNoSerialParam(NoSerialEntity entity) { SerialEntity test=new SerialEntity(); test.setName(entity.getName()); return test; } }
未序列化的实体类NoSerialEntityspa
/** * */ package com.zhu.example.entity; /** * @author zhukai * */ public class NoSerialEntity { String name; public String getName() { return name; } public void setName(String name) { this.name = name; } }
序列化的实体类SerialEntity设计
/** * */ package com.zhu.example.entity; import java.io.Serializable; /** * @author zhukai * */ public class SerialEntity implements Serializable{ /** * */ private static final long serialVersionUID = 4004132163444150913L; String name; public String getName() { return name; } public void setName(String name) { this.name = name; } }
consumer 调用dubbo服务code
package com.zhu.example; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.util.Assert; import com.zhu.example.dubbox.DubboxProviderNoSerial; import com.zhu.example.entity.NoSerialEntity; import com.zhu.example.entity.SerialEntity; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:applicationContext-dubbo-consumer.xml") public class TestDubboxConsumer { @Autowired DubboxProviderNoSerial dubboxNoSerial; @Test public void NoSerialCallback() throws InterruptedException { NoSerialEntity result = dubboxNoSerial.testDubboxNoSerialCallback(); System.out.println(result.getName()); Assert.notNull(result); } @Test public void SerialCallback() throws InterruptedException { SerialEntity result = dubboxNoSerial.testDubboxSerialCallback(); System.out.println(result.getName()); Assert.notNull(result); } @Test public void NoSerialParam() throws InterruptedException { NoSerialEntity entity =new NoSerialEntity(); entity.setName("NoSerialParam"); SerialEntity result = dubboxNoSerial.testDubboxNoSerialParam(entity); System.out.println(result.getName()); Assert.notNull(result); } }
返回值参数都序列化的测试方法正常xml
返回值或者参数没有序列化的会抛出异常
java.lang.IllegalStateException: Serialized class com.zhu.example.entity.NoSerialEntity must implement java.io.Serializable