无法启动嵌入式Tomcat org.springframework.context.ApplicationContextException

时间:2019-07-05 09:01:10

标签: java spring maven spring-boot

最近开始使用Spring Boot。尝试创建一个简单的登录名,以便我可以开始将spring mvc项目迁移到spring boot。但是继续得到错误:

  

无法启动Web服务器;嵌套的异常是org.springframework.boot.web.server.WebServerException:无法启动嵌入式Tomcat。

我已经遍历了所有stackoverflow和与无法启动嵌入式tomcat有关的文章。例如,我尝试将pom.xml更改为较旧的版本,尝试更改端口,因为有人说这可能是问题,并且尝试将spring boot控制器启动时的带注释的调用更改为{{ 1}}和@EnableAutoConfiguration似乎根本不会更改错误日志。

我也尝试过更改Java版本,并尝试同时使用Java 8和10。

@SpringBootApplication-从https://start.spring.io/

POM.XML

MainController.java

<?xml version="1.0" encoding="UTF-8"?>
<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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.bottomline</groupId>
    <artifactId>flashcards</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>flashcards</name>
    <description>Test</description>
    <packaging>war</packaging>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-rest</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jersey</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.session</groupId>
            <artifactId>spring-session-jdbc</artifactId>
        </dependency>

        <dependency>
            <groupId>com.microsoft.sqlserver</groupId>
            <artifactId>mssql-jdbc</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>
package com.bottomline.flashcards.controller;

import java.security.Principal;

import com.bottomline.flashcards.utils.WebUtils;

import org.springframework.boot.autoconfigure.EnableAutoConfiguration;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.userdetails.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@EnableAutoConfiguration   
@Controller
public class MainController {

    @RequestMapping(value = { "/", "/welcome" }, method = RequestMethod.GET)
    public String welcomePage(Model model) {
        model.addAttribute("title", "Welcome");
        model.addAttribute("message", "This is welcome page!");
        return "welcomePage";
    }

    @RequestMapping(value = "/admin", method = RequestMethod.GET)
    public String adminPage(Model model, Principal principal) {

        User loginedUser = (User) ((Authentication) principal).getPrincipal();

        String userInfo = WebUtils.toString(loginedUser);
        model.addAttribute("userInfo", userInfo);

        return "adminPage";
    }

    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public String loginPage(Model model) {

        return "loginPage";
    }

    @RequestMapping(value = "/logoutSuccessful", method = RequestMethod.GET)
    public String logoutSuccessfulPage(Model model) {
        model.addAttribute("title", "Logout");
        return "logoutSuccessfulPage";
    }

    @RequestMapping(value = "/userInfo", method = RequestMethod.GET)
    public String userInfo(Model model, Principal principal) {

        // (1) (en)
        // After user login successfully.
        // (vi)
        // Sau khi user login thanh cong se co principal
        String userName = principal.getName();

        System.out.println("User Name: " + userName);

        User loginedUser = (User) ((Authentication) principal).getPrincipal();

        String userInfo = WebUtils.toString(loginedUser);
        model.addAttribute("userInfo", userInfo);

        return "userInfoPage";
    }

    @RequestMapping(value = "/403", method = RequestMethod.GET)
    public String accessDenied(Model model, Principal principal) {

        if (principal != null) {
            User loginedUser = (User) ((Authentication) principal).getPrincipal();

            String userInfo = WebUtils.toString(loginedUser);

            model.addAttribute("userInfo", userInfo);

            String message = "Hi " + principal.getName() //
                    + "<br> You do not have permission to access this page!";
            model.addAttribute("message", message);

        }

        return "403Page";
    }

}

任何帮助都会很棒,如果没有足够的信息,请告诉我。

5 个答案:

答案 0 :(得分:3)

首先,通常可以在 last Caused by语句中找到根本原因进行调试。

因此,根据您发布的错误日志,Caused by: org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set应该是关键!

尽管Hibernate与数据库无关,但是我们可以指定当前的数据库方言,以使其为该数据库生成更好的SQL查询。因此,可以通过简单地在属性文件中标识hibernate.dialect来解决此异常,如下所示:

对于application.properties:

spring.jpa.database-platform=org.hibernate.dialect.MySQL5Dialect

对于application.yml:

spring:
  jpa:
    database-platform=org.hibernate.dialect.MySQL5Dialect

答案 1 :(得分:0)

就我而言:

  1. 我已经用相同的代码和在相同的工作空间中创建了单独的新项目
  2. 启动了应用程序。
  3. 这次tomcat在第一个实例本身中成功启动。

答案 2 :(得分:0)

1:请创建一个新项目,使其正常运行,并且tomcat正常运行; 2:然后将您在旧项目中编写的代码复制到新项目中 项目。 3:做得很好 可能是旧项目的旧工作场所坏了,无法修复 希望我能为您服务!

答案 3 :(得分:0)

通过向 application.properties 文件添加缺少的属性解决了这个问题。

spring.jpa.hibernate.ddl-auto=update

答案 4 :(得分:-2)

大概端口Tomcat正在开始正忙。检查这一点。

或者简单地定义其他端口为您的项目,编辑的 application.properties 的使用说明文件。

示例

void ResetCanShoot()
{
    CanFire = true;
}