iT邦幫忙

0

spring boot @RestController 使用jpa啟動spring 出錯

請教各位
在使用@Restcontroller function 下有使用jpa UserRepository.findAll()在啟動spring 時會出現錯誤,網上看了幾個例子大致也是這用法,請問原因為何還是哪邊有使用有誤在麻煩各位了謝謝

像這個樣子

@CrossOrigin
@RestController
public class UserController {

	@Autowired
	private UserRepository UserRepository;

	private List<Users> user = findAll();

	@RequestMapping(value = "/all", method = RequestMethod.POST)
		private List<Users> findAll() {
		List<Users> tempEmployees = new ArrayList<Users>();
		tempEmployees = UserRepository.findAll();
		return tempEmployees;

	}

錯誤訊息

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userController' defined in file
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1303) ~[spring-beans-5.1.6.RELEASE.jar:5.1.6.RELEASE]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1197) ~[spring-beans-5.1.6.RELEASE.jar:5.1.6.RELEASE]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555) ~[spring-beans-5.1.6.RELEASE.jar:5.1.6.RELEASE]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:515) ~[spring-beans-5.1.6.RELEASE.jar:5.1.6.RELEASE]
	at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320) ~[spring-beans-5.1.6.RELEASE.jar:5.1.6.RELEASE]
.........
Caused by: java.lang.NullPointerException: null
	at com.lungsheng.system.controller.UserController.findAll(UserController.java:29) ~[classes/:na]
	at com.lungsheng.system.controller.UserController.<init>(UserController.java:24) ~[classes/:na]
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_181]
	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:1.8.0_181]
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.8.0_181]
	at java.lang.reflect.Constructor.newInstance(Constructor.java:423) ~[na:1.8.0_181]
	at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:172) ~[spring-beans-5.1.6.RELEASE.jar:5.1.6.RELEASE]
	... 19 common frames omitted

1 個回答

0
Rex Chien
iT邦新手 5 級 ‧ 2019-12-01 22:08:15
最佳解答

猜測是你的 UserRepository 沒有加 @Repository Annotation

超超 iT邦新手 5 級 ‧ 2019-12-02 18:40:08 檢舉

@Repository Annotation 有加上呢
@Autowired時我的UserRepository 好像沒順利取得值,值是null,問題不知道出在哪

@Repository
public interface UserRepository extends JpaRepository<Users, Long>{
	Optional<Users> findByAccount(String account);
	 @Override
	List<Users> findAll();
	Users findByName(String name);
//	Optional<Users> findByUsername(String username);
}
Rex Chien iT邦新手 5 級 ‧ 2019-12-02 22:28:59 檢舉

private List<Users> user = findAll(); 這行拿掉試試看,因為這行的執行時間應該是在 UserRepository 被注入之前,所以呼叫 findAll() 方法時 UserRepository 還是 null。

超超 iT邦新手 5 級 ‧ 2019-12-04 19:12:35 檢舉

確實是這樣執行時間上的問題,謝謝您

我要發表回答

立即登入回答