【问题标题】:@RestController not working with spring-data-jpa starters@RestController 不适用于 spring-data-jpa 启动器
【发布时间】:2019-10-23 15:24:08
【问题描述】:

我正在尝试创建一个简单的 Spring Boot 项目,该项目使用 spring data jpa 进行 DB 交互。

应用类:

package org.railway.fms.documentmgmt;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;

@SpringBootApplication
@EnableJpaRepositories(basePackages = {"org.railway.fms.documentmgmt.repository"})
@EntityScan(basePackages= {"org.railway.fms.documentmgmt.entities"})
public class FMSApplication {

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

}

控制器类:

package org.railway.fms.documentmgmt;

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

@RestController
public class NatureOfDocumentRestService {

    @GetMapping("/document/nature")
    public String getNatureOfDocuments() {
        return "test";
    }
}

build.gradle 文件:

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:2.1.4.RELEASE")
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'

bootJar {
    baseName = 'document-mgmt'
    version =  '0.1.0'
}

repositories {
    mavenCentral()
}

sourceCompatibility = 1.8
targetCompatibility = 1.8

dependencies {
    compile ("org.springframework.boot:spring-boot-starter-web")
    implementation ("org.springframework.boot:spring-boot-starter-data-jpa")
    implementation ("org.postgresql:postgresql")
    testCompile("junit:junit")
}

应用程序属性

# Database
spring.datasource.url=jdbc:postgresql://localhost:5444/db?currentSchema=fms
spring.datasource.username=username
spring.datasource.password=password

# The SQL dialect makes Hibernate generate better SQL for the chosen database
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect

# Hibernate ddl auto (create, create-drop, validate, update)
spring.jpa.hibernate.ddl-auto = update

spring.jpa.show-sql=true

我面临的问题是,当我的 build.gradle 文件中没有使用 org.springframework.boot:spring-boot-starter-data-jpa 依赖项时,我能够成功地从浏览器中访问我的控制器。

但是当我使用 org.springframework.boot:spring-boot-starter-data-jpa 时,控制器没有在 spring 上下文中加载,我无法从浏览器中点击控制器。

如何在包含暴露网络服务的 Spring Boot 项目中使用 spring-data-jpa,请帮助!

注意:日志中没有错误,应用程序启动成功。

日志:

2019-06-10 09:52:11.348  INFO 15540 --- [           main] o.r.fms.documentmgmt.FMSApplication      : Starting FMSApplication on abcd with PID 15540 (C:\Users\furquan.ahmed\Workspaces\fmsWorkspace\document-mgmt\bin\main started by furquan.ahmed in C:\Users\furquan.ahmed\Workspaces\fmsWorkspace\document-mgmt)
2019-06-10 09:52:11.354  INFO 15540 --- [           main] o.r.fms.documentmgmt.FMSApplication      : No active profile set, falling back to default profiles: default
2019-06-10 09:52:12.200  INFO 15540 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data repositories in DEFAULT mode.
2019-06-10 09:52:12.228  INFO 15540 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 13ms. Found 0 repository interfaces.
2019-06-10 09:52:13.083  INFO 15540 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration' of type [org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration$$EnhancerBySpringCGLIB$$5149c32d] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2019-06-10 09:52:14.106  INFO 15540 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2019-06-10 09:52:14.161  INFO 15540 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2019-06-10 09:52:14.162  INFO 15540 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.17]
2019-06-10 09:52:14.402  INFO 15540 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2019-06-10 09:52:14.402  INFO 15540 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 2968 ms
2019-06-10 09:52:14.714  INFO 15540 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
2019-06-10 09:52:18.002  INFO 15540 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
2019-06-10 09:52:18.099  INFO 15540 --- [           main] o.hibernate.jpa.internal.util.LogHelper  : HHH000204: Processing PersistenceUnitInfo [
    name: default
    ...]
2019-06-10 09:52:18.267  INFO 15540 --- [           main] org.hibernate.Version                    : HHH000412: Hibernate Core {5.3.9.Final}
2019-06-10 09:52:18.268  INFO 15540 --- [           main] org.hibernate.cfg.Environment            : HHH000206: hibernate.properties not found
2019-06-10 09:52:18.494  INFO 15540 --- [           main] o.hibernate.annotations.common.Version   : HCANN000001: Hibernate Commons Annotations {5.0.4.Final}
2019-06-10 09:52:19.060  INFO 15540 --- [           main] org.hibernate.dialect.Dialect            : HHH000400: Using dialect: org.hibernate.dialect.PostgreSQLDialect

【问题讨论】:

  • 请发布application.properties文件和日志。
  • 日志没有错误,我已经添加了日志和属性文件
  • 你的意思是'HAL'?
  • 对不起,我没听懂!

标签: spring spring-boot jpa spring-data-jpa spring-data


【解决方案1】:

如果没有直接需要为@EnableJpaRepositories@EntityScan 明确声明basePackages,我建议让Spring 通过自动配置找出位置,因为您的所有类都位于org.railway.fms.documentmgmt

@SpringBootApplication
@EnableJpaRepositories
public class FMSApplication { ... }

否则,请尝试添加@ComponentScan 来说明 Spring 组件的位置:

@SpringBootApplication
@EnableJpaRepositories(basePackages = "org.railway.fms.documentmgmt.repository")
@EntityScan(basePackages= "org.railway.fms.documentmgmt.entities")
@ComponentScan(basePackages= "org.railway.fms.documentmgmt")
public class FMSApplication { ... }

此外,如果您确实需要说明基本包的位置,还可以选择使用 basePackageClasses 代替 basePackages,这是一种类型安全的替代方案。

【讨论】:

  • 对不起,这没有帮助
  • 在显式 ComponentScan 中也不需要,因为 @SpringBootApplication 包含具有相同默认包的 @ComponentScan(FMSApplication 类所在的位置)
【解决方案2】:

如你所说

我面临的问题是,当我没有在 build.gradle 文件中使用 org.springframework.boot:spring-boot-starter-data-jpa 依赖项时,我能够成功地从浏览器中访问我的控制器。 但是当我使用 org.springframework.boot:spring-boot-starter-data-jpa 时,控制器没有在 spring 上下文中加载,我无法从浏览器中点击控制器。

我认为当您添加org.springframework.boot:spring-boot-starter-data-jpa 时,您可能忘记在application.properties 中添加数据库配置 如果我是正确的,那么在application.properties 文件中添加以下属性并将值替换为特定于您的 db

spring.jpa.hibernate.ddl-auto=create
spring.datasource.url=jdbc:mysql://localhost:3306/db_example
spring.datasource.username=springuser
spring.datasource.password=ThePassword

如果您已经添加了这些属性。您能否提供日志和堆栈跟踪以查看您遇到了什么错误。

【讨论】:

  • 我的 application.properties 中有这些属性,并且应用程序成功连接到数据库,但控制器类未加载到 spring 上下文中。
  • 我没有看到任何错误。此外,没有什么像服务器启动一样。我怀疑你是否放了整个日志。你能创建一个最小项目并上传到 github 并分享 Repo URL 吗?
【解决方案3】:

尝试在 application.properties 文件中定义以下属性。

server.servlet.context-path
spring.data.rest.base-path

现在使用 context-path 访问您的控制器映射,并使用 rest.base-path 访问其余存储库映射。

【讨论】:

    猜你喜欢
    • 2023-03-06
    • 2019-04-19
    • 2015-05-31
    • 1970-01-01
    • 2016-12-03
    • 1970-01-01
    • 2019-08-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多