首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SpringBoot helloworld MVC应用程序无法加载

SpringBoot helloworld MVC应用程序无法加载
EN

Stack Overflow用户
提问于 2015-04-01 19:41:11
回答 2查看 1.4K关注 0票数 1

我熟悉将spring与servlet映射结合使用的老方法,但我认为我应该尝试使用SpringBoot来开发新的应用程序,以便更快地运行,并尽可能使用默认值。

我似乎不能让最基本的helloworld控制器工作。

看起来SpringBoot一点也不自举。我在Application main方法中设置了一个断点,但它没有中断。

使用gradle构建项目,并通过IntelliJ在tomcat中部署。

我确信我错过了一些非常简单的东西。但是它是什么呢?

下面是gradle文件:

代码语言:javascript
复制
buildscript {
    ext {
        springBootVersion = '1.2.3.RELEASE'
    }
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'spring-boot'
apply plugin: 'war'


war {
    baseName = 'test'
    version = '0.0.1-SNAPSHOT'
}
sourceCompatibility = 1.8
targetCompatibility = 1.8

repositories {
    mavenCentral()
}

configurations {
    providedRuntime
}

dependencies {
    compile("org.springframework.boot:spring-boot-starter-actuator")
    compile("org.springframework.boot:spring-boot-starter-security")
    compile("org.springframework.boot:spring-boot-starter-aop")
    compile("org.springframework.boot:spring-boot-starter-data-jpa")
    compile("org.springframework.boot:spring-boot-starter-web")
    compile("org.springframework.boot:spring-boot-starter-log4j2")
    providedRuntime("org.springframework.boot:spring-boot-starter-tomcat")
    testCompile("org.springframework.boot:spring-boot-starter-test")
}

task wrapper(type: Wrapper) {
    gradleVersion = '1.12'
}

Application.java

代码语言:javascript
复制
@SpringBootApplication
public class TestApplication {

public static void main(String[] args) {
  ApplicationContext ctx = SpringApplication.run(TestApplication.class, args);
}
}

Servlet初始化器:

代码语言:javascript
复制
public class ServletInitializer extends SpringBootServletInitializer {
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(TestApplication.class);
    }
}

和控制器:

代码语言:javascript
复制
@RestController
@EnableAutoConfiguration
public class IndexController {
    @RequestMapping("/")
    public String index() {
        return "Greetings from Spring Boot!";
    }
}
EN

回答 2

Stack Overflow用户

发布于 2015-04-03 17:02:21

找到了问题所在。

我在没有spring boot的情况下启动了一个新应用程序。这只是一个老式的基于xml的spring应用程序,使用这种方法我可以看到一个例外。它抱怨在一个Java版本中编译而在另一个版本中运行。我试着让它在Java8中工作。

我已经切换回Java7,果然,SpringBoot应用程序运行良好。

感谢PaulINUK的建议。但是,如果在引导SpringBoot时出现问题,您看不到异常并不是一件好事。

票数 2
EN

Stack Overflow用户

发布于 2015-04-03 04:08:16

首先,我会通过运行main方法来检查它是否与嵌入式配置一起工作(例如,从gradle配置中移除Tomcat提供的依赖项)。

它还值得通过添加以下内容来实现完整的日志记录

logging.level.org.springframework.web:调试

添加到您的应用程序。/yaml

一旦你确定了这一点,构建war,部署并发布tomcat服务器日志。如果是servlet 3+,您应该会看到有关检测到的ServletInitializers的消息。

顺便说一下,您的@EnableAutoConfiguration是多余的,因为@SpringBootApplication注释包含前者:

http://docs.spring.io/spring-boot/docs/current/reference/html/using-boot-using-springbootapplication-annotation.html

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29389838

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档