背景:公司須要作一個小程序,從把服務器數據庫裏面的數據拷貝到本地java
技術:spring-boot, XXL-RPC, JPAspring
問題 : 客戶端查詢數據,調用RPC服務,一直報錯 StackOverflowError:null,數據庫
問題重現:小程序
數據表test服務器
CREATE TABLE test ( "rid" int(11) NOT NULL AUTO_INCREMENT, "name" varchar(45) DEFAULT NULL, "age" int(11) DEFAULT NULL, "create_time" datetime DEFAULT NULL, PRIMARY KEY ("rid") ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Test實體類ide
import java.io.Serializable; import java.time.LocalDateTime; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Table; import lombok.Getter; import lombok.Setter; @Getter @Setter @Entity @Table(name = "test") public class TestEntity implements Serializable{ /** * */ private static final long serialVersionUID = -7189192698006290220L; @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "rid") private Integer id; private String name; private Integer age; private LocalDateTime createTime; }
Repository ,使用的是JPA,具體關於JPARepository的知識,本身補習吧spring-boot
import org.springframework.data.jpa.repository.JpaRepository; public interface TestRepository extends JpaRepository<TestEntity, Integer>{ }
Service服務,直接與數據庫交互的測試
public interface TestService { TestEntity save(TestEntity entity); }
@Service public class TestServiceImpl implements TestService{ @Resource private TestRepository testRepository; @Override public TestEntity save(TestEntity entity) { return testRepository.save(entity); } }
服務端開放的RPC服務spa
public interface WqqTestRpcService { TestEntity save(TestEntity entity); }
import javax.annotation.Resource; import org.springframework.stereotype.Service; import com.wqq.test.TestEntity; import com.wqq.test.WqqTestRpcService; import com.wqq.test.TestService; import com.xxl.rpc.remoting.provider.annotation.XxlRpcService; @XxlRpcService @Service public class WqqTestRpcServiceImpl implements WqqTestRpcService{ @Resource private TestService testService; @Override public TestEntity save(TestEntity entity) { return testService.save(entity); } }
客戶端使用code
@XxlRpcReference private WqqTestRpcService wqqTestRpcService; public void save() { TestEntity entity = new TestEntity(); entity.setAge(23); entity.setName("我是誰"); entity.setId(3); entity.setCreateTime(LocalDateTime.now()); wqqTestRpcService.save(entity); }
習慣寫完以後測試
測試TestService, WqqTestRpcService
@ContextConfiguration(classes = TestServerApplication.class) @RunWith(SpringRunner.class) @SpringBootTest //@Transactional //@Rollback public class Test{ @Resource private TestService testService; @Resource private WqqTestRpcService wqqTestRpcService; @Test public void test1() { TestEntity entity = new TestEntity(); entity.setAge(23); entity.setName("測試1"); entity.setId(1); entity.setCreateTime(LocalDateTime.now()); wqqTestRpcService.save(entity); } @Test public void test2() { TestEntity entity = new TestEntity(); entity.setAge(23); entity.setName("測試2"); entity.setId(2); entity.setCreateTime(LocalDateTime.now()); testService.save(entity); } }
結果:數據庫插入兩條數據,說明方法是沒有問題的
正式運行。開始報錯 Caused by: java.lang.StackOverflowError: null,
百思不得其解,查了網上不少基本都是說死循環或者是無線遞歸,但是我只是調用一個保存的方法,哪裏來的死循環還有遞歸,還有說改變配置,可是我測試方法是行得通的,直覺告訴我不會說是改變配置(真的直覺,可是可能別人是有這樣解決的,反正我沒有試),後來又進行了不少次測試,結果徹底想不到是參數類型的問題,把TestEntity的參數類型與LocalDateTime改爲Date就能夠,具體爲啥這樣,還沒結論,只是知道這麼再也不報錯,可是是否是真的最好的結局辦法,依然不知, 但願有大神能夠解惑吧