SpringBoot报错:Unable to start web server; nested exception is org.springframework.context.Application

报错前配置

  • Controller类:
@Controller
public class HelloController {
    @ResponseBody
    @RequestMapping("/hello")
    public String hello(){
        return "hello SpringBoot!";
    }
}
  • 主程序:
@SpringBootApplication
public class HelloApplication {
    public static void main(String[] args) throws Exception {
        SpringApplication.run(HelloController.class, args);
    }
}

运行报错:
SpringBoot报错:Unable to start web server; nested exception is org.springframework.context.Application_第1张图片

ApplicationContextException:无法开始ServletWebServerApplicationContext由于缺少ServletWebServerFactory bean
原因是因为我这里的启动类是Controller类,而Controller类并没有做任何配置标注。
解决方法:在启动类之前添加一个:@EnableAutoConfiguration注解来解决问题。

  • 修改Controller如下:
@EnableAutoConfiguration
@Controller
public class HelloController {
    @ResponseBody
    @RequestMapping("/hello")
    public String hello(){
        return "hello SpringBoot!";
    }
}

如果你的springboot出现下面错误:

ERROR StatusLogger Log4j2 could not find a logging implementation. Please add log4j-core to the classpath. Using SimpleLogger to log to the console…
ERROR SpringApplication Application run failed

解决方法是添加依赖:

<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-core</artifactId>
    <version>2.10.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-api -->
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-api</artifactId>
    <version>2.10.0</version>
</dependency>

另外,附上springboot、maven、jdk版本对应:

SpringBoot版本 JDK版本 Maven版本
小于1.2.0 6 3.0
1.2.0 6 3.2+
大于1.2.0小于2.0.0 7 3.2+
大于等于2.0.0 8 3.2+

你可能感兴趣的:(JAVA,Maven)