You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring新项目构建失败求助:执行mvn spring-boot:run报错

Fixing Your Spring Boot Startup Failure

Hey there, let's break down your issues step by step since you're new to Spring Boot—no worries, these are common gotchas for beginners!

1. Java Version Compatibility Problem

Your system is running JDK 9.0.4, but your pom.xml specifies <java.version>1.8</java.version>, and Spring Boot 1.5.9.RELEASE (the version you're using) has limited support for JDK 9—it's primarily built for JDK 8. This mismatch is likely causing low-level compatibility errors during startup.

Solutions:

  • Option 1 (Most Stable): Switch your system's JDK to version 8. Since Spring Boot 1.5.x is heavily optimized for JDK 8, this will avoid most compatibility headaches.
  • Option 2: Upgrade your Spring Boot version to 2.x (e.g., 2.1.0.RELEASE or later), which fully supports JDK 9. Remember to update the <java.version> property in your pom.xml to 9 as well.

2. Controller Return Value Misconfiguration

You've added the spring-boot-starter-thymeleaf dependency, which tells Spring Boot to treat your controller's returned string as a Thymeleaf template filename, not plain text. Your code returns "Hello World.", so Spring tries to find a template file named Hello World..html in src/main/resources/templates—which doesn't exist, leading to an error.

Solutions:

  • If you want to return plain text: Add the @ResponseBody annotation to your controller method, or replace @Controller with @RestController (which automatically applies @ResponseBody to all methods):
    package com.demo.spring.controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    public class HomeController {
        @RequestMapping(value = "/", method = RequestMethod.GET)
        public String index(){
            return "Hello World.";
        }
    }
    
  • If you want to use Thymeleaf: Create a template file named index.html in src/main/resources/templates, then modify your controller to return "index" instead:
    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String index(){
        return "index";
    }
    

3. Missing Spring Boot Application Class

Spring Boot requires a root application class annotated with @SpringBootApplication to bootstrap the application. This class needs to be in a parent package of your controller (e.g., com.demo.spring for your com.demo.spring.controller package) so it can scan and register your controller bean. You haven't mentioned this class, so it's probably missing.

Solution:

Create a file named DemoSpringApplication.java in src/main/java/com/demo/spring:

package com.demo.spring;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

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

Final Steps

After fixing these issues, run mvn spring-boot:run again. If you still see errors, re-run Maven with the -e flag (mvn spring-boot:run -e) to get the full stack trace—it will give more specific details about any remaining problems.

内容的提问来源于stack exchange,提问作者Dan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 08:32:39