Spring:通过Spring Boot服务连接Sql Server [英] Spring:Connect Sql server from spring boot service

查看:150
本文介绍了Spring:通过Spring Boot服务连接Sql Server的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有rest crud服务,这将帮助我在我的application.properties内部使用sql server发布和获取请求并接收respopnses.我使用类似的数据:

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:

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

这是我的宝物:

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

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

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

由以下原因引起:org.springframework.beans.factory.BeanCreationException: 无法自动连线栏位:私人 org.springframework.boot.autoconfigure.web.HttpMessageConverters org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration $ WebMvcAutoConfigurationAdapter.messageConverters; 嵌套异常是

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

和:

在类路径中定义的名称为'mappingJackson2HttpMessageConverter' 资源 [org/springframework/boot/autoconfigure/web/JacksonHttpMessageConvertersConfiguration $ MappingJackson2HttpMessageConverterConfiguration.class]: 不满意的依赖关系通过构造函数参数表示为 类型[com.fasterxml.jackson.databind.ObjectMapper]的索引0:错误 创建在类路径资源中定义的名称为"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:错误 创建在类路径资源中定义的名称为"config"的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:错误 创建在类路径中定义的名称为"resourceMappings"的bean 资源 [org/springframework/boot/autoconfigure/data/rest/SpringBootRepositoryRestMvcConfiguration.class]: 通过工厂方法实例化Bean失败;嵌套异常是

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

我应该在pom或属性文件中进行哪些更改才能使程序正常工作?

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

推荐答案

不建议使用SQL Server依赖项,请使用以下命令与SQL Server进行交互

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

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

点击链接以获取更多信息

follow links for more information

为Microsoft SQL Server配置Spring Boot

开放源代码的Microsoft JDBC驱动程序和Maven支持

这篇关于Spring:通过Spring Boot服务连接Sql Server的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆