Java 无法启动RESTful Web服务IntelliJ

Java 无法启动RESTful Web服务IntelliJ,java,spring,intellij-idea,Java,Spring,Intellij Idea,我一直遵循构建RESTful web服务的指南 我将IntelliJ用于gradle项目,build.gradle的内容是: group 'SpringTest' version '1.0-SNAPSHOT' buildscript { repositories { mavenCentral() } dependencies { classpath("org.springframework.boot:spring-boot-gradle-

我一直遵循构建RESTful web服务的指南

我将IntelliJ用于gradle项目,build.gradle的内容是:

group 'SpringTest'
version '1.0-SNAPSHOT'

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:1.2.6.RELEASE")
    }
}

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

sourceCompatibility = 1.5

repositories {
    mavenCentral()
}

dependencies {
    compile 'org.springframework.boot:spring-boot-starter-web'
    testCompile group: 'junit', name: 'junit', version: '4.11'
}
以下是主要课程:

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

@SpringBootApplication
public class Application
{
    public static void main(String[] args)
    {
        SpringApplication.run(MainController.class, args);
    }
}
这是主控制器:

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MainController
{
    @RequestMapping("/test")
    public String test()
    {
        return "Test success!";
    }
}
尝试使用SpringBoot配置运行时(应用程序也是如此),会引发以下异常:

Exception in thread "main" org.springframework.context.ApplicationContextException: Unable to start embedded container; nested exception is org.springframework.context.ApplicationContextException: Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean.
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:133)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:474)
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:687)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:321)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:967)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:956)
    at testserver.Application.main(Application.java:11)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
Caused by: org.springframework.context.ApplicationContextException: Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean.
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.getEmbeddedServletContainerFactory(EmbeddedWebApplicationContext.java:183)
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.createEmbeddedServletContainer(EmbeddedWebApplicationContext.java:156)
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:130)
    ... 12 more

如何在IntelliJ中运行服务器?

在主方法中,您运行的类错误:

SpringApplication.run(MainController.class, args);
应该是:

SpringApplication.run(Application.class, args);

我不认为是这样,因为MainController类没有在其他任何地方引用,所以即使解决方案可以工作,也没有任何功能。您可能还需要进行重建,以使其获得更改。您不直接引用控制器类。Spring使用注释扫描自动查找所有控制器。重建没有帮助,我尝试将
SpringApplication
替换为
Application
,它确实运行了,但是/test返回到白标签错误页面Whoops抱歉,我在上面输入了错误的类名:)现已修复。您希望将带注释的类名称放入run语句中。如果您看到的是白标签页面,这意味着您的服务器至少正在启动。让我们来看看。