Spring Boot 1.5.22升级2.x后接口401、PostgreSQL表为空问题
Spring Boot 1.5升2.0后表结构创建成功但无数据、接口401排查方案
问题背景
运行环境为Java 8,当前正将应用从Spring Boot 1.5.22升级至2.7.0,适配Spring Boot 2.0.0阶段已完成以下调整:
- 持久层批量操作的
save()方法替换为saveAll() - 替换PostgreSQL驱动,将原依赖
compile group: 'postgresql', name: 'postgresql', version: '9.1-901-1.jdbc4'替换为implementation 'org.postgresql:postgresql:42.2.9' - 在
application.properties配置文件中新增配置项:spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation = true
当前故障现象:
项目可正常构建无报错,使用Postman携带正确Token发起请求时接口返回401状态码(升级前同场景接口返回200)。经排查,项目启动后本地PostgreSQL数据库表结构可正常创建,但所有表均无数据,是接口返回401的直接原因。
附当前项目build.gradle配置:
import org.apache.tools.ant.filters.ReplaceTokens buildscript { repositories { mavenCentral() } dependencies { classpath 'org.springframework.boot:spring-boot-gradle-plugin:2.0.0.RELEASE' } } plugins { id "io.franzbecker.gradle-lombok" version "1.11" } apply plugin: 'java' apply plugin: 'eclipse' apply plugin: 'idea' apply plugin: 'org.springframework.boot' apply plugin: "io.spring.dependency-management" project.version = '1.1.5' jar { baseName = 'refurbishment' version = project.version } sourceSets { util { compileClasspath += sourceSets.main.runtimeClasspath } } sourceCompatibility = 1.8 targetCompatibility = 1.8 repositories { maven { url "http://projectlombok.org/mavenrepo" } maven { url "http://repo.maven.apache.org/maven2" } } dependencies { compile 'org.springframework.boot:spring-boot-starter-web' compile 'org.springframework.boot:spring-boot-starter-actuator' compile group: 'org.springframework.security.oauth', name: 'spring-security-oauth2', version: '2.5.2.RELEASE' compile group: 'org.springframework.boot', name: 'spring-boot-starter-mail', version:'1.5.8.RELEASE' compile group: 'org.springframework.security', name: 'spring-security-jwt', version:'1.1.1.RELEASE' compile group: 'org.springframework.boot', name: 'spring-boot-starter-data-jpa', version:'1.5.8.RELEASE' implementation 'org.springframework.boot:spring-boot-properties-migrator:2.1.18.RELEASE' testCompile 'org.springframework.boot:spring-boot-starter-test' compile group: 'org.hibernate', name: 'hibernate-validator', version:'5.2.4.Final' compile group: 'com.google.guava', name: 'guava', version:'16.0.1' compile group: 'joda-time', name: 'joda-time', version:'2.8.2' compile group: 'commons-lang', name: 'commons-lang', version:'2.6' compile group: 'net.sf.ehcache', name: 'ehcache-core', version:'2.6.9' implementation 'org.postgresql:postgresql:42.2.9' compile group: 'io.jsonwebtoken', name: 'jjwt', version:'0.6.0' compile group: 'org.projectlombok', name: 'lombok', version:'1.16.6' compile group: 'org.apache.commons', name: 'commons-csv', version:'1.2' compile group: 'com.google.code.gson', name: 'gson', version:'2.6.2' compile group: 'commons-io', name: 'commons-io', version:'2.4' compile group: 'org.json', name: 'json', version:'20160212' compile group: 'io.springfox', name: 'springfox-swagger-ui', version:'2.5.0' compile group: 'io.springfox', name: 'springfox-swagger2', version:'2.5.0' compile group: 'com.fasterxml', name: 'classmate', version:'1.3.1' compile 'com.microsoft.azure:azure-storage:5.4.0' runtime group: 'com.microsoft.sqlserver', name: 'mssql-jdbc', version: '6.3.4.jre8-preview' compile files('libs/PDFjet-5.1.jar') testCompile(group: 'junit', name: 'junit') //DBUnit testCompile 'org.dbunit:dbunit:2.5.3' testCompile 'com.github.springtestdbunit:spring-test-dbunit:1.3.0' testCompile group: 'org.hamcrest', name: 'hamcrest-all', version:'1.3' testCompile group: 'org.assertj', name: 'assertj-core', version:'1.7.0' testCompile(group: 'org.mockito', name: 'mockito-core', version:'1.10.19') { exclude(module: 'hamcrest-core') } testCompile group: 'org.springframework', name: 'spring-test', version:'4.2.6.RELEASE' testCompile group: 'com.jayway.jsonpath', name: 'json-path', version:'2.0.0' testCompile group: 'com.jayway.jsonpath', name: 'json-path-assert', version:'0.9.1' } bootRun { def profiles = findProperty('profiles') if (profiles) { args = ["--spring.profiles.active=" + profiles] } } task copyWebConfig(type: Copy) { from('src/main/templates') { include 'web.config' } into "$buildDir/libs" filter(ReplaceTokens, tokens: [VERSION: project.version]) inputs.property("VERSION", project.version) } assemble.dependsOn(copyWebConfig)
排查与修复步骤
1. 优先修复build.gradle依赖版本冲突问题
当前配置存在严重的Spring Boot跨版本依赖混用问题,是多数隐性故障的根源:
- 删除所有Spring Boot官方starter的硬编码版本号,包括
spring-boot-starter-mail、spring-boot-starter-data-jpa,这类组件的版本由io.spring.dependency-management插件统一对齐当前Spring Boot版本,手动指定1.5.8.RELEASE会导致自动配置逻辑完全错乱 - 删除测试依赖中硬编码的
spring-test:4.2.6.RELEASE,版本交由依赖管理自动适配 - 替换不兼容的第三方依赖:
- 移除硬编码的
hibernate-validator:5.2.4.Final,Spring Boot 2.0默认适配Hibernate Validator 6.x,5.x版本会导致JPA生命周期回调、数据初始化事件监听失效 - 升级lombok至1.18.x版本,1.16.6版本对Java 8和Spring Boot 2.0的注解处理存在兼容问题
- 升级springfox至2.9.2以上版本,2.5.0版本和Spring Boot 2.0的请求映射扫描逻辑存在冲突,可能拦截初始化脚本执行请求
- 移除
spring-boot-properties-migrator:2.1.18.RELEASE,当前适配的是2.0.0版本,跨小版本的properties-migrator本身会导致配置加载异常
- 移除硬编码的
2. 修正Spring Boot 2.0的数据初始化配置
Spring Boot 2.0对数据源初始化逻辑做了不兼容变更,1.5版本的默认配置在2.0中不会生效:
- 如果你用
data.sql做初始化数据导入,需要在application.properties中新增两项配置:# 始终执行初始化脚本(2.0默认仅对内存数据库执行) spring.datasource.initialization-mode=always # 延迟初始化脚本执行,等Hibernate建表完成后再跑data.sql spring.jpa.defer-datasource-initialization=true - 如果你用Hibernate原生的
import.sql做初始化,把日志级别logging.level.org.hibernate.tool.hbm2ddl设为DEBUG,2.0版本Hibernate对SQL语法校验更严格,脚本里只要有一句语法错误就会跳过整个文件执行,且INFO级别日志不会打印报错 - 检查
spring.jpa.hibernate.ddl-auto配置,确认没有配置错误的schema指向,同时确认数据库连接账号拥有对应库的写入权限
3. 校验PostgreSQL连接配置
新版PostgreSQL驱动和旧版存在默认行为差异:
- 确认JDBC URL格式正确,新版驱动不会自动拼接缺失的端口、库名参数,检查是否实际连接到了预期的数据库实例,而非空的新建库
- 移除URL中旧版驱动专属的连接参数,比如旧版的
loglevel、sslfactory参数在新版42.2.x中配置错误会导致连接静默降级为只读模式
4. 修复后验证
依赖和配置调整完成后,重启项目查看启动日志中是否有数据脚本执行的记录,登录数据库确认用户、权限表数据存在后,再验证接口返回:
- 若仍返回401,检查Spring Security配置:Spring Boot 2.0中默认会拦截所有路径,需要确认OAuth2认证过滤器的顺序配置正确,Token解析逻辑能正常查询到数据库中的用户信息
内容的提问来源于stack exchange,提问作者Mozee
相关产品推荐
相关产品推荐

