You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot连接SQL Server的REST CRUD服务启动异常修复咨询

解决Spring Boot REST CRUD服务连接SQL Server的启动异常问题

问题背景

我搭建了一个可与SQL Server交互的Spring Boot REST CRUD服务,支持POST、GET请求及响应,但运行项目时始终触发Bean创建相关的异常。以下是我的配置和异常详情:

application.properties 配置

server.port=9004
spring.datasource.url=jdbc:sqlserver://localhost/1433;databaseName=test1
spring.datasource.username=sa
spring.datasource.password=*****
spring.datasource.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver
spring.jpa.show-sql=true
spring.jpa.hibernate.dialect=org.hibernate.dialect.SQLServer2014Dialect
spring.jpa.hibernate.ddl-auto =ddl-auto

pom.xml 内容

<properties>
  <maven.compiler.source>1.8</maven.compiler.source>
  <maven.compiler.target>1.8</maven.compiler.target>
</properties>
<parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>1.3.6.RELEASE</version>
</parent>
<dependencies>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-rest</artifactId>
  </dependency>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
  </dependency>
  <dependency>
    <groupId>com.microsoft.sqlserver</groupId>
    <artifactId>sqljdbc4</artifactId>
    <version>4.0</version>
  </dependency>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-tomcat</artifactId>
    <scope>provided</scope>
  </dependency>
</dependencies>
<build>
  <plugins>
    <plugin>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-maven-plugin</artifactId>
    </plugin>
  </plugins>
</build>
</project>

启动异常信息

Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.springframework.boot.autoconfigure.web.HttpMessageConverters org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter.messageConverters; nested exception is and: with name 'mappingJackson2HttpMessageConverter' defined in class path resource [org/springframework/boot/autoconfigure/web/JacksonHttpMessageConvertersConfiguration$MappingJackson2HttpMessageConverterConfiguration.class]: Unsatisfied dependency expressed through constructor argument with index 0 of type [com.fasterxml.jackson.databind.ObjectMapper]: Error creating bean with name 'objectMapper' defined in class path resource [org/springframework/boot/autoconfigure/data/rest/SpringBootRepositoryRestMvcConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.fasterxml.jackson.databind.ObjectMapper]: Factory method 'objectMapper' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'config' defined in class path resource [org/springframework/boot/autoconfigure/data/rest/SpringBootRepositoryRestMvcConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.data.rest.core.config.RepositoryRestConfiguration]: Factory method 'config' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'resourceMappings' defined in class path resource [org/springframework/boot/autoconfigure/data/rest/SpringBootRepositoryRestMvcConfiguration.class]: Bean instantiation via factory method failed; nested exception is

解决方案

我帮你梳理了几个关键修改点,应该能解决这个启动异常:

1. 修复application.properties中的配置错误

  • 修正ddl-auto值:你写的spring.jpa.hibernate.ddl-auto =ddl-auto是无效的,合法值包括create、create-drop、update、validate、none,推荐用update(保留数据并更新表结构):
    spring.jpa.hibernate.ddl-auto=update
    
  • 修正SQL Server连接URL:端口应该用冒号分隔,不是斜杠,正确格式:
    spring.datasource.url=jdbc:sqlserver://localhost:1433;databaseName=test1
    

2. 升级Spring Boot版本(核心修复)

你用的1.3.6.RELEASE版本太老旧,和spring-boot-starter-data-rest的Jackson组件兼容性问题是这次异常的主要原因。建议升级到和Java 8兼容的稳定版本,比如1.5.22.RELEASE:

<parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>1.5.22.RELEASE</version>
</parent>

3. 替换SQL Server驱动依赖

旧的sqljdbc4驱动对新SQL Server版本支持有限,换成微软官方维护的mssql-jdbc驱动,适配Java 8的版本选6.4.0.jre8:

<dependency>
  <groupId>com.microsoft.sqlserver</groupId>
  <artifactId>mssql-jdbc</artifactId>
  <version>6.4.0.jre8</version>
</dependency>

4. 调整Tomcat依赖作用域

spring-boot-starter-tomcat的provided作用域会导致本地运行时缺失Tomcat相关类,如果你是本地直接启动应用,要么去掉scope属性,要么直接删除这个依赖(因为spring-boot-starter-data-rest已经包含了Tomcat):

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>

5. 检查实体类配置

最后确认你的JPA实体类是否正确添加了@Entity、@Id等注解,保证Hibernate能正确映射到SQL Server的表结构。

按以上步骤修改后,重新启动项目,应该就能正常运行了。

内容的提问来源于stack exchange,提问作者Sagitarius

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 06:36:04