gpt4 book ai didi

java - 在spring和eclipse中演示微服务不起作用

转载 作者:行者123 更新时间:2023-12-02 01:10:34 25 4
gpt4 key购买 nike

我正在尝试使用 Spring 库并遵循 this 在 java (eclipse) 中构建微服务的概念证明。教程。

由于某些限制,我没有使用 Maven,而是手动下载了以下 jar 文件并将它们导入到我的 java 项目中。

spring libraries being used

我的java代码非常简单,如下:

package hello;

import org.springframework.boot.SpringApplication;
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
@SpringBootApplication
@EnableAutoConfiguration
public class HelloWorld {

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

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

SpringApplication.run(HelloWorld.class,args);
}


}

问题是:当我运行代码时,服务器启动,但除了以下输出之外没有错误,并且应用程序立即关闭:

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

2019-08-29 09:41:17.999 INFO 14096 --- [ main] hello.HelloWorld : Starting HelloWorld on HQTPM146609D with PID 14096 (D:\Ramesh\ADMWorkspace\HelloWorldMicroservice\build\classes started by abdallah.alhodaly in D:\Ramesh\ADMWorkspace\HelloWorldMicroservice)
2019-08-29 09:41:18.002 INFO 14096 --- [ main] hello.HelloWorld : No active profile set, falling back to default profiles: default
2019-08-29 09:41:18.078 INFO 14096 --- [ main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@3d36e4cd: startup date [Thu Aug 29 09:41:18 GST 2019]; root of context hierarchy
2019-08-29 09:41:19.408 INFO 14096 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2019-08-29 09:41:19.426 INFO 14096 --- [ main] hello.HelloWorld : Started HelloWorld in 1.86 seconds (JVM running for 2.504)
2019-08-29 09:41:19.430 INFO 14096 --- [ Thread-3] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@3d36e4cd: startup date [Thu Aug 29 09:41:18 GST 2019]; root of context hierarchy
2019-08-29 09:41:19.432 INFO 14096 --- [ Thread-3] o.s.j.e.a.AnnotationMBeanExporter : Unregistering JMX-exposed beans on shutdown

如何修复我的代码,以便显示消息 Hello World!!!是否显示在网页上?谢谢。

更新:按照以下答案,我将应用程序的上下文设置为 WebApplicationType.SERVLET。现在,我收到以下错误:

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

2019-08-29 11:03:28.515 INFO 9416 --- [ main] hello.HelloWorld : Starting HelloWorld on HQTPM146609D with PID 9416 (D:\Ramesh\ADMWorkspace\HelloWorldMicroservice\build\classes started by abdallah.alhodaly in D:\Ramesh\ADMWorkspace\HelloWorldMicroservice)
2019-08-29 11:03:28.518 INFO 9416 --- [ main] hello.HelloWorld : No active profile set, falling back to default profiles: default
2019-08-29 11:03:28.616 INFO 9416 --- [ main] ConfigServletWebServerApplicationContext : Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@62e136d3: startup date [Thu Aug 29 11:03:28 GST 2019]; root of context hierarchy
2019-08-29 11:03:29.521 WARN 9416 --- [ 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:03:29.534 INFO 9416 --- [ main] ConditionEvaluationReportLoggingListener :

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2019-08-29 11:03:29.542 ERROR 9416 --- [ 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:29) [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

最佳答案

在主类中,您需要编写“SpringApplication.run(HelloWorldApplication.class,args);”

关于java - 在spring和eclipse中演示微服务不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57690175/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com