首页 新闻 会员 周边

测试Spring-Boot官网程序踩到坑了

0
悬赏园豆:30 [已解决问题] 解决于 2018-10-31 21:11

下午【抄写】Spring-Boot官网的程序,结果,遇到问题了,查询了很久,一直没有找到解决方法。
请园友帮帮我呗。

官网程序:
Accessing JPA Data with REST
https://spring.io/guides/gs/accessing-data-rest

从其Github下载到本地,导入Eclipse中,运行发生异常:
https://github.com/spring-guides/gs-accessing-data-rest
注意,导入其中的complete文件夹下的内容。

错误提示信息如下:
2018-10-30 20:34:14.382 INFO 7296 --- [ main] o.apache.catalina.core.StandardService : Stopping service [Tomcat]
2018-10-30 20:34:14.401 INFO 7296 --- [ main] ConditionEvaluationReportLoggingListener :

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2018-10-30 20:34:14.475 ERROR 7296 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter :


APPLICATION FAILED TO START
***************************

Description:

Parameter 0 of constructor in org.springframework.data.rest.webmvc.ProfileController required a bean named 'entityManagerFactory' that could not be found.

Action:

Consider defining a bean named 'entityManagerFactory' in your configuration.

更多错误信息(上面的储物信息之前):
2018-10-30 20:34:14.379 WARN 7296 --- [ main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'profileController' defined in URL [jar:file:/D:/ws/apache-maven-3.5.4/repository/org/springframework/data/spring-data-rest-webmvc/3.0.10.RELEASE/spring-data-rest-webmvc-3.0.10.RELEASE.jar!/org/springframework/data/rest/webmvc/ProfileController.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'repositoryRestConfiguration' defined in class path resource [org/springframework/data/rest/webmvc/config/RepositoryRestMvcConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.data.rest.core.config.RepositoryRestConfiguration]: Factory method 'repositoryRestConfiguration' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'repositories' defined in class path resource [org/springframework/data/rest/webmvc/config/RepositoryRestMvcConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.data.repository.support.Repositories]: Factory method 'repositories' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'personRepository': Cannot create inner bean '(inner bean)#38a1c423' of type [org.springframework.orm.jpa.SharedEntityManagerCreator] while setting bean property 'entityManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#38a1c423': Cannot resolve reference to bean 'entityManagerFactory' while setting constructor argument; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'entityManagerFactory' available

要怎么解决呢?
有的说是JPA和Hibernate冲突,删掉仓库里的Hibernate就可以了,做了,不可以。
还有说在Application里面添加@ComponentScan(...)注解,可是,也不行。

听说Spring-Boot的坑比较多,没想到第一天就遇到一个这么大的!

说明,我对Spring及Spring Boot属于初学。

HELP!

快乐的凡人721的主页 快乐的凡人721 | 老鸟四级 | 园豆:3918
提问于:2018-10-30 20:37
< >
分享
最佳答案
0

刚刚百度了一下,搜索:spring-boot 没有加载我的Repository接口

打开下面的链接:√
https://stackoverflow.com/questions/40384056/consider-defining-a-bean-of-type-package-in-your-configuration-spring-boot

使用其中的 最佳解法 修改自己的Application中的代码:添加三条注解
@ComponentScan({"hello"})
@EntityScan("hello")
@EnableJpaRepositories("hello")

截图:

前面解决问题时 在 @SpringBootApplication 后面添加的 (exclude = DataSourceAutoConfiguration.class) 也 删除 了。

再次运行可得:和Spring官网的示例差不多了,只是,多了profile的链接

一瞬间,整个人轻松了好多好多啊!
对了,还不知道原因,哪位高手还请赐教一二啊?

快乐的凡人721 | 老鸟四级 |园豆:3918 | 2018-10-31 17:02

你只要百度你添加得注解得含义就好了

番茄先生 | 园豆:911 (小虾三级) | 2018-10-31 17:20

@番茄vs西红柿:
@SpringBootApplication is a convenience annotation that adds all of the following:...

这个注解里面包含了几乎所有需要的,而且 说是还会自动扫描Application所在的package,所以,官文中没有添加,结果,运行出错了。

快乐的凡人721 | 园豆:3918 (老鸟四级) | 2018-10-31 17:28

在@SpringBootApplication后添加(scanBasePackages = {"hello"})也可以。
@SpringBootApplication(scanBasePackages = {"hello"})

即链接中 Ameen.M 给出的解决方案,看起来更easy。

快乐的凡人721 | 园豆:3918 (老鸟四级) | 2018-10-31 17:50

@行者Ben: 对,你只要把Application这个项目得入口文件,放到你总包下面就会全部扫面,也不需要添加hello了

番茄先生 | 园豆:911 (小虾三级) | 2018-11-01 09:44

@番茄vs西红柿:
就一个包,hello,所有文件都是放在这个里面的,
按照案例的解释是 会自动扫描的,但结果却没有,

快乐的凡人721 | 园豆:3918 (老鸟四级) | 2018-11-01 15:18
其他回答(3)
0

entityManagerFactory
注入失败,看一下entityManagerFactory那边的代码

收获园豆:15
ShaneJim | 园豆:206 (菜鸟二级) | 2018-10-30 21:46

可是,工程里面根本就没有 entityManagerFactory 啊,
代码也很简单,就三个:Application.java Person.java PersonRepository.java,从GitHub直接下载,以Maven工程导入的,

支持(0) 反对(0) 快乐的凡人721 | 园豆:3918 (老鸟四级) | 2018-10-30 22:21

@行者Ben: 好吧,没用过jpa额,Maven里加spring-boot-starter-data-jpa的引用了吗

支持(0) 反对(0) ShaneJim | 园豆:206 (菜鸟二级) | 2018-10-30 22:46

@瓦尔登湖的一滴: 加了,来自官网工程,pom.xml文件里面有的,
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
Maven本地仓库也有,

支持(0) 反对(0) 快乐的凡人721 | 园豆:3918 (老鸟四级) | 2018-10-30 23:04

@瓦尔登湖的一滴:
很惨的就是,网上没有找到这个问题的 解法,
而且试了试官网的其它几个实例,也都存在问题,

看来,我明天得补补Spring-Boot的基础了,或许会更有效地解决问题,

谢谢了!

支持(0) 反对(0) 快乐的凡人721 | 园豆:3918 (老鸟四级) | 2018-10-30 23:06

@瓦尔登湖的一滴:
有一些进展了,可是,没有得到预期的结果,
详情见下方的回复。

支持(0) 反对(0) 快乐的凡人721 | 园豆:3918 (老鸟四级) | 2018-10-31 12:36

问题已解决。

支持(0) 反对(0) 快乐的凡人721 | 园豆:3918 (老鸟四级) | 2018-10-31 17:03
0

这不是 提示了 entityManagerFactory 需要这么一个类么。。这个不是自己写的,那就是第三方jar包里面的,看看你引用的jar包对不对。或者是你引用的jar包中,有重复版本,导致冲突

收获园豆:5
队长给我球。 | 园豆:324 (菜鸟二级) | 2018-10-31 10:56

只是,我不会写,初学阶段,很新那种,

刚刚上午又查找了一遍,发现了一种解决方案(问题未完全解决):
删掉Maven本地仓库的Hibernate文件夹——昨天我只是删除文件夹里面的内容进行测试,在maven->update project,测试出现下面的错误:
***************************
APPLICATION FAILED TO START
***************************

Description:

Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.

Reason: Failed to determine a suitable driver class

Action:

Consider the following:
If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.
If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active).
--
表明之前的问题解决了,正如那篇博文所言,和Hibernate存在冲突,现在,冲突解决了。
参考链接:
https://blog.csdn.net/u010001058/article/details/83480095

上面的问题继续查找,发现下面的链接:
https://blog.csdn.net/wolfking0608/article/details/82886062
修改后:添加了括号中的内容
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)

此时,Application可以运行起来了。

但是,访问不了,@RepositoryRestResource(collectionResourceRel = "people", path = "people")中定义的/people链接:
user@DESKTOP-F6FLB0T:~$ curl http://127.0.0.1:8080
{
"_links" : {
"profile" : {
"href" : "http://127.0.0.1:8080/profile"
}
}
}

支持(0) 反对(0) 快乐的凡人721 | 园豆:3918 (老鸟四级) | 2018-10-31 12:09

已解决。

支持(0) 反对(0) 快乐的凡人721 | 园豆:3918 (老鸟四级) | 2018-10-31 17:03

@行者Ben: 你的controller 呢??

支持(0) 反对(0) 队长给我球。 | 园豆:324 (菜鸟二级) | 2018-10-31 18:14

@队长给我球。:
这个Spring-Boot示例里面不需要写的,之前抄写的一个实例也是如此,

支持(0) 反对(0) 快乐的凡人721 | 园豆:3918 (老鸟四级) | 2018-10-31 18:23
0

把你maven仓库中得/D:/ws/apache-maven-3.5.4/repository/org/springframework/data/spring-data-rest-webmvc删掉,重新maven下载

收获园豆:10
番茄先生 | 园豆:911 (小虾三级) | 2018-10-31 11:01

试了下,删除后update project,再执行程序,结果出现下面的错误:
***************************
APPLICATION FAILED TO START
***************************

Description:

Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.

Reason: Failed to determine a suitable driver class

Action:

Consider the following:
If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.
If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active).
--

按照回复前面园友的方式进行了修改,结果出现下面的问题:
$ curl http://127.0.0.1:8080/people
{"timestamp":"2018-10-31T04:14:31.380+0000","status":404,"error":"Not Found","message":"No message available","path":"/people"}

这个链接还是不可以访问。查询了一个多钟了,还是没搞定啊。

支持(0) 反对(0) 快乐的凡人721 | 园豆:3918 (老鸟四级) | 2018-10-31 12:15

@行者Ben: 这是运行起来了吧?这是404了,看下控制器里/people路径那,这个好解决

支持(0) 反对(0) ShaneJim | 园豆:206 (菜鸟二级) | 2018-10-31 12:39

@瓦尔登湖的一滴:
项目里面没有自己写控制器的,Spring-Boot的这个实例的官网说相关控制器会自动建立:
Spring Data REST builds on top of Spring MVC. It creates a collection of Spring MVC controllers, JSON converters, and other beans needed to provide a RESTful front end.

At runtime, Spring Data REST will create an implementation of this interface automatically. Then it will use the @RepositoryRestResource annotation to direct Spring MVC to create RESTful endpoints at /people.

@RepositoryRestResource is not required for a repository to be exported. It is only used to change the export details, such as using /people instead of the default value of /persons.

其实,对我来说,也不好解决啊,下午继续努力,

支持(0) 反对(0) 快乐的凡人721 | 园豆:3918 (老鸟四级) | 2018-10-31 12:56

@行者Ben: https://www.jianshu.com/p/3423fa97d185 你参考一下这个maven配置和具体启动配置

支持(0) 反对(0) 番茄先生 | 园豆:911 (小虾三级) | 2018-10-31 13:40

@番茄vs西红柿:
这个文章之前看过,就是介绍我现在遇到问题的这个官网 project,
刚刚又仔细对比了一遍,里面的代码和我的 project 的是一样的,都是官网的,
TA加了一些注释,
幸运的是,TA的程序跑通了,得到了结果,而我的却没有,

支持(0) 反对(0) 快乐的凡人721 | 园豆:3918 (老鸟四级) | 2018-10-31 14:10

@番茄vs西红柿:
看了下,这篇文章发布于2016.12.13,也就是说,这个实例是那一年的,或许,太旧了?
也可能,我是用Eclipse,而TA用的不是Eclipse?而是像官网一样用的命令行 编译 可执行 jar?我试试,

支持(0) 反对(0) 快乐的凡人721 | 园豆:3918 (老鸟四级) | 2018-10-31 14:13

@行者Ben: 404这个错误代表你的 /people 这个restful服务没创建成功啊,确认一下jar版本,jdk版本(虽然不知道是不是jdk得原因),或者你直接访问你的服务端口看看tomcat服务器状态

支持(0) 反对(0) 番茄先生 | 园豆:911 (小虾三级) | 2018-10-31 14:25

@番茄vs西红柿:
是的,所以不能访问 /people,
访问服务端口查看tomcat服务器的状态?我这个是Spring-Boot内建的服务器上运行的,没有运行在Tomcat上,

刚刚使用官网的命令行进行了测试,和Eclipse中测试结果一样,
查看启动信息发现,有很多 **HandlerMapping:
RequestMappingHandlerMapping、SimpleUrlHandlerMapping、RepositoryRestHandlerMapping

启动时还存在下面这句:
hello.Application : No active profile set, falling back to default profiles: default
还有:
o.s.d.r.w.BasePathAwareHandlerMapping : Mapped "{[/profile],methods=[OPTIONS]}" onto public org.springframework.http.HttpEntity<?> org.springframework.data.rest.webmvc.ProfileController.profileOptions()
o.s.d.r.w.BasePathAwareHandlerMapping : Mapped "{[/profile],methods=[GET]}" onto org.springframework.http.HttpEntity<org.springframework.hateoas.ResourceSupport> org.springframework.data.rest.webmvc.ProfileController.listAllFormsOfMetadata()

表明 /profile 是映射成功了,也能访问。

但自定义的 PersonRepository 却没有成功运行,

支持(0) 反对(0) 快乐的凡人721 | 园豆:3918 (老鸟四级) | 2018-10-31 14:48

@行者Ben: Spring-Boot内部集成tomcat服务器,那你这个问题我就不确定了

支持(0) 反对(0) 番茄先生 | 园豆:911 (小虾三级) | 2018-10-31 14:50

@番茄vs西红柿:
其实也是Tomcat:
o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
o.apache.catalina.core.StandardService : Starting service [Tomcat]
org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.34
o.a.catalina.core.AprLifecycleListener : The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path:

关于Mapped信息,也有映射到Repository的:
Mapped "{[/{repository}/{id}],methods=[PUT],produces=[application/hal+json || application/json]}"
Mapped "{[/{repository}],methods=[HEAD],produces=[application/hal+json || application/json]}"
Mapped "{[/{repository}/{id}/{property}/{propertyId}],methods=[GET],produces=[application/hal+json || application/json]}"
Mapped "{[/{repository}/search/{search}],methods=[GET],produces=[application/x-spring-data-compact+json]}"
Mapped "{[/{repository}/search],methods=[OPTIONS],produces=[application/hal+json || application/json]}"
Mapped "{[/{repository}/search/{search}],methods=[OPTIONS],produces=[application/hal+json || application/json]}"
Mapped "{[/profile/{repository}],methods=[GET],produces=[application/schema+json]}"

从上面的Mapped信息来看,是可以访问 /people 信息的,可却没有。

先这样,我再多看点文档吧,
对了,我的主机是Windows 10,

支持(0) 反对(0) 快乐的凡人721 | 园豆:3918 (老鸟四级) | 2018-10-31 14:58

已解决。

支持(0) 反对(0) 快乐的凡人721 | 园豆:3918 (老鸟四级) | 2018-10-31 17:03
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册