簡體   English   中英

Spring-Boot:運行Spring Boot的Restful程序失敗

[英]Spring-Boot: Failure on running of a spring-boot restful program

我是春季新手。 我編寫了一個非常簡單的寧靜項目,對實體執行CRUD操作,並且使用jpa和sql服務器。 當我運行它時,它失敗並且錯誤與數據庫和驅動程序有關。

我已經閱讀了幾乎所有與此錯誤有關的問題,但沒有一個是答案。

這是所有信息:

application.properties:
spring.datasource.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver
spring.jpa.hibernate.dialect=org.hibernate.dialect.SQLServer2012Dialect
spring.datasource.url=jdbc:sqlserver://localhost:1433;databaseName=restful;integratedSecurity=true
spring.datasource.initialize=true
spring.jpa.database=SQL_SERVER
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto = create-drop

spring.datasource.tomcat.max-wait=20000
spring.datasource.tomcat.max-active=50
spring.datasource.tomcat.max-idle=20
spring.datasource.tomcat.min-idle=15

pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.myweb</groupId>
    <artifactId>restful-api</artifactId>
    <version>1.0-SNAPSHOT</version>

    <name>restful-api</name>
    <!-- FIXME change it to the project's website -->
    <url>http://www.example.com</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>
    </properties>

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

    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>2.0.3.RELEASE</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/commons-logging/commons-logging -->
        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.2</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/com.microsoft.sqlserver/mssql-jdbc -->
        <dependency>
            <groupId>com.microsoft.sqlserver</groupId>
            <artifactId>mssql-jdbc</artifactId>
            <version>6.2.2.jre8</version>
            <scope>test</scope>
        </dependency>


        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-jpa -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
            <version>2.0.3.RELEASE</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.apache.tomcat/tomcat-jdbc -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
            <plugins>
                <plugin>
                    <artifactId>maven-clean-plugin</artifactId>
                    <version>3.0.0</version>
                </plugin>
                <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
                <plugin>
                    <artifactId>maven-resources-plugin</artifactId>
                    <version>3.0.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.7.0</version>
                </plugin>
                <plugin>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.20.1</version>
                </plugin>
                <plugin>
                    <artifactId>maven-jar-plugin</artifactId>
                    <version>3.0.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-install-plugin</artifactId>
                    <version>2.5.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-deploy-plugin</artifactId>
                    <version>2.8.2</version>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
</project>

UserDaoImp.java:

@Repository
@Transactional
public class UserDaoImp implements UserDao {
    @Autowired
    private JdbcTemplate jt;

    @Override
    public List<User> getUsers() {
        String query = "select all from Users";
        RowMapper<User> rm = new UserRowMapper();
        return jt.query(query, rm);
    }

    @Override
    public User getUser(long id) {
        String query = "select * from Users where Users.ID=?";
        RowMapper<User> rm = new UserRowMapper();
        return jt.queryForObject(query, rm, id);
    }

    @Override
    public User getUser(String name, String lastName) {
        String query = "select * from Users where Users.Name=? and Users.LastName=?";
        RowMapper<User> rm = new UserRowMapper();
        return jt.queryForObject(query, rm, name, lastName);
    }

    @Override
    public void addUser(User user) {
        if (!doesUserExist(user)) {
            String query = "insert into Users values(?,?,?);";
            jt.update(query, user.getName(), user.getLastName(), user.getId());

        }
    }

    @Override
    public void deleteUser(User user) {
        String query = "Delete from Users where Users.id=? And Users.Name=? And Users.LastName=?";
        jt.update(query, user.getId(), user.getName(), user.getLastName());
    }

    @Override
    public void updateUser(User user) {
        if (doesUserExist(user)) {
            String query = "Update Users set Name=?, LastName=? where Users.Id=?";
            jt.update(query, user.getName(), user.getLastName(), user.getId());
        }
    }

    @Override
    public boolean doesUserExist(User user) {
        String query = "select * from Users where Users.Id=? And Users.Name=? And Users.LastName=?";
        RowMapper<User> rm = new UserRowMapper();
        User u = jt.queryForObject(query, rm, user.getId(), user.getName(), user.getLastName());
        if (u == null) {
            return false;
        } else {
            return true;
        }
    }
}

ServiceImp.java:

@org.springframework.stereotype.Service
public class ServiceImp implements Service {

    @Autowired
    private UserDaoImp udi;

    @Override
    public List<User> getUsers() {
        return udi.getUsers();
    }

    @Override
    public User getUser(long id) {
        return udi.getUser(id);
    }

    @Override
    public void addUser(User user) {
        udi.addUser(user);
    }

    @Override
    public void deleteUser(User user) {
        udi.deleteUser(user);
    }

    @Override
    public void update(User user) {
        udi.updateUser(user);
    }
}

例外:

Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaConfiguration': Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.zaxxer.hikari.HikariDataSource]: Factory method 'dataSource' threw exception; nested exception is org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException: Failed to determine a suitable driver class

最后說:

Description:

Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.

Reason: Failed to determine a suitable driver class


Action:

Consider the following:
    If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.
    If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active).

注意:我不知道的一件事是我已經在pom.xml中注冊了sql服務器驅動程序的依賴項,但是該庫在External Library中不存在。

問候

嘗試這個 :

1)右鍵單擊項目。

2)Maven->更新項目

3)再次刷新項目。

或嘗試其他依賴

<!-- https://mvnrepository.com/artifact/com.microsoft.sqlserver/sqljdbc4 -->
<dependency>
    <groupId>com.microsoft.sqlserver</groupId>
    <artifactId>sqljdbc4</artifactId>
    <version>4.0</version>
    <scope>test</scope>
</dependency>

`

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM