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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/48522338/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 02:52:35  来源:igfitidea点击:

Spring:Connect Sql server from spring boot service

javaspringhibernatemaven

提问by Omega

I have rest crud service which will help me make post and get requests and receive respopnses from sql server , inside my application.properties i use similar data:

我有 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

AND HERE IS MY POM:

这是我的 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>

when i run this as a java project i alway got this error:

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

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

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

and:

和:

with name 'mappingHymanson2HttpMessageConverter' defined in class path resource [org/springframework/boot/autoconfigure/web/HymansonHttpMessageConvertersConfiguration$MappingHymanson2HttpMessageConverterConfiguration.class]: Unsatisfied dependency expressed through constructor argument with index 0 of type [com.fasterxml.Hymanson.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.Hymanson.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

在类路径资源 [org/springframework/boot/autoconfigure/web/HymansonHttpMessageConvertersConfiguration$MappingHymanson2HttpMessageConverterConfiguration.class] 中定义了名称“mappingHymanson2HttpMessageConverter”:通过构造函数参数表达的不满意的依赖关系,类型为 [com.fasterxml.Hymanson.databind.ObjectMapper] 的索引为 0 :创建名为“objectMapper”的 bean 在类路径资源 [org/springframework/boot/autoconfigure/data/rest/SpringBootRepositoryRestMvcConfiguration.class] 中定义时出错:通过工厂方法的 Bean 实例化失败;嵌套异常是 org.springframework.beans.BeanInstantiationException:无法实例化 [com.fasterxml.Hymanson.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 实例化失败; 嵌套异常是

what should i change inside my pom or properties file to make my program work?

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

回答by alltej

Since it is a REST app, need to add the spring-boot-starter-webdependency in pom.xml

由于它是一个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>  

Add also connection properties in config (application.propertiesby default):

在配置中添加连接属性(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

Try that one and see if you can build/run the project.

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

回答by Talib Hussain

SQL Server dependency is deprecated, use following to interact with SQL Server

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

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

follow links for more information

点击链接获取更多信息

Configuring Spring Boot for Microsoft SQL Server

为 Microsoft SQL Server 配置 Spring Boot

Open sourcing the Microsoft JDBC Driver and Maven support

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