纠正spring boot应用程序的类路径

50few1ms  于 2021-10-10  发布在  Java
关注(0)|答案(2)|浏览(462)

我的spring boot microservice出现了一些问题,我已经成功地将我的microservice连接到运行在google cloud上的sql示例,但是由于某种原因,当我尝试使用一个简单返回字符串“working”的示例端点测试控制器是否正常工作时,我得到了一个404 not found错误,我还尝试了一个示例空白项目,除了启动和运行spring boot所需的项目外,没有任何依赖项,但是我仍然面临相同的问题,即我得到了一个404 not found错误,即使它正在正确编译和运行。我已经检查了我的防火墙,它没有阻止服务。
然而,在浏览堆栈溢出时,我意识到问题在于我的包结构,它没有在我的微服务中找到控制器。
问题是我的demoapplication.java(包含主方法的方法,在一个名为com.example.demo.run的包中,显然这是导致问题的原因,我没有修复它,并将其移动到com.example.demo。
但现在我面临一个新问题,这是我的堆栈跟踪

  1. ***************************
  2. APPLICATION FAILED TO START
  3. ***************************
  4. Description:
  5. An attempt was made to call a method that does not exist. The attempt was made from the following location:
  6. org.springframework.data.repository.core.support.TransactionalRepositoryProxyPostProcessor$RepositoryAnnotationTransactionAttributeSource.<init>(TransactionalRepositoryProxyPostProcessor.java:111)
  7. The following method did not exist:
  8. 'void org.springframework.transaction.annotation.AnnotationTransactionAttributeSource.<init>(boolean)'
  9. The method's class, org.springframework.transaction.annotation.AnnotationTransactionAttributeSource, is available from the following locations:
  10. jar:file:/C:/Users/Engineering/.gradle/caches/modules-2/files-2.1/org.springframework/spring-dao/1.2.9/6f90baf86fc833cac3c677a8f35d3333ed86baea/spring-dao-1.2.9.jar!/org/springframework/transaction/annotation/AnnotationTransactionAttributeSource.class
  11. jar:file:/C:/Users/Engineering/.gradle/caches/modules-2/files-2.1/org.springframework/spring-tx/5.3.9-SNAPSHOT/d8f8af631e6429037c39f756fe2279f50c636844/spring-tx-5.3.9-SNAPSHOT.jar!/org/springframework/transaction/annotation/AnnotationTransactionAttributeSource.class
  12. The class hierarchy was loaded from the following locations:
  13. org.springframework.transaction.annotation.AnnotationTransactionAttributeSource: file:/C:/Users/Engineering/.gradle/caches/modules-2/files-2.1/org.springframework/spring-dao/1.2.9/6f90baf86fc833cac3c677a8f35d3333ed86baea/spring-dao-1.2.9.jar
  14. org.springframework.transaction.interceptor.AbstractFallbackTransactionAttributeSource: file:/C:/Users/Engineering/.gradle/caches/modules-2/files-2.1/org.springframework/spring-dao/1.2.9/6f90baf86fc833cac3c677a8f35d3333ed86baea/spring-dao-1.2.9.jar
  15. Action:
  16. Correct the classpath of your application so that it contains a single, compatible version of org.springframework.transaction.annotation.AnnotationTransactionAttributeSource
  17. Process finished with exit code 1

这是控制器
videocontroller.java

  1. package com.example.demo.Controllers;
  2. import org.springframework.web.bind.annotation.RequestMapping;
  3. import org.springframework.web.bind.annotation.RestController;
  4. @RestController
  5. public class VideoController {
  6. @RequestMapping(path = "/test")
  7. public String index() {
  8. return "Greetings from Spring Boot!";
  9. }
  10. }

运行日志

  1. 2021-07-11 16:53:28.677 INFO 16456 --- [ main] com.example.demo.Run.DemoApplication : Starting DemoApplication using Java 15.0.2 on DESKTOP-OO1339O with PID 16456 (C:\Users\Engineering\IdeaProjects\gcp-demo\build\classes\java\main started by Engineering in C:\Users\Engineering\IdeaProjects\gcp-demo)
  2. 2021-07-11 16:53:28.679 INFO 16456 --- [ main] com.example.demo.Run.DemoApplication : No active profile set, falling back to default profiles: default
  3. 2021-07-11 16:53:29.149 INFO 16456 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
  4. 2021-07-11 16:53:29.161 INFO 16456 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 5 ms. Found 0 JPA repository interfaces.
  5. 2021-07-11 16:53:29.510 INFO 16456 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
  6. 2021-07-11 16:53:29.517 INFO 16456 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
  7. 2021-07-11 16:53:29.517 INFO 16456 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.48]
  8. 2021-07-11 16:53:29.624 INFO 16456 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
  9. 2021-07-11 16:53:29.625 INFO 16456 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 913 ms
  10. 2021-07-11 16:53:29.751 INFO 16456 --- [ main] o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [name: default]
  11. 2021-07-11 16:53:29.788 INFO 16456 --- [ main] org.hibernate.Version : HHH000412: Hibernate ORM core version 5.4.32.Final
  12. 2021-07-11 16:53:29.888 INFO 16456 --- [ main] o.hibernate.annotations.common.Version : HCANN000001: Hibernate Commons Annotations {5.1.2.Final}
  13. 2021-07-11 16:53:29.971 INFO 16456 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
  14. 2021-07-11 16:53:30.025 INFO 16456 --- [ main] c.g.cloud.sql.core.CoreSocketFactory : Connecting to Cloud SQL instance [XXXXXXXXHIDDENFROMSTACKOVERFLOWPOST] via SSL socket.
  15. 2021-07-11 16:53:30.025 INFO 16456 --- [ main] c.g.cloud.sql.core.CoreSocketFactory : First Cloud SQL connection, generating RSA key pair.
  16. 2021-07-11 16:53:31.820 INFO 16456 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed.
  17. 2021-07-11 16:53:31.835 INFO 16456 --- [ main] org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.dialect.MySQL57Dialect
  18. 2021-07-11 16:53:31.924 INFO 16456 --- [onnection adder] c.g.cloud.sql.core.CoreSocketFactory : Connecting to Cloud SQL instance [XXXXXXXXHIDDENFROMSTACKOVERFLOWPOST] via SSL socket.
  19. 2021-07-11 16:53:32.035 INFO 16456 --- [onnection adder] c.g.cloud.sql.core.CoreSocketFactory : Connecting to Cloud SQL instance [XXXXXXXXHIDDENFROMSTACKOVERFLOWPOST] via SSL socket.
  20. 2021-07-11 16:53:32.040 INFO 16456 --- [ main] o.h.e.t.j.p.i.JtaPlatformInitiator : HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
  21. 2021-07-11 16:53:32.049 INFO 16456 --- [ main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
  22. 2021-07-11 16:53:32.099 WARN 16456 --- [ main] JpaBaseConfiguration$JpaWebConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning
  23. 2021-07-11 16:53:32.147 INFO 16456 --- [onnection adder] c.g.cloud.sql.core.CoreSocketFactory : Connecting to Cloud SQL instance [XXXXXXXXHIDDENFROMSTACKOVERFLOWPOST] via SSL socket.
  24. 2021-07-11 16:53:32.245 INFO 16456 --- [onnection adder] c.g.cloud.sql.core.CoreSocketFactory : Connecting to Cloud SQL instance [XXXXXXXXHIDDENFROMSTACKOVERFLOWPOST] via SSL socket.
  25. 2021-07-11 16:53:32.265 INFO 16456 --- [ main] c.g.c.s.core.DefaultCredentialsProvider : Default credentials provider for service account acn-demo-app@involuted-earth-319307.iam.gserviceaccount.com
  26. 2021-07-11 16:53:32.265 INFO 16456 --- [ main] c.g.c.s.core.DefaultCredentialsProvider : Scopes in use by default credentials: [https://www.googleapis.com/auth/pubsub, https://www.googleapis.com/auth/spanner.admin, https://www.googleapis.com/auth/spanner.data, https://www.googleapis.com/auth/datastore, https://www.googleapis.com/auth/sqlservice.admin, https://www.googleapis.com/auth/devstorage.read_only, https://www.googleapis.com/auth/devstorage.read_write, https://www.googleapis.com/auth/cloudruntimeconfig, https://www.googleapis.com/auth/trace.append, https://www.googleapis.com/auth/cloud-platform, https://www.googleapis.com/auth/cloud-vision, https://www.googleapis.com/auth/bigquery, https://www.googleapis.com/auth/monitoring.write]
  27. 2021-07-11 16:53:32.277 INFO 16456 --- [ main] c.g.c.s.a.c.GcpContextAutoConfiguration : The default project ID is XXXXXXXXHIDDENFROMSTACKOVERFLOWPOST
  28. 2021-07-11 16:53:32.353 INFO 16456 --- [onnection adder] c.g.cloud.sql.core.CoreSocketFactory : Connecting to Cloud SQL instance [XXXXXXXXHIDDENFROMSTACKOVERFLOWPOST] via SSL socket.
  29. 2021-07-11 16:53:32.378 INFO 16456 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
  30. 2021-07-11 16:53:32.389 INFO 16456 --- [ main] com.example.demo.Run.DemoApplication : Started DemoApplication in 4.027 seconds (JVM running for 4.567)
  31. 2021-07-11 16:53:32.449 INFO 16456 --- [onnection adder] c.g.cloud.sql.core.CoreSocketFactory : Connecting to Cloud SQL instance [XXXXXXXXHIDDENFROMSTACKOVERFLOWPOST] via SSL socket.
  32. 2021-07-11 16:53:32.554 INFO 16456 --- [onnection adder] c.g.cloud.sql.core.CoreSocketFactory : Connecting to Cloud SQL instance [XXXXXXXXHIDDENFROMSTACKOVERFLOWPOST] via SSL socket.
  33. 2021-07-11 16:53:32.659 INFO 16456 --- [onnection adder] c.g.cloud.sql.core.CoreSocketFactory : Connecting to Cloud SQL instance [involuted-earth-319307:australia-southeast1:cloud-demo] via SSL socket.
  34. 2021-07-11 16:53:32.768 INFO 16456 --- [onnection adder] c.g.cloud.sql.core.CoreSocketFactory : Connecting to Cloud SQL instance [XXXXXXXXHIDDENFROMSTACKOVERFLOWPOST] via SSL socket.
  35. 2021-07-11 16:53:38.167 INFO 16456 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
  36. 2021-07-11 16:53:38.167 INFO 16456 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
  37. 2021-07-11 16:53:38.168 INFO 16456 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 1 ms

我的应用程序属性文件
应用程序属性

  1. spring.main.allow-bean-definition-overriding=true
  2. spring.datasource.url=SOMEVALUE HERE
  3. spring.datasource.username=SOMEVALUE HERE
  4. spring.datasource.password=SOMEVALUEHERE
  5. spring.cloud.gcp.sql.database-name=SOMEVALUEHERE
  6. spring.cloud.gcp.sql.instance-connection-name=SOMVEVALUEHERE
  7. spring.jpa.show-sql=true
  8. spring.jpa.hibernate.ddl-auto=update

我的文件结构

我的应用程序运行类
demoapplication.java

  1. @SpringBootApplication(scanBasePackages = {"com.example.demo"})
  2. @EnableJpaRepositories(basePackages = {"com.example.demo.Respository"})
  3. public class DemoApplication {
  4. public static void main(String[] args) {
  5. SpringApplication.run(DemoApplication.class, args);
  6. }
  7. }


k7fdbhmy

k7fdbhmy1#

DemoApplicationcom.example.demo.Run 包裹这意味着其组件扫描,通过 @SpringBootApplication ,将在中找到组件 com.example.demo.Run 在任何子包中,如 com.example.demo.Run.example . 因此,代码的其余部分不在可以找到它的包中。
我会通过搬家来解决这个问题 DemoApplication 向上进入 com.example.demo 包裹这将意味着您现有的任何 com.example.demo.* 找到了包。
或者,你可以离开 DemoApplication 它在哪里,使用 scanBasePackages 归因于 @SpringBootApplication 列出所有其他 com.example.demo.* Package 。这将比移动更冗长 DemoApplication 并且在每次引入新包时都需要进行更改。

mwg9r5ms

mwg9r5ms2#

正如您所说的,您的第一个问题是因为主类和控制器类的包级别。但是第二个问题似乎并不依赖于重构主类。这与jdk或其他库的版本冲突有关

相关问题