2015-10-13 128 views
2

我一直在遵循this指南来构建REST风格的Web服务。无法启动REST风格的Web服务IntelliJ

我使用的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); 
    } 
} 

和这里的MainController:

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配置(相同的应用程序)下面的异常是t hrown:

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中运行服务器?

回答

1

在你跑错类的主要方法:

SpringApplication.run(MainController.class, args); 

应该是:

SpringApplication.run(Application.class, args); 
+0

我不认为是这样的情况下,由于MainController类没有其他地方引用,所以即使解决方案可行,也不会有功能 – tofiffe

+0

你试过了吗? :) – nerdherd

+0

它会产生相同的错误 – tofiffe