org.springframework.context.ApplicationContextException:由于缺少ServletWebServerFactory bean而无法启动ServletWebServerApplicationContext

时间:2019-08-29 07:43:15

标签: java spring spring-boot

我正在尝试使用Spring库为Java(Eclipse)中的微服务构建概念证明。由于某些限制,我没有使用Maven,而是手动下载了以下jar文件并将其导入到我的Java项目中。

enter image description here

我的代码如下:

package hello;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@EnableAutoConfiguration
@SpringBootApplication
public class HelloWorld{

    @RequestMapping("/")
    //http://localhost:port/
    String hello()
    {
        return "Hello World!!!";
    }

    public static void main (String[] args) throws Exception
    {

        SpringApplication app = new SpringApplication(HelloWorld.class);

        app.setWebApplicationType(WebApplicationType.SERVLET);

        app.run(args);
    }


}

不幸的是,当我运行代码时,出现以下错误。如何在不启动服务器的情况下运行程序?

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.0.0.RELEASE)

2019-08-29 11:33:59.550  INFO 15572 --- [           main] hello.HelloWorld                         : Starting HelloWorld on HQTPM146609D with PID 15572 (D:\Ramesh\ADMWorkspace\HelloWorldMicroservice\build\classes started by abdallah.alhodaly in D:\Ramesh\ADMWorkspace\HelloWorldMicroservice)
2019-08-29 11:33:59.554  INFO 15572 --- [           main] hello.HelloWorld                         : No active profile set, falling back to default profiles: default
2019-08-29 11:33:59.682  INFO 15572 --- [           main] ConfigServletWebServerApplicationContext : Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@18a70f16: startup date [Thu Aug 29 11:33:59 GST 2019]; root of context hierarchy
2019-08-29 11:34:00.473  WARN 15572 --- [           main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is org.springframework.context.ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean.
2019-08-29 11:34:00.485  INFO 15572 --- [           main] ConditionEvaluationReportLoggingListener : 

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2019-08-29 11:34:00.495 ERROR 15572 --- [           main] o.s.boot.SpringApplication               : Application run failed

org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is org.springframework.context.ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean.
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:155) ~[spring-boot-2.0.0.RELEASE.jar:2.0.0.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543) ~[spring-context-5.0.4.RELEASE.jar:5.0.4.RELEASE]
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:140) ~[spring-boot-2.0.0.RELEASE.jar:2.0.0.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:752) ~[spring-boot-2.0.0.RELEASE.jar:2.0.0.RELEASE]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:388) ~[spring-boot-2.0.0.RELEASE.jar:2.0.0.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:327) ~[spring-boot-2.0.0.RELEASE.jar:2.0.0.RELEASE]
    at hello.HelloWorld.main(HelloWorld.java:30) [classes/:na]
Caused by: org.springframework.context.ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean.
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.getWebServerFactory(ServletWebServerApplicationContext.java:204) ~[spring-boot-2.0.0.RELEASE.jar:2.0.0.RELEASE]
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.createWebServer(ServletWebServerApplicationContext.java:178) ~[spring-boot-2.0.0.RELEASE.jar:2.0.0.RELEASE]
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:152) ~[spring-boot-2.0.0.RELEASE.jar:2.0.0.RELEASE]
    ... 6 common frames omitted

0 个答案:

没有答案
相关问题