0

几天前我看过 Stack Overflow。我的问题似乎很常见,但我无法解决它。

我试过这些方法12

这是我的 application.propreties :

spring.datasource.driver-class-name= org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://localhost:5433/mydatabase
spring.datasource.username=username
spring.datasource.password=password
spring.jpa.hibernate.ddl-auto=create
spring.main.web-application-type=none

当我尝试运行它时,它给了我以下错误:

org.springframework.context.ApplicationContextException: Unable to start web server; 
nested exception is org.springframework.context.ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean

这是我的主要课程:

@SpringBootApplication
public class StartApplication {

    @Autowired
    BookRepository repository;

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

这是我的 pom.xml:

<?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>
    <groupId>toto</groupId>
    <artifactId>toto</artifactId>
    <version>1.0-SNAPSHOT</version>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.2.RELEASE</version>
    </parent>
    <properties>
        <java.version>1.8</java.version>
        <downloadSources>true</downloadSources>
        <downloadJavadocs>true</downloadJavadocs>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>2.1.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.22.0</version>
            </plugin>
        </plugins>
    </build>
</project>

在此先感谢您的帮助。

4

3 回答 3

0

我不是专业程序员,但如果我是你我会怎么做:

  1. 确保你有一个合适的项目结构
  2. 查看Spring.io的基本配置指南!

我有我的个人项目,我在其中使用 Spring Framework 进行培训,也许你会从中得到一些有用的东西,项目链接

祝好运并玩得开心点 :)

于 2019-07-23T15:44:32.857 回答
0

你的主要课程应该是这样的:

@SpringBootApplication
public class StartApplication {
    
    @Autowired
    BookRepository repository;
    
    public static void main(String[] args) {
        SpringApplication.run(StartApplication.class, args);
    }
}
于 2020-08-26T11:57:26.753 回答
0

您应该从 application.properties 文件中删除以下属性

spring.main.web-application-type=none

这意味着您的应用程序中不需要 Web 服务器。

于 2019-07-23T15:36:30.743 回答