当前位置: 首页 > 知识库问答 >
问题:

修正Spring Boot应用程序的类路径

郝峰
2023-03-14

我的spring boot微服务有一些问题,我已经能够成功地将我的微服务连接到运行在Google Cloud上的SQL实例,但出于某种原因,当我试图使用简单返回字符串“working”的示例endpoint测试控制器是否工作时,我得到了一个404 not found错误,我也尝试了一个示例空白项目,除了启动和运行Spring Boot所需的依赖项之外,没有其他依赖项,但我仍然面临同样的问题,即我得到了一个404 not found错误,尽管它正在正确编译和运行。我已经检查了我的防火墙,它没有阻止服务。

然而,在浏览堆栈溢出时,我意识到问题是我的包结构,它没有在我的微服务中找到控制器。

问题出在我的demoapplication.java(包含main方法的方法在一个名为com.example.demo.run的包中,显然这是导致问题的原因,我没有修复它,并将其移到com.example.demo。

但是现在我面临一个新的问题,下面是我的堆栈跟踪

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

Description:

An attempt was made to call a method that does not exist. The attempt was made from the following location:

    org.springframework.data.repository.core.support.TransactionalRepositoryProxyPostProcessor$RepositoryAnnotationTransactionAttributeSource.<init>(TransactionalRepositoryProxyPostProcessor.java:111)

The following method did not exist:

    'void org.springframework.transaction.annotation.AnnotationTransactionAttributeSource.<init>(boolean)'

The method's class, org.springframework.transaction.annotation.AnnotationTransactionAttributeSource, is available from the following locations:

    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
    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

The class hierarchy was loaded from the following locations:

    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
    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


Action:

Correct the classpath of your application so that it contains a single, compatible version of org.springframework.transaction.annotation.AnnotationTransactionAttributeSource


Process finished with exit code 1

这是控制器

videoController.java

package com.example.demo.Controllers;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class VideoController {


    @RequestMapping(path = "/test")
    public String index() {
        return "Greetings from Spring Boot!";
    }
}
    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)
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
2021-07-11 16:53:29.149  INFO 16456 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
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.
2021-07-11 16:53:29.510  INFO 16456 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2021-07-11 16:53:29.517  INFO 16456 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2021-07-11 16:53:29.517  INFO 16456 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.48]
2021-07-11 16:53:29.624  INFO 16456 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2021-07-11 16:53:29.625  INFO 16456 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 913 ms
2021-07-11 16:53:29.751  INFO 16456 --- [           main] o.hibernate.jpa.internal.util.LogHelper  : HHH000204: Processing PersistenceUnitInfo [name: default]
2021-07-11 16:53:29.788  INFO 16456 --- [           main] org.hibernate.Version                    : HHH000412: Hibernate ORM core version 5.4.32.Final
2021-07-11 16:53:29.888  INFO 16456 --- [           main] o.hibernate.annotations.common.Version   : HCANN000001: Hibernate Commons Annotations {5.1.2.Final}
2021-07-11 16:53:29.971  INFO 16456 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
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.
2021-07-11 16:53:30.025  INFO 16456 --- [           main] c.g.cloud.sql.core.CoreSocketFactory     : First Cloud SQL connection, generating RSA key pair.
2021-07-11 16:53:31.820  INFO 16456 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
2021-07-11 16:53:31.835  INFO 16456 --- [           main] org.hibernate.dialect.Dialect            : HHH000400: Using dialect: org.hibernate.dialect.MySQL57Dialect
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.
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.
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]
2021-07-11 16:53:32.049  INFO 16456 --- [           main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
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
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.
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.
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
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]
2021-07-11 16:53:32.277  INFO 16456 --- [           main] c.g.c.s.a.c.GcpContextAutoConfiguration  : The default project ID is XXXXXXXXHIDDENFROMSTACKOVERFLOWPOST
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.
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 ''
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)
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.
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.
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.
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.
2021-07-11 16:53:38.167  INFO 16456 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
2021-07-11 16:53:38.167  INFO 16456 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2021-07-11 16:53:38.168  INFO 16456 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 1 ms
  spring.main.allow-bean-definition-overriding=true
spring.datasource.url=SOMEVALUE HERE
spring.datasource.username=SOMEVALUE HERE
spring.datasource.password=SOMEVALUEHERE
spring.cloud.gcp.sql.database-name=SOMEVALUEHERE
spring.cloud.gcp.sql.instance-connection-name=SOMVEVALUEHERE
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update

   @SpringBootApplication(scanBasePackages = {"com.example.demo"})
@EnableJpaRepositories(basePackages = {"com.example.demo.Respository"})
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

}

共有1个答案

龙才俊
2023-03-14

DemoApplication位于com.example.demo.run包中。这意味着通过@springbootapplication启用的组件扫描将在com.example.demo.run和任何子包(如com.example.demo.run.example)中查找组件。因此,其余的代码不在可以找到它的包中。

我将通过将demoapplication向上移动到com.example.demo包来解决这个问题。这意味着可以在任何现有的com.example.demo.*包中找到组件。

或者,您可以保留demoapplication的位置,并使用@springbootapplication上的scanbasePackages属性列出所有其他com.example.demo.*包。这将比移动demoapplication更冗长,并且每次引入新包时都需要进行更改。

 类似资料:
  • 尝试运行spring项目时出现以下错误: 我认为这个错误可能是由于某些依赖项的版本冲突造成的,但我不是很确定。这是我的第一个Spring项目。 这是我更新的pom。xml文件 如果您发现任何错误,请随时指出并解释原因。尽可能多地学习。

  • 我有一个springboot应用程序,我试图使用ByteBuddy来测试它。我遇到了类路径问题,我无法理解。 首先,以下是关于这方面的其他文献: https://github.com/raphw/byte-buddy/issues/473 这里需要注意的一点是,如果我使用IntelliJ运行应用程序,它不会使用uber-jar,而是通过main类运行,其中有一堆JAR作为类路径参数。 由于这种差异

  • 他推荐我的操作是:更正应用程序的类路径,使其包含类 org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory 和 org.apache.catalina.Context 的兼容版本 这是控制台的error

  • 我在运行Springboot Maven应用程序时遇到问题。这个问题始于一个缺失的依赖关系,这让我无法解决另一个问题,我尝试了来自整个网络的多个建议,但都无济于事。我确实移除了。m2文件夹并重新构建,但同样的错误再次出现。 我控制台中的错误: 我的pom。xml: 欢迎任何意见。

  • 这是我使用SpringBoot的第一天,我试图理解体系结构,因此我开始构建一个hello world应用程序: 在我的pom.xml中,在maven-shade-plugin下,我将mainClass声明如下: 文件目标是src/main/java/com/demo/helloworld.java,该文件中的代码是: 我错过了什么?

  • 我有个问题。尝试改变版本(就像它在互联网上说的),但没有帮助。我已经读过了,这些答案修正了应用程序的类路径,使它包含一个单一的、兼容的org.springframework.plugin.core.pluginregistry版本,但它也没有帮助。 问题: 更正应用程序的类路径,使其包含Org.SpringFramework.http.Converter.Support.AllEncompassi