Spring:从 spring 启动服务连接 Sql 服务器

Tkh*_*ome 5 java spring hibernate maven

我有 rest crud 服务,它将帮助我发布和获取请求并从 sql server 接收响应,在我的 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
Run Code Online (Sandbox Code Playgroud)

这是我的 POM:

<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>
Run Code Online (Sandbox Code Playgroud)

当我将它作为 Java 项目运行时,我总是收到此错误:

引起:org.springframework.beans.factory.BeanCreationException:无法自动装配字段:private org.springframework.boot.autoconfigure.web.HttpMessageConverters org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter.messageConverters; 嵌套异常是

和:

在类路径资源中定义了名称“mappingJackson2HttpMessageConverter”[org/springframework/boot/autoconfigure/web/JacksonHttpMessageConvertersConfiguration$MappingJackson2HttpMessageConverterConfiguration.class]:通过构造函数参数表达的不满意的依赖关系,类型为 [com.fasterxml.jackson.databind.ObjectMapper] :创建名为“objectMapper”的 bean 在类路径资源 [org/springframework/boot/autoconfigure/data/rest/SpringBootRepositoryRestMvcConfiguration.class] 中定义时出错:通过工厂方法的 Bean 实例化失败;嵌套异常是 org.springframework.beans.BeanInstantiationException:无法实例化 [com.fasterxml.jackson.databind.ObjectMapper]:工厂方法“objectMapper”抛出异常;嵌套异常是 org.springframework.beans。factory.BeanCreationException:在类路径资源 [org/springframework/boot/autoconfigure/data/rest/SpringBootRepositoryRestMvcConfiguration.class] 中定义名称为“config”的 bean 创建错误:通过工厂方法的 Bean 实例化失败;嵌套异常是 org.springframework.beans.BeanInstantiationException:无法实例化 [org.springframework.data.rest.core.config.RepositoryRestConfiguration]:工厂方法 'config' 抛出异常;嵌套异常是 org.springframework.beans.factory.BeanCreationException:在类路径资源 [org/springframework/boot/autoconfigure/data/rest/SpringBootRepositoryRestMvcConfiguration.class] 中定义名称为“resourceMappings”的 bean 创建时出错:通过工厂方法的 Bean 实例化失败; 嵌套异常是 在类路径资源 [org/springframework/boot/autoconfigure/data/rest/SpringBootRepositoryRestMvcConfiguration.class] 中定义名称为“config”的 bean 创建时出错:通过工厂方法的 Bean 实例化失败;嵌套异常是 org.springframework.beans.BeanInstantiationException:无法实例化 [org.springframework.data.rest.core.config.RepositoryRestConfiguration]:工厂方法 'config' 抛出异常;嵌套异常是 org.springframework.beans.factory.BeanCreationException:在类路径资源 [org/springframework/boot/autoconfigure/data/rest/SpringBootRepositoryRestMvcConfiguration.class] 中定义名称为“resourceMappings”的 bean 创建时出错:通过工厂方法的 Bean 实例化失败; 嵌套异常是 在类路径资源 [org/springframework/boot/autoconfigure/data/rest/SpringBootRepositoryRestMvcConfiguration.class] 中定义名称为“config”的 bean 创建时出错:通过工厂方法的 Bean 实例化失败;嵌套异常是 org.springframework.beans.BeanInstantiationException:无法实例化 [org.springframework.data.rest.core.config.RepositoryRestConfiguration]:工厂方法 'config' 抛出异常;嵌套异常是 org.springframework.beans.factory.BeanCreationException:在类路径资源 [org/springframework/boot/autoconfigure/data/rest/SpringBootRepositoryRestMvcConfiguration.class] 中定义名称为“resourceMappings”的 bean 创建时出错:通过工厂方法的 Bean 实例化失败; 嵌套异常是 在类路径资源[org/springframework/boot/autoconfigure/data/rest/SpringBootRepositoryRestMvcConfiguration.class]中定义:通过工厂方法的Bean实例化失败;嵌套异常是 org.springframework.beans.BeanInstantiationException:无法实例化 [org.springframework.data.rest.core.config.RepositoryRestConfiguration]:工厂方法 'config' 抛出异常;嵌套异常是 org.springframework.beans.factory.BeanCreationException:在类路径资源 [org/springframework/boot/autoconfigure/data/rest/SpringBootRepositoryRestMvcConfiguration.class] 中定义名称为“resourceMappings”的 bean 创建时出错:通过工厂方法的 Bean 实例化失败; 嵌套异常是 在类路径资源[org/springframework/boot/autoconfigure/data/rest/SpringBootRepositoryRestMvcConfiguration.class]中定义:通过工厂方法的Bean实例化失败;嵌套异常是 org.springframework.beans.BeanInstantiationException:无法实例化 [org.springframework.data.rest.core.config.RepositoryRestConfiguration]:工厂方法 'config' 抛出异常;嵌套异常是 org.springframework.beans.factory.BeanCreationException:在类路径资源 [org/springframework/boot/autoconfigure/data/rest/SpringBootRepositoryRestMvcConfiguration.class] 中定义名称为“resourceMappings”的 bean 创建时出错:通过工厂方法的 Bean 实例化失败; 嵌套异常是 通过工厂方法的 Bean 实例化失败;嵌套异常是 org.springframework.beans.BeanInstantiationException:无法实例化 [org.springframework.data.rest.core.config.RepositoryRestConfiguration]:工厂方法 'config' 抛出异常;嵌套异常是 org.springframework.beans.factory.BeanCreationException:在类路径资源 [org/springframework/boot/autoconfigure/data/rest/SpringBootRepositoryRestMvcConfiguration.class] 中定义名称为“resourceMappings”的 bean 创建时出错:通过工厂方法的 Bean 实例化失败; 嵌套异常是 通过工厂方法的 Bean 实例化失败;嵌套异常是 org.springframework.beans.BeanInstantiationException:无法实例化 [org.springframework.data.rest.core.config.RepositoryRestConfiguration]:工厂方法 'config' 抛出异常;嵌套异常是 org.springframework.beans.factory.BeanCreationException:在类路径资源 [org/springframework/boot/autoconfigure/data/rest/SpringBootRepositoryRestMvcConfiguration.class] 中定义名称为“resourceMappings”的 bean 创建时出错:通过工厂方法的 Bean 实例化失败; 嵌套异常是 config'抛出异常;嵌套异常是 org.springframework.beans.factory.BeanCreationException:在类路径资源 [org/springframework/boot/autoconfigure/data/rest/SpringBootRepositoryRestMvcConfiguration.class] 中定义名称为“resourceMappings”的 bean 创建时出错:通过工厂方法的 Bean 实例化失败; 嵌套异常是 config'抛出异常;嵌套异常是 org.springframework.beans.factory.BeanCreationException:在类路径资源 [org/springframework/boot/autoconfigure/data/rest/SpringBootRepositoryRestMvcConfiguration.class] 中定义名称为“resourceMappings”的 bean 创建时出错:通过工厂方法的 Bean 实例化失败; 嵌套异常是

我应该在我的 pom 或属性文件中更改什么才能使我的程序工作?

Tal*_*ain 9

不推荐使用 SQL Server 依赖项,使用以下内容与 SQL Server 交互

<dependency>
    <groupId>com.microsoft.sqlserver</groupId>
    <artifactId>mssql-jdbc</artifactId>
    <version>6.1.0.jre8</version>
</dependency> 
Run Code Online (Sandbox Code Playgroud)

点击链接获取更多信息

为 Microsoft SQL Server 配置 Spring Boot

开源 Microsoft JDBC 驱动程序和 Maven 支持


all*_*tej 6

由于它是一个REST应用程序,需要spring-boot-starter-webpom.xml

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
       <groupId>com.microsoft.sqlserver</groupId>
       <artifactId>sqljdbc4</artifactId>
       <version>4.0</version>
     </dependency>  
Run Code Online (Sandbox Code Playgroud)

在配置中添加连接属性(application.properties默认情况下):

spring.datasource.url=jdbc:sqlserver://localhost;databaseName=springbootdb
spring.datasource.username=sa
spring.datasource.password=replace_value
spring.datasource.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver
spring.jpa.show-sql=true
spring.jpa.hibernate.dialect=org.hibernate.dialect.SQLServer2012Dialect
spring.jpa.hibernate.ddl-auto = create-drop
Run Code Online (Sandbox Code Playgroud)

试试那个,看看你是否可以构建/运行项目。