我正在尝试创建一个简单的Spring Boot项目,该项目使用spring data jpa进行数据库交互。
应用程序类:
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")
}
application.properties
# 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上下文中加载,因此无法从浏览器中访问该控制器。
如何在包含公开Web服务的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
答案 0 :(得分:0)
如果不需要直接为basePackages
和@EnableJpaRepositories
声明@EntityScan
,我建议让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
,这是一种类型安全的选择。
答案 1 :(得分:0)
您提到的
我面临的问题是在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
文件中添加以下属性,并用特定于您的数据库的值替换
spring.jpa.hibernate.ddl-auto=create
spring.datasource.url=jdbc:mysql://localhost:3306/db_example
spring.datasource.username=springuser
spring.datasource.password=ThePassword
如果您已经添加了这些属性。您能否提供日志和堆栈跟踪信息以查看您遇到了什么错误。
答案 2 :(得分:0)
尝试在application.properties文件中定义以下属性。
server.servlet.context-path
spring.data.rest.base-path
现在使用上下文路径访问控制器映射,并使用rest.base-path访问其余存储库映射。